-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc2c.py
More file actions
63 lines (51 loc) · 1.56 KB
/
Copy pathc2c.py
File metadata and controls
63 lines (51 loc) · 1.56 KB
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
# -*- coding: utf-8 -*-
#
# A ical file generator based on connpass events.
#
# Preparations:
# $ pip install icalendar dateutil
# Usage:
# $ python c2c.py <keywords> > <filename>
# Example:
# $ python c2c.py php,ruby,python > llstudy.ics
#
from dateutil import parser
from icalendar import Calendar, Event
import os
import requests
import sys
import tempfile
# Max results = 100 is server side upper limit.
CONNPASS_SEARCH_API = 'https://connpass.com/api/v1/event/?count=100&keyword_or='
TMPFILE = 'temp.ics'
PRODID = '-//syucream connpass calendar//connpass2cal//'
def _get_connpass_events(keyword):
res = requests.get(CONNPASS_SEARCH_API + keyword)
return res.json()['events']
def _generate_ical(events):
ical = Calendar()
ical.add('version', '2.0')
ical.add('prodid', PRODID)
for e in events:
iev = Event()
iev.add('summary', e['title'])
iev.add('description', 'Event URL: ' + e['event_url'] + '\n\n' + e['description'])
iev.add('dtstart', parser.parse(e['started_at']))
iev.add('dtend', parser.parse(e['ended_at']))
ical.add_component(iev)
return ical.to_ical()
def _dump_via_tmpfile(ical):
directory = tempfile.mkdtemp()
wf = open(os.path.join(directory, TMPFILE), 'wb')
wf.write(ical)
wf.close()
rf = open(os.path.join(directory, TMPFILE), 'r')
data = rf.read()
rf.close()
return data
if __name__ == '__main__':
keywords = sys.argv[1]
events = _get_connpass_events(keywords)
ical = _generate_ical(events)
out = _dump_via_tmpfile(ical)
print(out)