forked from pablobarbera/pytwools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-tweets.py
76 lines (61 loc) · 1.75 KB
/
print-tweets.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
'''
print-tweets.py
Prints basic information about tweets in console
@p_barbera
Usage:
## prints text of first 10 tweets
python print-tweets.py -o text -f tweets.json -k 10
## prints coordinates and text of first 10 geolocated tweets
python print-tweets.py -o geo -f tweets.json -k 10
'''
import sys
import json
import argparse
# arguments
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', required=True,
help = 'name of file with tweets in json format')
parser.add_argument('-o', '--output', default='text',
help = 'type of output to be printed in console', choices=['text', 'geo'])
parser.add_argument('-k', '--count', default=20, type=int,
help = 'number of results to display in console')
args = parser.parse_args()
output = args.output
tweetfile = args.file
k = args.count
def print_text(tweetfile, k):
i = 1
fh = open(tweetfile,'r')
for line in fh:
try:
tweet = json.loads(line)
except:
continue
if 'text' not in tweet:
continue
print '[' + tweet['user']['screen_name'] + ']: ' + tweet['text']
i += 1
if i == k:
break
def print_geo(tweetfile, k):
i = 1
fh = open(tweetfile,'r')
for line in fh:
try:
tweet = json.loads(line)
except:
continue
try:
coord = tweet['geo']['coordinates']
except:
continue
print '[' + str(coord[0]) + ',' + str(coord[1]) + ']: ' + tweet['text']
i += 1
if i == k:
break
if output == 'text':
print_text(tweetfile, k)
if output == 'geo':
print_geo(tweetfile, k)
if output != 'text' and output != 'geo':
print "Error! Only 'text' or 'geo' options are allowed."