-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplurk.py
executable file
·147 lines (128 loc) · 5.91 KB
/
plurk.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import urllib, urllib2, cookielib
from datetime import datetime
import pynotify
import json, os, time, sys
from io import StringIO
api_key = 'vB8TYzK9lyDFfHvCjSf0RlF9KBYAUTaL'
username_and_password = 'password.dat'
class Plurk:
def __init__(self):
self.currentpath=self.get_current_path()
self.login_state = False
self.api_key = api_key
self.get_api_url = lambda x: 'http://www.plurk.com/API%s' % x
self.encode = urllib.urlencode
self.offset = None
self.unReadCount = 0
self.friend_name = {}
self.responses = {}
self.friend_name_unread = {}
self.friend_pic = {}
self.unReadPlurks = json.load(StringIO('{"plurks":[]}'))
pynotify.init("plurk")
self.load_login_data()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
def get_current_path(self):
pathname = os.path.abspath(os.path.dirname(sys.argv[0]))+'/'
return pathname
def load_login_data(self):
file_line = open(self.currentpath+username_and_password,"r").readlines()
self.username = file_line[0].strip()
self.password = file_line[1].strip()
def login(self):
print 'start login...'
result=self.opener.open(self.get_api_url('/Users/#'),
self.encode({'username': self.username,
'password': self.password,
'api_key': self.api_key,
'no_data': '1'}))
if json.load(result)['success_text'] == 'ok':
print 'login success'
return True
else:
print 'login failed'
return False
def get_recent_plurks(self):
plurks = self.opener.open(self.get_api_url('/Polling/getPlurks'),
self.encode({'api_key': self.api_key,
'offset': self.offset,
'limit' : 20}))
return json.load(plurks)
def get_unread_plurks(self):
unreadplurks = self.opener.open(self.get_api_url('/Timeline/getUnreadPlurks'),
self.encode({'api_key': self.api_key
}))
return json.load(unreadplurks)
def mark_all_as_read(self):
plurks = self.get_unread_plurks()
for plurk_id in plurks['plurks']:
print 'not yet'
def get_unread_count(self):
unread = self.opener.open(self.get_api_url('/Polling/getUnreadCount'),
self.encode({'api_key': self.api_key}))
self.unReadCount = json.load(unread)['all']
def set_offset(self):
self.offset = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
def download_avatar(self,url,uid, avatar_version):
if not(os.path.exists(self.currentpath+'avatar')):
os.mkdir(self.currentpath+'avatar')
fileurl = self.currentpath+'avatar/'+str(uid)+'-'+str(avatar_version)+'.gif'
if not(os.path.isfile(fileurl)):
urllib.urlretrieve(url, fileurl )
return fileurl
def get_avatar(self, uid, plurk_user):
if plurk_user['has_profile_image'] == 1:
if plurk_user['avatar'] == 0: #0 or None?
return self.download_avatar('http://avatars.plurk.com/%s-medium.gif' % uid, uid, 'none')
else:
return self.download_avatar('http://avatars.plurk.com/%s-medium%s.gif' % (uid, plurk_user['avatar']), uid, plurk_user['avatar'])
else:
return self.download_avatar('http://www.plurk.com/static/default_medium.gif', 'default', 'medium')
def get_name(self, plurk_user):
if 'display_name' in plurk_user:
return plurk_user['display_name']
else:
return plurk_user['nick_name']
def get_qualifier(self, plurk):
if 'qualifier_translated' in plurk:
return plurk['qualifier_translated']
else:
return plurk['qualifier']
def parse_plurk_data(self, plurk_data, unread):
if unread == False:
for uid in plurk_data['plurk_users']:
self.friend_name[uid] = self.get_name(plurk_data['plurk_users'][uid])
self.friend_pic[uid] = self.get_avatar(uid, plurk_data['plurk_users'][uid])
else:
for uid in plurk_data['plurk_users']:
self.friend_name_unread[uid] = self.get_name(plurk_data['plurk_users'][uid])
def get_responses(self, plurkID):
responces = self.opener.open(self.get_api_url('/Responses/get'),
self.encode({'api_key': self.api_key,
'plurk_id': plurkID,
'limit' : 20}))
return json.load(responces)
def notify_header(self, plurk, unread):
if unread == False:
return "%s %s" % (self.friend_name[str(plurk['owner_id'])],
self.get_qualifier(plurk))
else:
return "%s %s" % (self.friend_name_unread[str(plurk['owner_id'])],
self.get_qualifier(plurk))
def notify_plurks(self, plurk_data):
for p in plurk_data['plurks']:
pynotify.Notification(self.notify_header(p,False),
str(p['content']),
self.friend_pic[str(p['owner_id'])]).show()
def run(self):
self.login_state = self.login()
if self.login_state == True:
self.get_unread_count()
plurks = self.get_recent_plurks()
self.unReadPlurks = self.get_unread_plurks()
self.parse_plurk_data(plurks,False)
self.parse_plurk_data(self.unReadPlurks,True)
self.notify_plurks(plurks)
self.set_offset()
else:
print 'login error'