-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscrape_cities.py
59 lines (47 loc) · 1.9 KB
/
scrape_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
from datetime import date, timedelta
import twint_to_db
import sys # for command line argument parsing
import numpy as np # for creation of a an array of ones
# a function to construct filenames for dataframe storage
def construct_filename(row):
filename = './'+row['location_name']+'_'+row['searchterm']+'.pkl'
return(filename)
def main(test=False):
yesterday = date.today() - timedelta(days=1)
yesterday = yesterday.strftime('%Y-%m-%d')
# print(yesterday)
dataframe_of_cities = pd.DataFrame({
'location_name':['dublin','london','newyork'],
'searchterm':['nuclear','nuclear','nuclear'],
'geo' : ['53.349538,-6.260678,10km','51.516479,-0.096949,10km','40.751177,-73.994641,10km'],
'since':[yesterday,yesterday,yesterday],
'until':[False,False,False],
'limit':[0,0,0]
},index=[0,1,2])
if test:
n=len(dataframe_of_cities)
dataframe_of_cities.loc[:,'limit'] = 20*np.ones(n).astype(int)
# create new dataframe column based on other columns using a lambda function:
# https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns
dataframe_of_cities['filepath'] = dataframe_of_cities.apply(lambda row: construct_filename(row), axis=1)
print(dataframe_of_cities)
# iterate through rows of the dataframe to call the scraper
for row in dataframe_of_cities.itertuples(index=True, name='Pandas'):
twint_to_db.main(
searchterm = getattr(row,"searchterm"),
geo = getattr(row,"geo"),
since = getattr(row,"since"),
until = getattr(row,"until"),
limit = getattr(row,"limit"),
filepath = getattr(row,"filepath")
)
if __name__ == '__main__':
if len(sys.argv)==2 and str(sys.argv[1])=='--help':
print("To scrape a limit of 20 tweets per line run: ")
print("python3 scrape_cities.py --test")
elif len(sys.argv)==2 and str(sys.argv[1]) == '--test':
print("Scraping 20 tweets per city as a test.")
main(test=True)
else:
main()