-
Notifications
You must be signed in to change notification settings - Fork 6
/
json2csv.py
executable file
·137 lines (123 loc) · 3.27 KB
/
json2csv.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
126
127
128
129
130
131
132
133
134
135
136
137
import csv
import sys
import json
import fileinput
def main():
sheet = csv.writer(sys.stdout, encoding="utf-8")
sheet.writerow(get_headings())
for line in fileinput.input():
tweet = json.loads(line)
sheet.writerow(get_row(tweet))
def get_headings():
return [
'coordinates',
'created_at',
'hashtags',
'media',
'urls',
'favorite_count',
'id',
'in_reply_to_screen_name',
'in_reply_to_status_id',
'in_reply_to_user_id',
'lang',
'place',
'possibly_sensitive',
'retweet_count',
'reweet_id',
'retweet_screen_name',
'source',
'text',
'tweet_url',
'user_created_at',
'user_screen_name',
'user_default_profile_image',
'user_description',
'user_favourites_count',
'user_followers_count',
'user_friends_count',
'user_listed_count',
'user_location',
'user_name',
'user_screen_name',
'user_statuses_count',
'user_time_zone',
'user_urls',
'user_verified',
]
def get_row(t):
get = t.get
user = t.get('user').get
row = [
coordinates(t),
get('created_at'),
hashtags(t),
media(t),
urls(t),
get('favorite_count'),
get('id_str'),
get('in_reply_to_screen_name'),
get('in_reply_to_status_id'),
get('in_reply_to_user_id'),
get('lang'),
place(t),
get('possibly_sensitive'),
get('retweet_count'),
retweet_id(t),
retweet_screen_name(t),
get('source'),
get('text'),
tweet_url(t),
user('created_at'),
user('screen_name'),
user('default_profile_image'),
user('description'),
user('favourites_count'),
user('followers_count'),
user('friends_count'),
user('listed_count'),
user('location'),
user('name'),
user('screen_name'),
user('statuses_count'),
user('time_zone'),
user_urls(t),
user('verified'),
]
return row
def coordinates(t):
if 'coordinates' in t and t['coordinates']:
return '%f %f' % tuple(t['coordinates']['coordinates'])
return None
def hashtags(t):
return ' '.join([h['text'] for h in t['entities']['hashtags']])
def media(t):
if 'media' in t['entities']:
return ' '.join([h['expanded_url'] for h in t['entities']['media']])
else:
return None
def urls(t):
return ' '.join([h['expanded_url'] for h in t['entities']['urls']])
def place(t):
if t['place']:
return t['place']['full_name']
def retweet_id(t):
if 'retweeted_status' in t and t['retweeted_status']:
return t['retweeted_status']['id_str']
def retweet_screen_name(t):
if 'retweeted_status' in t and t['retweeted_status']:
return t['retweeted_status']['user']['screen_name']
def tweet_url(t):
return "https://twitter.com/%s/status/%s" % (t['user']['screen_name'], t['id_str'])
def user_urls(t):
u = t.get('user')
if not u:
return None
urls = []
if 'entities' in u and 'url' in u['entities'] and 'urls' in u['entities']['url']:
for url in u['entities']['url']['urls']:
if url['expanded_url']:
urls.append(url['expanded_url'])
return ' '.join(urls)
if __name__ == "__main__":
main()