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
|
import json
import os
import re
import time
import urllib2
import urlparse
from rcal.db import Session
from rcal.model import Calendar, Category
CONFIG = json.load(open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'conf',
'reddit-import.json'),
'r'))
def main():
cal_list = None
if not os.path.exists(CONFIG['cache']) or \
int(time.time()) - int(os.path.getmtime(CONFIG['cache'])) > \
CONFIG['cache_time']:
opener = urllib2.build_opener()
opener.addheaders = [('User-Agent', CONFIG['user_agent'])]
cal_list = json.loads(opener.open(CONFIG['reddit_url']).read())
cal_list = cal_list[0]['data']['children'][0]['data']['selftext']
with open(CONFIG['cache'], 'w') as cache_file:
cache_file.write(cal_list)
cache_file.close()
else:
cal_list = open(CONFIG['cache'], 'r').read()
session = Session.create()
ical_markdown = re.compile(r'^\[iCal\]\((.*)\)$')
cells = [row.split('|') for row in cal_list.split('\n')]
for row in cells:
if len(row) == 7:
row = [r for r in row if r]
if len(row) == 5:
markdown_match = re.match(ical_markdown, row[2])
if markdown_match:
ical_url = urlparse.urlparse(markdown_match.group(1))
if ical_url.netloc == 'calendar.google.com':
ical_path = re.sub(
'^/?calendar/ical/', '', ical_url.path).split('/')
if len(ical_path) == 3:
calendar_uid = ical_path[0]
calendar = Calendar.fetch(
calendar_uid,
session,
row[0],
ical_url.geturl())
calendar.website = row[4]
calendar.category = Category.fetch(row[1], session)
else:
print ical_url.geturl()
else:
print ical_url.geturl()
session.commit()
if __name__ == '__main__':
main()
|