-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_tweets_cities.py
42 lines (31 loc) · 1.21 KB
/
get_tweets_cities.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
# New England Mad: get tweets by New England cities
# Author: Heikal Badrulhisham <[email protected]>
# Year: 2018
"""
Load tweets from New England cities through a browser and save them in a CSV file
"""
import csv
import get_tweets
import os
if __name__ == '__main__':
# Get a list of cities
cities_csv = csv.reader(open('neweng_cities.csv'))
cities = [(r[0], r[1]) for r in cities_csv]
# Folder for saving data files
save_dir = 'cities_tweets'
if not os.path.exists(save_dir):
os.mkdir(save_dir)
# URL template for Twitter search page
url_template = 'https://twitter.com/search?l=en&q=mad%20near%3A%22{0}%22%20within%3A15mi&src=typd'
# Get tweets by city
for city in cities:
# Stem of the data file name
file_name_stem = '{0}_{1}'.format(city[0], city[1])
# Skip if tweets have been collected for the city
if os.path.exists('{0}/{1}.csv'.format(save_dir, file_name_stem)):
continue
# Location term for searching (City + State abbreviation)
location = '{0}, {1}'.format(city[0], city[1])
curr_url = url_template.format(location)
get_tweets.get_tweets(curr_url, save_dir, file_name_stem, 2, 2)
exit(0)