-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtwittercrawler.py
More file actions
179 lines (148 loc) · 5.65 KB
/
twittercrawler.py
File metadata and controls
179 lines (148 loc) · 5.65 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import urllib2
import threading
import re
from xml import sax
TWITTER_URL = "https://twitter.com/users/"
NUM_THREADS = 100 # Number of threads used to download data
NUM_IDS = 1000 # number of ids to download
START_ID = 284991007
END_ID = START_ID + NUM_IDS
## {{{ http://code.activestate.com/recipes/534109/ (r8)
def xml2obj(src):
"""
A simple function to converts XML data into native Python object.
"""
non_id_char = re.compile('[^_0-9a-zA-Z]')
def _name_mangle(name):
return non_id_char.sub('_', name)
class DataNode(object):
def __init__(self):
self._attrs = {} # XML attributes and child elements
self.data = None # child text data
def __len__(self):
# treat single element as a list of 1
return 1
def __getitem__(self, key):
if isinstance(key, basestring):
return self._attrs.get(key,None)
else:
return [self][key]
def __contains__(self, name):
return self._attrs.has_key(name)
def __nonzero__(self):
return bool(self._attrs or self.data)
def __getattr__(self, name):
if name.startswith('__'):
# need to do this for Python special methods???
raise AttributeError(name)
return self._attrs.get(name,None)
def _add_xml_attr(self, name, value):
if name in self._attrs:
# multiple attribute of the same name are represented by a list
children = self._attrs[name]
if not isinstance(children, list):
children = [children]
self._attrs[name] = children
children.append(value)
else:
self._attrs[name] = value
def __str__(self):
return self.data or ''
def __repr__(self):
items = sorted(self._attrs.items())
if self.data:
items.append(('data', self.data))
return u'{%s}' % ', '.join([u'%s:%s' % (k,repr(v)) for k,v in items])
# JT: added "new" methods
def keys(self): return self._attrs.keys()
def iteritems(self): return self._attrs.iteritems()
def popitem(self): return self._attrs.popitem()
def pop(self, k, d=None): return self._attrs.pop(k, d)
def items(self): return self._attrs.items()
class TreeBuilder(sax.ContentHandler):
def __init__(self):
self.stack = []
self.root = DataNode()
self.current = self.root
self.text_parts = []
def startElement(self, name, attrs):
self.stack.append((self.current, self.text_parts))
self.current = DataNode()
self.text_parts = []
# xml attributes --> python attributes
for k, v in attrs.items():
self.current._add_xml_attr(_name_mangle(k), v)
def endElement(self, name):
text = ''.join(self.text_parts).strip()
if text:
self.current.data = text
if self.current._attrs:
obj = self.current
else:
# a text only node is simply represented by the string
obj = text or ''
self.current, self.text_parts = self.stack.pop()
self.current._add_xml_attr(_name_mangle(name), obj)
def characters(self, content):
self.text_parts.append(content)
builder = TreeBuilder()
if isinstance(src,basestring):
sax.parseString(src, builder)
else:
sax.parse(src, builder)
return builder.root._attrs.values()[0]
## end of http://code.activestate.com/recipes/534109/ }}}
def get_user(content):
user = xml2obj(content)
return user
def split_list(alist, wanted_parts=1):
length = len(alist)
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
def process_lines(lines, num):
global counter
global output_file
global total
global output_data
opener = urllib2.build_opener()
#lock = threading.RLock()
for user_id in lines:
#request_url = TWITTER_URL + user_id + ".json"
request_url = TWITTER_URL + user_id
#authenticated_url = get_authenticated_url(request_url)
try:
response = opener.open(request_url)
user = get_user(response.read())
except Exception, e:
print str(e)
continue
output_line = '"%s";"%s"\n' % (user_id, user['screen_name'])
#lock.acquire()
output_file.write(output_line)
#output_data.append(output_line) # TODO: save to DB
counter += 1
#lock.release()
def main():
global counter
global output_file
global total
global output_data
output_data = []
output_file = open('twitter_threaded.csv', 'w')
pool = [] # threads pool
ids = [str(i) for i in range(START_ID, END_ID)]
total = len(ids)
counter = 0
thread_num = 0
for sub_list in split_list(ids, NUM_THREADS):
thread = threading.Thread(target=process_lines, args=(sub_list, thread_num))
thread.start()
pool.append(thread)
thread_num += 1
for thread in pool:
thread.join()
#output_file.writelines(output_data)
output_file.close()
print "FINISH: %s, %s" % (total, counter)
if __name__ == '__main__':
main()