-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
93 lines (76 loc) · 2.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf8 -*-
import os
import sys
sys.path.insert(1, 'app/')
from datetime import datetime, timedelta
from Git.Client import GitClient
from Meetup.Client import MeetupClient
from Meetup.Filter import filter_event
from Meetup.Event import MeetupEvent
from File.DB import FileDB
from File.Writer import FileWriter
class App:
def __init__(self, CONSTANTS):
for name in CONSTANTS:
setattr(self, name, CONSTANTS[name])
self.meetup = MeetupClient(self.MEETUP_GROUP_NAME)
self.git = GitClient(self.REPO_PATH, self.REPO_AUTHOR_NAME,
self.REPO_AUTHOR_EMAIL)
self.writer = FileWriter(self.EVENT_POSTS_DIR)
self.db = FileDB(self.EVENT_POSTS_DIR)
@property
def time_to_search_to(self):
# Returning 1 month from now
return datetime.now() + timedelta(days=30)
def sync_event_file(self, event):
renamed = False
file_info = self.db.find_event(event['id'])
if file_info:
if file_info['title'] != event['title']:
renamed = True
(file_date, title) = FileWriter.split_filename(
file_info['filename'])
filename = FileWriter.create_filename(file_date, event['title'])
else:
filename = file_info['filename']
else:
filename = FileWriter.create_filename(event['time'], event['title'])
if renamed:
self.writer.delete(file_info['filename'])
self.writer.write(MeetupEvent(event), filename)
def sync_git(self):
if self.git.status:
self.git.stage_all()
self.git.commit()
try:
self.git.push()
return True
except Exception as e:
self.git.remove_head_commit()
self.git.pull()
raise e
else:
return False
def poll_and_update(self):
events = self.meetup.events
if events:
self.git.reset_hard()
self.git.pull()
for event in events:
event = self.meetup.parse_event(event)
if filter_event(event, self.time_to_search_to):
self.sync_event_file(event)
self.sync_git()
def CONSTANTS():
CONSTANTS = {
'REPO_PATH': os.path.join(os.getcwd(), 'opentwincities.github.com/'),
'REPO_AUTHOR_NAME': os.environ['SITE_BOT_REPO_AUTHOR_NAME'],
'REPO_AUTHOR_EMAIL': os.environ['SITE_BOT_REPO_AUTHOR_EMAIL'],
'MEETUP_GROUP_NAME': os.environ['SITE_BOT_MEETUP_GROUP_NAME']
}
CONSTANTS['EVENT_POSTS_DIR'] = os.path.join(
CONSTANTS['REPO_PATH'], 'events', '_posts')
return CONSTANTS
if __name__ == "__main__":
app = App(CONSTANTS())
app.poll_and_update()