-
Notifications
You must be signed in to change notification settings - Fork 103
/
renren.py
162 lines (131 loc) · 5.02 KB
/
renren.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#-*-coding:utf-8-*-
import requests
import json
import re
import random
from urlparse import urlparse, parse_qsl
from pyquery import PyQuery
from ntype import NTYPES
from encrypt import encryptString
class RenRen:
def __init__(self, email=None, pwd=None):
self.session = requests.Session()
self.token = {}
if email and pwd:
self.login(email, pwd)
def loginByCookie(self, cookie_path):
with open(cookie_path) as fp:
cookie_str = fp.read()
cookie_dict = dict([v.split('=', 1) for v in cookie_str.strip().split(';')])
self.session.cookies = requests.utils.cookiejar_from_dict(cookie_dict)
self.getToken()
def saveCookie(self, cookie_path):
with open(cookie_path, 'w') as fp:
cookie_dict = requests.utils.dict_from_cookiejar(self.session.cookies)
cookie_str = '; '.join([k + '=' + v for k, v in cookie_dict.iteritems()])
fp.write(cookie_str)
def login(self, email, pwd):
key = self.getEncryptKey()
data = {
'email': email,
'origURL': 'http://www.renren.com/home',
'icode': '',
'domain': 'renren.com',
'key_id': 1,
'captcha_type': 'web_login',
'password': encryptString(key['e'], key['n'], pwd) if key['isEncrypt'] else pwd,
'rkey': key['rkey']
}
url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=%f' % random.random()
r = self.post(url, data)
result = r.json()
if result['code']:
print 'login successfully'
self.email = email
r = self.get(result['homeUrl'])
self.getToken(r.text)
else:
print 'login error', r.text
def getEncryptKey(self):
r = requests.get('http://login.renren.com/ajax/getEncryptKey')
return r.json()
def getToken(self, html=''):
p = re.compile("get_check:'(.*)',get_check_x:'(.*)',env")
if not html:
r = self.get('http://www.renren.com')
html = r.text
result = p.search(html)
self.token = {
'requestToken': result.group(1),
'_rtk': result.group(2)
}
def request(self, url, method, data={}):
if data:
data.update(self.token)
if method == 'get':
return self.session.get(url, data=data)
elif method == 'post':
return self.session.post(url, data=data)
def get(self, url, data={}):
return self.request(url, 'get', data)
def post(self, url, data={}):
return self.request(url, 'post', data)
def getUserInfo(self):
r = self.get('http://notify.renren.com/wpi/getonlinecount.do')
return r.json()
def getNotifications(self):
url = 'http://notify.renren.com/rmessage/get?getbybigtype=1&bigtype=1&limit=999&begin=0&view=16'
r = self.get(url)
r.raise_for_status()
return r.json()
def getDoings(self, uid, page=0):
url = 'http://status.renren.com/GetSomeomeDoingList.do?userId=%s&curpage=%d' % (str(uid), page)
r = self.get(url)
return r.json()['doingArray']
def getDoingById(self, owner_id, doing_id):
doings = self.getDoings(owner_id)
doing = filter(lambda doing: doing['id'] == doing_id, doings)
return doing[0] if doing else None
def getDoingComments(self, owner_id, doing_id):
url = 'http://status.renren.com/feedcommentretrieve.do'
r = self.post(url, {
'doingId': doing_id,
'source': doing_id,
'owner': owner_id,
't': 3
})
return r.json()['replyList']
def getCommentById(self, owner_id, doing_id, comment_id):
comments = self.getDoingComments(owner_id, doing_id)
comment = filter(lambda comment: comment['id'] == comment_id, comments)
return comment[0] if comment else None
def addComment(self, data):
url = 'http://status.renren.com/feedcommentreply.do'
#url = 'http://page.renren.com/doing/reply'
payloads = {
't': 3,
'rpLayer': 0,
'source': data['doing_id'],
'owner': data['owner_id'],
'c': data['message']
}
if data.get('reply_id', None):
payloads.update({
'rpLayer': 1,
'replyTo': data['author_id'],
'replyName': data['author_name'],
'secondaryReplyId': data['reply_id'],
'c': '回复%s:%s' % (data['author_name'].encode('utf-8'), data['message'])
})
print self.email, 'going to send a comment: ', payloads['c']
r = self.post(url, payloads)
r.raise_for_status()
print 'comment sent', r.json()
return r.json()
if __name__ == '__main__':
renren = RenRen()
renren.login('email', 'password')
#renren.loginByCookie('cookie.txt')
info = renren.getUserInfo()
print 'hello', info['hostname']
print renren.getNotifications()