forked from ptwobrussell/Recipes-for-Mining-Twitter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
recipe__get_user_info.py
82 lines (52 loc) · 2.24 KB
/
recipe__get_user_info.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
# -*- coding: utf-8 -*-
from recipe__oauth_login import oauth_login
from recipe__make_twitter_request import make_twitter_request
# Assume ids have been fetched from a scenario such as the
# one presented in recipe__get_friends_followers.py and that
# t is an authenticated instance of twitter.Twitter
def get_info_by_id(t, ids):
id_to_info = {}
while len(ids) > 0:
# Process 100 ids at a time...
ids_str = ','.join([str(_id) for _id in ids[:100]])
ids = ids[100:]
response = make_twitter_request(t,
getattr(getattr(t, "users"), "lookup"),
user_id=ids_str)
if response is None:
break
if type(response) is dict: # Handle Twitter API quirk
response = [response]
for user_info in response:
id_to_info[user_info['id']] = user_info
return id_to_info
# Similarly, you could resolve the same information by screen name
# using code that's virtually identical. These two functions
# could easily be combined.
def get_info_by_screen_name(t, screen_names):
sn_to_info = {}
while len(screen_names) > 0:
# Process 100 ids at a time...
screen_names_str = ','.join([str(sn) for sn in screen_names[:100]])
screen_names = screen_names[100:]
response = make_twitter_request(t,
getattr(getattr(t, "users"), "lookup"),
screen_name=screen_names_str)
if response is None:
break
if type(response) is dict: # Handle Twitter API quirk
response = [response]
for user_info in response:
sn_to_info[user_info['screen_name']] = user_info
return sn_to_info
if __name__ == '__main__':
# Be sure to pass in any necessary keyword parameters
# if you don't have a token already stored on file
t = oauth_login()
# Basic usage...
info = {}
info.update(get_info_by_screen_name(t, ['ptwobrussell', 'socialwebmining']))
info.update(get_info_by_id(t, ['2384071']))
# Do something useful with the profile information like store it to disk
import json
print json.dumps(info, indent=1)