-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_notifications.py
45 lines (41 loc) · 1.37 KB
/
fetch_notifications.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
# -*- coding: utf-8 -*-
import sys
from plistlib import load
import requests
import unicodedata
import re
import json
try:
reply = sys.argv[1]
except:
reply = ""
info = load(open('info.plist', 'rb'))
access = info['variables']['access_key']
instance = info['variables']['instance']
head = {'Authorization': 'Bearer '+access}
notis = json.loads(requests.get(
instance+'/api/v1/notifications', headers=head).content.decode('utf-8'))
def strip(t):
t = re.sub('</p><p>', '\n', t)
t = re.sub('(<.?p>|<.?a.*?>|<.?span.*?>)', '', t)
t = re.sub('<', '<', t)
t = re.sub('>', '>', t)
t = re.sub(''', '\'', t)
t = re.sub('"', '\'', t)
t = re.sub('<br.*?\/?>', '\n', t)
return t
items = list()
for noti in notis:
if noti["type"] == 'mention':
item = dict()
item["title"] = strip(noti['status']['content'])
item["subtitle"] = "from: "+str(noti['account']['acct'])
if noti['status']['mentions']:
mention = ['@'+i['acct'] for i in noti['status']['mentions']]
mention.append('@'+str(noti['account']['acct']))
else:
mention = []
item["arg"] = '{"status":"'+str(reply)+'","in_reply_to_id":"'+str(noti['status']['id'])+'","acct":"'+' '.join(mention)+'"}' # list 어떻게 넘겨야 되지
items.append(item)
results = {"items": items}
print(json.dumps(results))