-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterDownloader_National.py
More file actions
55 lines (42 loc) · 2.01 KB
/
Copy pathTwitterDownloader_National.py
File metadata and controls
55 lines (42 loc) · 2.01 KB
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
import sys
import tweepy
import pandas as pd
import csv
#import os
from time import gmtime, strftime
current_time=strftime("%Y-%m-%d %Hh%Mm", gmtime())
#os.environ['PYTHONIOENCODING'] = 'utf-8' #setting the sys environment, or we will get unicoden error
TwitterData=pd.DataFrame(columns=['User','Coordinates','Date','Tweet']) # create empty dataframe
consumer_key=" "
consumer_secret=" "
access_key=" "
access_secret=" "
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('Twitter_National_%s.csv'%current_time, 'wb')
#Use csv Writer
csvWriter = csv.writer(csvFile)
csvWriter.writerow(['User','Latitude','Longitude','Date','Tweet']) #entity
keywords=["corn","disease","@corndisease"]
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
#if exact_word_match("corn", status.text):
if any(keyword in status.text.lower() for keyword in keywords):
if status.geo is not None:
csvWriter.writerow([status.user.id,status.geo['coordinates'][0],status.geo['coordinates'][1],status.created_at,status.text.encode('utf-8')]) #writing and arranging all information in csv
print "National ",status.created_at,status.text
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
#try:
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
# IL IN OH
sapi.filter(locations=[-123.19,25.79,-66.06,47.63]) # Each bounding box should be specified as a pair of longitude and latitude pairs, with the southwest corner of the bounding box coming first
#except (UnicodeEncodeError, UnicodeDecodeError):
# print "*****************Found unicode error***************"
#csvFile.close()