2020-04-03 11:53:31 +00:00
|
|
|
__filename__ = "happening.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-04-03 11:53:31 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
2021-09-10 16:14:50 +00:00
|
|
|
__email__ = "bob@libreserver.org"
|
2020-04-03 11:53:31 +00:00
|
|
|
__status__ = "Production"
|
2021-06-25 16:10:09 +00:00
|
|
|
__module_group__ = "Core"
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2020-02-22 19:11:51 +00:00
|
|
|
import os
|
2020-08-13 13:40:38 +00:00
|
|
|
from uuid import UUID
|
2020-02-22 19:11:51 +00:00
|
|
|
from datetime import datetime
|
2021-05-31 13:00:17 +00:00
|
|
|
from datetime import timedelta
|
2020-02-22 19:11:51 +00:00
|
|
|
|
2021-12-28 14:41:10 +00:00
|
|
|
from utils import is_public_post
|
2021-12-26 15:13:34 +00:00
|
|
|
from utils import load_json
|
2021-12-26 14:47:21 +00:00
|
|
|
from utils import save_json
|
2021-12-26 20:36:08 +00:00
|
|
|
from utils import locate_post
|
2021-12-26 10:57:03 +00:00
|
|
|
from utils import has_object_dict
|
2021-12-26 12:02:29 +00:00
|
|
|
from utils import acct_dir
|
2020-02-22 19:11:51 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def _valid_uuid(testUuid: str, version: int):
|
2020-08-13 13:40:38 +00:00
|
|
|
"""Check if uuid_to_test is a valid UUID
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
uuid_obj = UUID(testUuid, version=version)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return str(uuid_obj) == testUuid
|
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def _remove_event_from_timeline(eventId: str, tlEventsFilename: str) -> None:
|
2020-08-13 13:40:38 +00:00
|
|
|
"""Removes the given event Id from the timeline
|
|
|
|
"""
|
|
|
|
if eventId + '\n' not in open(tlEventsFilename).read():
|
|
|
|
return
|
2021-06-21 22:52:04 +00:00
|
|
|
with open(tlEventsFilename, 'r') as fp:
|
|
|
|
eventsTimeline = fp.read().replace(eventId + '\n', '')
|
2021-06-21 22:53:04 +00:00
|
|
|
try:
|
|
|
|
with open(tlEventsFilename, 'w+') as fp2:
|
|
|
|
fp2.write(eventsTimeline)
|
2021-11-25 21:18:53 +00:00
|
|
|
except OSError:
|
2021-10-29 18:48:15 +00:00
|
|
|
print('EX: ERROR: unable to save events timeline')
|
2020-08-13 13:40:38 +00:00
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def save_event_post(base_dir: str, handle: str, post_id: str,
|
|
|
|
eventJson: {}) -> bool:
|
2020-08-13 13:40:38 +00:00
|
|
|
"""Saves an event to the calendar and/or the events timeline
|
|
|
|
If an event has extra fields, as per Mobilizon,
|
|
|
|
Then it is saved as a separate entity and added to the
|
|
|
|
events timeline
|
2020-08-20 17:08:25 +00:00
|
|
|
See https://framagit.org/framasoft/mobilizon/-/blob/
|
|
|
|
master/lib/federation/activity_stream/converter/event.ex
|
2020-08-13 11:58:05 +00:00
|
|
|
"""
|
2021-12-25 16:17:53 +00:00
|
|
|
if not os.path.isdir(base_dir + '/accounts/' + handle):
|
2021-07-18 14:15:16 +00:00
|
|
|
print('WARN: Account does not exist at ' +
|
2021-12-25 16:17:53 +00:00
|
|
|
base_dir + '/accounts/' + handle)
|
|
|
|
calendarPath = base_dir + '/accounts/' + handle + '/calendar'
|
2020-08-13 11:58:05 +00:00
|
|
|
if not os.path.isdir(calendarPath):
|
|
|
|
os.mkdir(calendarPath)
|
|
|
|
|
|
|
|
# get the year, month and day from the event
|
|
|
|
eventTime = datetime.strptime(eventJson['startTime'],
|
|
|
|
"%Y-%m-%dT%H:%M:%S%z")
|
|
|
|
eventYear = int(eventTime.strftime("%Y"))
|
2020-08-13 13:40:38 +00:00
|
|
|
if eventYear < 2020 or eventYear >= 2100:
|
|
|
|
return False
|
2020-08-13 11:58:05 +00:00
|
|
|
eventMonthNumber = int(eventTime.strftime("%m"))
|
2020-08-13 13:40:38 +00:00
|
|
|
if eventMonthNumber < 1 or eventMonthNumber > 12:
|
|
|
|
return False
|
2020-08-13 11:58:05 +00:00
|
|
|
eventDayOfMonth = int(eventTime.strftime("%d"))
|
2020-08-13 16:19:35 +00:00
|
|
|
if eventDayOfMonth < 1 or eventDayOfMonth > 31:
|
2020-08-13 13:40:38 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if eventJson.get('name') and eventJson.get('actor') and \
|
|
|
|
eventJson.get('uuid') and eventJson.get('content'):
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _valid_uuid(eventJson['uuid'], 4):
|
2020-08-13 13:40:38 +00:00
|
|
|
return False
|
2020-08-20 16:51:48 +00:00
|
|
|
print('Mobilizon type event')
|
2020-08-13 13:40:38 +00:00
|
|
|
# if this is a full description of an event then save it
|
|
|
|
# as a separate json file
|
2021-12-25 16:17:53 +00:00
|
|
|
eventsPath = base_dir + '/accounts/' + handle + '/events'
|
2020-08-13 13:40:38 +00:00
|
|
|
if not os.path.isdir(eventsPath):
|
|
|
|
os.mkdir(eventsPath)
|
|
|
|
eventsYearPath = \
|
2021-12-25 16:17:53 +00:00
|
|
|
base_dir + '/accounts/' + handle + '/events/' + str(eventYear)
|
2020-08-13 13:40:38 +00:00
|
|
|
if not os.path.isdir(eventsYearPath):
|
|
|
|
os.mkdir(eventsYearPath)
|
|
|
|
eventId = str(eventYear) + '-' + eventTime.strftime("%m") + '-' + \
|
|
|
|
eventTime.strftime("%d") + '_' + eventJson['uuid']
|
|
|
|
eventFilename = eventsYearPath + '/' + eventId + '.json'
|
|
|
|
|
2021-12-26 14:47:21 +00:00
|
|
|
save_json(eventJson, eventFilename)
|
2020-08-13 13:40:38 +00:00
|
|
|
# save to the events timeline
|
2021-12-25 16:17:53 +00:00
|
|
|
tlEventsFilename = base_dir + '/accounts/' + handle + '/events.txt'
|
2020-08-13 13:40:38 +00:00
|
|
|
|
|
|
|
if os.path.isfile(tlEventsFilename):
|
2021-12-29 21:55:09 +00:00
|
|
|
_remove_event_from_timeline(eventId, tlEventsFilename)
|
2020-08-13 13:40:38 +00:00
|
|
|
try:
|
|
|
|
with open(tlEventsFilename, 'r+') as tlEventsFile:
|
|
|
|
content = tlEventsFile.read()
|
2020-12-29 20:22:28 +00:00
|
|
|
if eventId + '\n' not in content:
|
|
|
|
tlEventsFile.seek(0, 0)
|
|
|
|
tlEventsFile.write(eventId + '\n' + content)
|
2021-12-25 15:28:52 +00:00
|
|
|
except OSError as ex:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: Failed to write entry to events file ' +
|
2021-12-25 15:28:52 +00:00
|
|
|
tlEventsFilename + ' ' + str(ex))
|
2020-08-13 13:40:38 +00:00
|
|
|
return False
|
|
|
|
else:
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(tlEventsFilename, 'w+') as tlEventsFile:
|
|
|
|
tlEventsFile.write(eventId + '\n')
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + tlEventsFilename)
|
2020-08-13 11:58:05 +00:00
|
|
|
|
|
|
|
# create a directory for the calendar year
|
|
|
|
if not os.path.isdir(calendarPath + '/' + str(eventYear)):
|
|
|
|
os.mkdir(calendarPath + '/' + str(eventYear))
|
|
|
|
|
|
|
|
# calendar month file containing event post Ids
|
|
|
|
calendarFilename = calendarPath + '/' + str(eventYear) + \
|
|
|
|
'/' + str(eventMonthNumber) + '.txt'
|
|
|
|
|
|
|
|
# Does this event post already exist within the calendar month?
|
|
|
|
if os.path.isfile(calendarFilename):
|
2021-12-26 19:47:06 +00:00
|
|
|
if post_id in open(calendarFilename).read():
|
2020-08-13 11:58:05 +00:00
|
|
|
# Event post already exists
|
|
|
|
return False
|
|
|
|
|
|
|
|
# append the post Id to the file for the calendar month
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(calendarFilename, 'a+') as calendarFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
calendarFile.write(post_id + '\n')
|
2021-11-25 21:18:53 +00:00
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to append ' + calendarFilename)
|
2020-08-13 11:58:05 +00:00
|
|
|
|
|
|
|
# create a file which will trigger a notification that
|
|
|
|
# a new event has been added
|
2021-12-25 16:17:53 +00:00
|
|
|
calNotifyFilename = base_dir + '/accounts/' + handle + '/.newCalendar'
|
2021-11-25 21:18:53 +00:00
|
|
|
notifyStr = \
|
|
|
|
'/calendar?year=' + str(eventYear) + '?month=' + \
|
|
|
|
str(eventMonthNumber) + '?day=' + str(eventDayOfMonth)
|
|
|
|
try:
|
|
|
|
with open(calNotifyFilename, 'w+') as calendarNotificationFile:
|
|
|
|
calendarNotificationFile.write(notifyStr)
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + calNotifyFilename)
|
2021-11-25 21:18:53 +00:00
|
|
|
return False
|
2020-08-13 11:58:05 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def _is_happening_event(tag: {}) -> bool:
|
2020-02-24 11:10:48 +00:00
|
|
|
"""Is this tag an Event or Place ActivityStreams type?
|
|
|
|
"""
|
|
|
|
if not tag.get('type'):
|
|
|
|
return False
|
2020-04-03 11:53:31 +00:00
|
|
|
if tag['type'] != 'Event' and tag['type'] != 'Place':
|
2020-02-24 11:10:48 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def _is_happening_post(post_json_object: {}) -> bool:
|
2020-02-24 11:10:48 +00:00
|
|
|
"""Is this a post with tags?
|
|
|
|
"""
|
2021-12-25 22:09:19 +00:00
|
|
|
if not post_json_object:
|
2020-02-24 11:10:48 +00:00
|
|
|
return False
|
2021-12-26 10:57:03 +00:00
|
|
|
if not has_object_dict(post_json_object):
|
2020-02-24 11:10:48 +00:00
|
|
|
return False
|
2021-12-25 22:09:19 +00:00
|
|
|
if not post_json_object['object'].get('tag'):
|
2020-02-24 11:10:48 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def get_todays_events(base_dir: str, nickname: str, domain: str,
|
|
|
|
currYear: int, currMonthNumber: int,
|
|
|
|
currDayOfMonth: int) -> {}:
|
2020-02-22 19:11:51 +00:00
|
|
|
"""Retrieves calendar events for today
|
|
|
|
Returns a dictionary of lists containing Event and Place activities
|
|
|
|
"""
|
2020-04-03 11:53:31 +00:00
|
|
|
now = datetime.now()
|
2020-02-22 19:11:51 +00:00
|
|
|
if not currYear:
|
2020-04-03 11:53:31 +00:00
|
|
|
year = now.year
|
2020-02-22 19:11:51 +00:00
|
|
|
else:
|
2020-04-03 11:53:31 +00:00
|
|
|
year = currYear
|
2020-02-22 19:11:51 +00:00
|
|
|
if not currMonthNumber:
|
2020-04-03 11:53:31 +00:00
|
|
|
monthNumber = now.month
|
2020-02-22 19:11:51 +00:00
|
|
|
else:
|
2020-04-03 11:53:31 +00:00
|
|
|
monthNumber = currMonthNumber
|
2020-02-22 19:11:51 +00:00
|
|
|
if not currDayOfMonth:
|
2020-04-03 11:53:31 +00:00
|
|
|
dayNumber = now.day
|
2020-02-22 19:11:51 +00:00
|
|
|
else:
|
2020-04-03 11:53:31 +00:00
|
|
|
dayNumber = currDayOfMonth
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarFilename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(base_dir, nickname, domain) + \
|
2020-04-03 11:53:31 +00:00
|
|
|
'/calendar/' + str(year) + '/' + str(monthNumber) + '.txt'
|
|
|
|
events = {}
|
2020-02-22 19:11:51 +00:00
|
|
|
if not os.path.isfile(calendarFilename):
|
|
|
|
return events
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarPostIds = []
|
|
|
|
recreateEventsFile = False
|
|
|
|
with open(calendarFilename, 'r') as eventsFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in eventsFile:
|
|
|
|
post_id = post_id.replace('\n', '').replace('\r', '')
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain, post_id)
|
|
|
|
if not post_filename:
|
2020-04-03 11:53:31 +00:00
|
|
|
recreateEventsFile = True
|
2020-02-23 09:49:10 +00:00
|
|
|
continue
|
|
|
|
|
2021-12-26 23:41:34 +00:00
|
|
|
post_json_object = load_json(post_filename)
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_post(post_json_object):
|
2020-02-23 09:49:10 +00:00
|
|
|
continue
|
|
|
|
|
2021-12-28 14:41:10 +00:00
|
|
|
publicEvent = is_public_post(post_json_object)
|
2021-03-07 14:52:33 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
postEvent = []
|
|
|
|
dayOfMonth = None
|
2021-12-25 22:09:19 +00:00
|
|
|
for tag in post_json_object['object']['tag']:
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_event(tag):
|
2020-02-22 19:11:51 +00:00
|
|
|
continue
|
2020-02-24 11:10:48 +00:00
|
|
|
# this tag is an event or a place
|
2020-04-03 11:53:31 +00:00
|
|
|
if tag['type'] == 'Event':
|
2020-02-23 09:49:10 +00:00
|
|
|
# tag is an event
|
|
|
|
if not tag.get('startTime'):
|
2020-02-22 19:11:51 +00:00
|
|
|
continue
|
2020-04-03 11:53:31 +00:00
|
|
|
eventTime = \
|
|
|
|
datetime.strptime(tag['startTime'],
|
2020-02-23 09:49:10 +00:00
|
|
|
"%Y-%m-%dT%H:%M:%S%z")
|
2020-04-03 11:53:31 +00:00
|
|
|
if int(eventTime.strftime("%Y")) == year and \
|
|
|
|
int(eventTime.strftime("%m")) == monthNumber and \
|
|
|
|
int(eventTime.strftime("%d")) == dayNumber:
|
|
|
|
dayOfMonth = str(int(eventTime.strftime("%d")))
|
2021-12-26 19:47:06 +00:00
|
|
|
if '#statuses#' in post_id:
|
2020-02-24 10:55:49 +00:00
|
|
|
# link to the id so that the event can be
|
|
|
|
# easily deleted
|
2021-12-26 19:47:06 +00:00
|
|
|
tag['post_id'] = post_id.split('#statuses#')[1]
|
|
|
|
tag['sender'] = post_id.split('#statuses#')[0]
|
2021-03-06 18:38:36 +00:00
|
|
|
tag['sender'] = tag['sender'].replace('#', '/')
|
2021-03-07 14:52:33 +00:00
|
|
|
tag['public'] = publicEvent
|
2020-02-22 19:11:51 +00:00
|
|
|
postEvent.append(tag)
|
2020-02-23 09:49:10 +00:00
|
|
|
else:
|
|
|
|
# tag is a place
|
|
|
|
postEvent.append(tag)
|
|
|
|
if postEvent and dayOfMonth:
|
2021-12-26 19:47:06 +00:00
|
|
|
calendarPostIds.append(post_id)
|
2020-02-23 09:49:10 +00:00
|
|
|
if not events.get(dayOfMonth):
|
2020-04-03 11:53:31 +00:00
|
|
|
events[dayOfMonth] = []
|
2020-02-23 09:49:10 +00:00
|
|
|
events[dayOfMonth].append(postEvent)
|
2020-02-22 19:11:51 +00:00
|
|
|
|
|
|
|
# if some posts have been deleted then regenerate the calendar file
|
|
|
|
if recreateEventsFile:
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(calendarFilename, 'w+') as calendarFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in calendarPostIds:
|
|
|
|
calendarFile.write(post_id + '\n')
|
2021-11-25 21:18:53 +00:00
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + calendarFilename)
|
2020-02-22 19:11:51 +00:00
|
|
|
|
|
|
|
return events
|
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def day_events_check(base_dir: str, nickname: str, domain: str,
|
|
|
|
currDate) -> bool:
|
2021-05-31 11:57:36 +00:00
|
|
|
"""Are there calendar events for the given date?
|
2020-02-22 19:11:51 +00:00
|
|
|
"""
|
2021-05-31 11:57:36 +00:00
|
|
|
year = currDate.year
|
|
|
|
monthNumber = currDate.month
|
|
|
|
dayNumber = currDate.day
|
2020-04-03 11:53:31 +00:00
|
|
|
|
|
|
|
calendarFilename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(base_dir, nickname, domain) + \
|
2020-04-03 11:53:31 +00:00
|
|
|
'/calendar/' + str(year) + '/' + str(monthNumber) + '.txt'
|
2020-02-22 19:11:51 +00:00
|
|
|
if not os.path.isfile(calendarFilename):
|
|
|
|
return False
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
eventsExist = False
|
|
|
|
with open(calendarFilename, 'r') as eventsFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in eventsFile:
|
|
|
|
post_id = post_id.replace('\n', '').replace('\r', '')
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain, post_id)
|
|
|
|
if not post_filename:
|
2020-02-23 09:49:10 +00:00
|
|
|
continue
|
|
|
|
|
2021-12-26 23:41:34 +00:00
|
|
|
post_json_object = load_json(post_filename)
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_post(post_json_object):
|
2020-02-23 09:49:10 +00:00
|
|
|
continue
|
|
|
|
|
2021-12-25 22:09:19 +00:00
|
|
|
for tag in post_json_object['object']['tag']:
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_event(tag):
|
2020-02-22 19:11:51 +00:00
|
|
|
continue
|
2020-02-24 11:10:48 +00:00
|
|
|
# this tag is an event or a place
|
2020-04-03 11:53:31 +00:00
|
|
|
if tag['type'] != 'Event':
|
2020-02-22 19:11:51 +00:00
|
|
|
continue
|
2020-02-24 11:10:48 +00:00
|
|
|
# tag is an event
|
|
|
|
if not tag.get('startTime'):
|
|
|
|
continue
|
2020-04-03 11:53:31 +00:00
|
|
|
eventTime = \
|
|
|
|
datetime.strptime(tag['startTime'],
|
2020-02-24 11:10:48 +00:00
|
|
|
"%Y-%m-%dT%H:%M:%S%z")
|
2021-05-31 10:24:48 +00:00
|
|
|
if int(eventTime.strftime("%d")) != dayNumber:
|
|
|
|
continue
|
|
|
|
if int(eventTime.strftime("%m")) != monthNumber:
|
|
|
|
continue
|
|
|
|
if int(eventTime.strftime("%Y")) != year:
|
|
|
|
continue
|
|
|
|
eventsExist = True
|
|
|
|
break
|
2020-02-22 19:11:51 +00:00
|
|
|
|
|
|
|
return eventsExist
|
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def get_this_weeks_events(base_dir: str, nickname: str, domain: str) -> {}:
|
2020-02-22 19:11:51 +00:00
|
|
|
"""Retrieves calendar events for this week
|
2020-02-24 10:55:49 +00:00
|
|
|
Returns a dictionary indexed by day number of lists containing
|
|
|
|
Event and Place activities
|
2020-02-23 09:42:09 +00:00
|
|
|
Note: currently not used but could be with a weekly calendar screen
|
2020-02-22 19:11:51 +00:00
|
|
|
"""
|
2020-04-03 11:53:31 +00:00
|
|
|
now = datetime.now()
|
2021-05-31 13:00:17 +00:00
|
|
|
endOfWeek = now + timedelta(7)
|
2020-04-03 11:53:31 +00:00
|
|
|
year = now.year
|
|
|
|
monthNumber = now.month
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarFilename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(base_dir, nickname, domain) + \
|
2020-04-03 11:53:31 +00:00
|
|
|
'/calendar/' + str(year) + '/' + str(monthNumber) + '.txt'
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
events = {}
|
2020-02-22 19:11:51 +00:00
|
|
|
if not os.path.isfile(calendarFilename):
|
|
|
|
return events
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarPostIds = []
|
|
|
|
recreateEventsFile = False
|
|
|
|
with open(calendarFilename, 'r') as eventsFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in eventsFile:
|
|
|
|
post_id = post_id.replace('\n', '').replace('\r', '')
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain, post_id)
|
|
|
|
if not post_filename:
|
2020-04-03 11:53:31 +00:00
|
|
|
recreateEventsFile = True
|
2020-02-23 09:49:10 +00:00
|
|
|
continue
|
|
|
|
|
2021-12-26 23:41:34 +00:00
|
|
|
post_json_object = load_json(post_filename)
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_post(post_json_object):
|
2020-02-23 09:49:10 +00:00
|
|
|
continue
|
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
postEvent = []
|
|
|
|
weekDayIndex = None
|
2021-12-25 22:09:19 +00:00
|
|
|
for tag in post_json_object['object']['tag']:
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_event(tag):
|
2020-02-22 19:11:51 +00:00
|
|
|
continue
|
2020-02-24 11:10:48 +00:00
|
|
|
# this tag is an event or a place
|
2020-04-03 11:53:31 +00:00
|
|
|
if tag['type'] == 'Event':
|
2020-02-23 09:49:10 +00:00
|
|
|
# tag is an event
|
|
|
|
if not tag.get('startTime'):
|
2020-02-22 19:11:51 +00:00
|
|
|
continue
|
2020-04-03 11:53:31 +00:00
|
|
|
eventTime = \
|
|
|
|
datetime.strptime(tag['startTime'],
|
2020-02-23 09:49:10 +00:00
|
|
|
"%Y-%m-%dT%H:%M:%S%z")
|
2021-05-31 13:00:17 +00:00
|
|
|
if eventTime >= now and eventTime <= endOfWeek:
|
|
|
|
weekDayIndex = (eventTime - now).days()
|
2020-02-22 19:11:51 +00:00
|
|
|
postEvent.append(tag)
|
2020-02-23 09:49:10 +00:00
|
|
|
else:
|
|
|
|
# tag is a place
|
|
|
|
postEvent.append(tag)
|
|
|
|
if postEvent and weekDayIndex:
|
2021-12-26 19:47:06 +00:00
|
|
|
calendarPostIds.append(post_id)
|
2021-05-31 13:00:17 +00:00
|
|
|
if not events.get(weekDayIndex):
|
2020-04-03 11:53:31 +00:00
|
|
|
events[weekDayIndex] = []
|
2021-05-31 13:00:17 +00:00
|
|
|
events[weekDayIndex].append(postEvent)
|
2020-02-22 19:11:51 +00:00
|
|
|
|
|
|
|
# if some posts have been deleted then regenerate the calendar file
|
|
|
|
if recreateEventsFile:
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(calendarFilename, 'w+') as calendarFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in calendarPostIds:
|
|
|
|
calendarFile.write(post_id + '\n')
|
2021-11-25 21:18:53 +00:00
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + calendarFilename)
|
2020-02-22 19:11:51 +00:00
|
|
|
|
|
|
|
return events
|
2020-02-23 09:35:52 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def get_calendar_events(base_dir: str, nickname: str, domain: str,
|
|
|
|
year: int, monthNumber: int) -> {}:
|
2020-02-23 09:35:52 +00:00
|
|
|
"""Retrieves calendar events
|
2020-02-24 10:55:49 +00:00
|
|
|
Returns a dictionary indexed by day number of lists containing
|
|
|
|
Event and Place activities
|
2020-02-23 09:35:52 +00:00
|
|
|
"""
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarFilename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(base_dir, nickname, domain) + \
|
2020-04-03 11:53:31 +00:00
|
|
|
'/calendar/' + str(year) + '/' + str(monthNumber) + '.txt'
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
events = {}
|
2020-02-23 09:35:52 +00:00
|
|
|
if not os.path.isfile(calendarFilename):
|
|
|
|
return events
|
2020-02-23 09:45:04 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarPostIds = []
|
|
|
|
recreateEventsFile = False
|
|
|
|
with open(calendarFilename, 'r') as eventsFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in eventsFile:
|
|
|
|
post_id = post_id.replace('\n', '').replace('\r', '')
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain, post_id)
|
|
|
|
if not post_filename:
|
2020-04-03 11:53:31 +00:00
|
|
|
recreateEventsFile = True
|
2020-02-23 09:42:09 +00:00
|
|
|
continue
|
2020-02-24 11:10:48 +00:00
|
|
|
|
2021-12-26 23:41:34 +00:00
|
|
|
post_json_object = load_json(post_filename)
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_post(post_json_object):
|
2020-02-23 09:42:09 +00:00
|
|
|
continue
|
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
postEvent = []
|
|
|
|
dayOfMonth = None
|
2021-12-25 22:09:19 +00:00
|
|
|
for tag in post_json_object['object']['tag']:
|
2021-12-29 21:55:09 +00:00
|
|
|
if not _is_happening_event(tag):
|
2020-02-23 09:42:09 +00:00
|
|
|
continue
|
2020-02-24 11:10:48 +00:00
|
|
|
# this tag is an event or a place
|
2020-04-03 11:53:31 +00:00
|
|
|
if tag['type'] == 'Event':
|
2020-02-23 09:42:09 +00:00
|
|
|
# tag is an event
|
|
|
|
if not tag.get('startTime'):
|
|
|
|
continue
|
2020-04-03 11:53:31 +00:00
|
|
|
eventTime = \
|
|
|
|
datetime.strptime(tag['startTime'],
|
2020-02-23 09:42:09 +00:00
|
|
|
"%Y-%m-%dT%H:%M:%S%z")
|
2020-04-03 11:53:31 +00:00
|
|
|
if int(eventTime.strftime("%Y")) == year and \
|
|
|
|
int(eventTime.strftime("%m")) == monthNumber:
|
|
|
|
dayOfMonth = str(int(eventTime.strftime("%d")))
|
2020-02-23 09:42:09 +00:00
|
|
|
postEvent.append(tag)
|
|
|
|
else:
|
|
|
|
# tag is a place
|
|
|
|
postEvent.append(tag)
|
|
|
|
|
|
|
|
if postEvent and dayOfMonth:
|
2021-12-26 19:47:06 +00:00
|
|
|
calendarPostIds.append(post_id)
|
2020-02-23 09:42:09 +00:00
|
|
|
if not events.get(dayOfMonth):
|
2020-04-03 11:53:31 +00:00
|
|
|
events[dayOfMonth] = []
|
2020-02-23 09:42:09 +00:00
|
|
|
events[dayOfMonth].append(postEvent)
|
2020-02-23 09:35:52 +00:00
|
|
|
|
|
|
|
# if some posts have been deleted then regenerate the calendar file
|
|
|
|
if recreateEventsFile:
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(calendarFilename, 'w+') as calendarFile:
|
2021-12-26 19:47:06 +00:00
|
|
|
for post_id in calendarPostIds:
|
|
|
|
calendarFile.write(post_id + '\n')
|
2021-11-25 21:18:53 +00:00
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + calendarFilename)
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2020-02-23 09:35:52 +00:00
|
|
|
return events
|
2020-02-23 13:28:27 +00:00
|
|
|
|
2020-04-03 11:53:31 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
def remove_calendar_event(base_dir: str, nickname: str, domain: str,
|
|
|
|
year: int, monthNumber: int, messageId: str) -> None:
|
2020-02-23 13:28:27 +00:00
|
|
|
"""Removes a calendar event
|
|
|
|
"""
|
2020-04-03 11:53:31 +00:00
|
|
|
calendarFilename = \
|
2021-12-26 12:02:29 +00:00
|
|
|
acct_dir(base_dir, nickname, domain) + \
|
2020-04-03 11:53:31 +00:00
|
|
|
'/calendar/' + str(year) + '/' + str(monthNumber) + '.txt'
|
2020-02-23 13:28:27 +00:00
|
|
|
if not os.path.isfile(calendarFilename):
|
|
|
|
return
|
|
|
|
if '/' in messageId:
|
2020-04-03 11:53:31 +00:00
|
|
|
messageId = messageId.replace('/', '#')
|
2020-02-23 13:28:27 +00:00
|
|
|
if messageId not in open(calendarFilename).read():
|
|
|
|
return
|
2020-04-03 11:53:31 +00:00
|
|
|
lines = None
|
2021-07-13 14:40:49 +00:00
|
|
|
with open(calendarFilename, 'r') as f:
|
2020-04-03 11:53:31 +00:00
|
|
|
lines = f.readlines()
|
2020-02-23 13:28:27 +00:00
|
|
|
if not lines:
|
|
|
|
return
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(calendarFilename, 'w+') as f:
|
|
|
|
for line in lines:
|
|
|
|
if messageId not in line:
|
|
|
|
f.write(line)
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + calendarFilename)
|