-
Notifications
You must be signed in to change notification settings - Fork 0
/
studip-news.py
executable file
·87 lines (64 loc) · 2.63 KB
/
studip-news.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
#!/usr/bin/env python
import re
from bs4 import BeautifulSoup
from config import Config
from feedgen.feed import FeedGenerator
from datetime import datetime, timezone
from studip_sync.config import CONFIG as STUDIP_SYNC_CONFIG
from studip_sync.logins import LoginError
from studip_sync.session import Session, SessionError
from studip_sync.parsers import ParserError
PAYLOAD = {'filtertype': Config.SELECTED_FILTERS}
USER_ID_REGEX = re.compile('STUDIP.ActivityFeed.user_id = \'(.+?)\';')
START_PAGE_URL = STUDIP_SYNC_CONFIG.base_url + "dispatch.php/start"
ACTIVITY_STREAM_URL = STUDIP_SYNC_CONFIG.base_url + "api.php/user/{0}/activitystream"
def main():
session = create_session()
activities = fetch_activity_stream(session)
generate_feed(activities)
def create_session():
with Session(base_url=STUDIP_SYNC_CONFIG.base_url) as studip_sync_session:
print("Logging in...")
try:
studip_sync_session.login(STUDIP_SYNC_CONFIG.auth_type, STUDIP_SYNC_CONFIG.auth_type_data,
STUDIP_SYNC_CONFIG.username,
STUDIP_SYNC_CONFIG.password)
except (LoginError, ParserError) as e:
print("Login failed!")
print(e)
return 1
return studip_sync_session.session
def extract_user_id(session):
if Config.USER_ID:
return Config.USER_ID
with session.get(START_PAGE_URL) as response:
if not response.ok:
raise SessionError("Could not open page for user id extraction!")
match = USER_ID_REGEX.search(response.text)
if match:
return match.group(1)
else:
raise ParserError("Could not extract userid!")
def generate_feed(json):
print("Generating feed...")
fg = FeedGenerator()
fg.id(STUDIP_SYNC_CONFIG.base_url)
fg.title('STUD.IP Activities')
for entry in json:
fe = fg.add_entry()
fe.id(entry['id'])
fe.title(entry['title'])
fe.content(BeautifulSoup(entry['content']).get_text())
fe.link({'href': STUDIP_SYNC_CONFIG.base_url + list(entry['object_url'].keys())[0]})
fe.published(datetime.fromtimestamp(int(entry['mkdate']), timezone.utc))
fg.atom_file("/dev/stdout", pretty=True)
def generate_user_id_url(session):
return ACTIVITY_STREAM_URL.format(extract_user_id(session))
def fetch_activity_stream(session):
print("Fetching activity stream...")
with session.get(generate_user_id_url(session), params=PAYLOAD) as response:
if not response.ok:
raise SessionError("Could not fetch activities!")
return response.json()
if __name__ == '__main__':
main()