-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_names.py
executable file
·125 lines (120 loc) · 5.31 KB
/
get_names.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
#!/usr/bin/env python2.6
# -*- coding: UTF-8 -*-
#import httplib2
import urllib
import urllib2
import gzip
import StringIO
'''
i don't know why this function can NOT WORK IN OAUTH.py!!
def get_nickname(uid):
h = httplib2.Http("./.cache")
next_line_is_nickname = False
resp, cont = h.request('http://www.douban.com/people/' + uid)
if resp['status'] == '200':
cont = cont.decode('utf8')
for line in cont.splitlines():
if '<title>' in line:
next_line_is_nickname = True
elif next_line_is_nickname == True:
nickname = line
return nickname
return None
return None
'''
def get_nickname(uid):
next_line_is_nikename = False
try:
url = 'http://www.douban.com/people/' + uid
req = urllib2.Request(url, headers = {'Accept-encoding' : 'gzip'})
rec = urllib2.urlopen(req)
compressed_data = rec.read()
compressed_stream = StringIO.StringIO(compressed_data)
gzipper = gzip.GzipFile(fileobj = compressed_stream)
cont = gzipper.read().decode('utf8')
for line in cont.splitlines():
if '<title>' in line:
next_line_is_nikename = True
elif next_line_is_nikename == True:
nikename = line
return nikename
except (urllib2.HTTPError, ValueError) as e:
if hasattr(e, 'reason'):
print "<h4>{0}</h4>".format(e.reason)
if hasattr(e, 'code'):
print("<h4>Return code:",e.code,"error</h4>")
print("<h4>This username/group may not exsit</h4>")
return None
'''
def get_group_name(group_url):
h = httplib2.Http("./.cache")
resp, cont = h.request(group_url)
if resp['status'] == '200':
for line in cont.splitlines():
if '<title>' in line:
return line.split('>')[1].split('<')[0] #Group name may have WhiteSpace!
return None
return None
'''
def get_group_name(group_url):
try:
req = urllib2.Request(group_url, headers = {'Accept-encoding' : 'gzip'})
rec = urllib2.urlopen(req)
compressed_data = rec.read()
compressed_stream = StringIO.StringIO(compressed_data)
gzipper = gzip.GzipFile(fileobj = compressed_stream)
content = gzipper.read().decode('utf8')
for line in content.splitlines():
if '<title>' in line:
return line.split('>')[1].split('<')[0] #Group name may have WhiteSpace!
except (urllib2.HTTPError, ValueError) as e:
if hasattr(e, 'reason'):
print("<h4>{0}</h4>".format(e.reason))
if hasattr(e, 'code'):
print("<h4>Return code:",e.code,"error</h4>")
print("<h4>This username/group may not exsit</h4>")
return None
def get_user_icon(uid):
fh = urllib2.urlopen('http://www.douban.com/people/' + uid)
cont = fh.read().decode('utf8')
for line in cont.splitlines():
if 'douban.com/icon' in line:
icon = line.split('"')[1]
return icon
def get_nickname_and_icon(uid):
h = httplib2.Http("./.cache")
next_line_is_nickname = False
resp, cont = h.request('http://www.douban.com/people/' + uid)
if resp['status'] == '200':
cont = cont.decode('utf8')
in_info = False
for line in cont.splitlines():
if '<div id="db-usr-profile">' in line:
in_info = True
if in_info == True:
if '</div>' in line:
return None
if 'douban.com/icon' in line:
icon = line.split('"')[1]
nickname = line.split('"')[3]
return [icon, nickname]
return None
def names_test():
# get_nickname_and_icon('aka')
print "aka's nickname is"
print get_nickname('aka')
print "sunus's nickname is"
print get_nickname('sunus')
# print get_user_icon('sunus')
# print get_user_icon('3215295')
# print get_user_icon('47844141')
print "unknownsunus is nickname is"
print get_nickname('unknownsunus')
print 'http://www.douban.com/group/zhuangb/'
print get_group_name('http://www.douban.com/group/zhuangb/')
print 'http://www.douban.com/group/The-Event/'
print get_group_name('http://www.douban.com/group/The-Event/')
print 'http://www.douban.com/group/yahooks/'
print get_group_name('http://www.douban.com/group/yahooks/')
print 'http://www.douban.com/group/yaho00ks/'
print get_group_name('http://www.douban.com/group/yah00ks/')