-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_stats.py
executable file
·75 lines (60 loc) · 2.32 KB
/
tweet_stats.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
#! /usr/bin/python
__author__ = '[email protected]'
# Patrick Trinkle
# Summer 2011
#
# This builds a massive table of information, including how many users have geo
# tweets. how many tweets have been collected. etc.
#
import re
import os
import sys
import codecs
sys.path.append("tweetlib")
import tweetdatabase as td
def usage():
print "usage: %s <database_folder> <output_file>" % sys.argv[0]
def main():
# Did they provide the correct args?
if len(sys.argv) != 3:
usage()
sys.exit(-1)
# --------------------------------------------------------------------------
# Parse the parameters.
database_folder = sys.argv[1]
output = sys.argv[2]
print "database folder: %s" % database_folder
# collecting certain information can be done more rapidly with grep -rn/wc -l.
stats = {
'user_count' : 0, # number of users
'user_geo_count' : 0, # number of users with geo tags
'user_twt_count' : 0, # number of users with tweets (collected)
'user_frd_count' : 0 # number of users with friends (identified)
}
# --------------------------------------------------------------------------
# Search the database file for certain things.
for input_file in td.fileWalk(database_folder):
#print "processing: %s, len(since_ids): %d" % (input_file, len(since_ids))
with codecs.open(input_file, "r", 'utf-8') as f:
ux = f.read()
usr = re.search("\t<id>(\d+?)</id>\n", ux)
if usr:
stats['user_count'] += 1
if "<geo>" in ux:
stats['user_geo_count'] += 1
if "<tweets>" in ux:
stats['user_twt_count'] += 1
if "<friends>" in ux:
stats['user_frd_count'] += 1
else:
sys.stderr.write("invalid user file: %s" % input_file)
sys.exit(-1)
# --------------------------------------------------------------------------
# Output results.
with open(output, "w") as f:
for x in stats:
f.write("%s:%d\n" % (x, stats[x]))
# --------------------------------------------------------------------------
# Done.
if __name__ == "__main__":
main()