This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Data_Extraction.py
49 lines (40 loc) · 1.49 KB
/
Data_Extraction.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
import tweepy
from textblob import TextBlob
import csv
import time
import os
consumer_key = ''
consumer_secret=''
access_token=''
access_token_secret=''
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api = tweepy.API(auth)
try:
os.remove('result.csv')
#print "File deleted"
except:
#print "File was not present already"
pass
csvFile = open('result.csv', 'w')
#Use csv writer
csvWriter = csv.writer(csvFile)
#Query(q) ---> AND (surge OR crash OR plunge OR high OR low OR future OR amazing OR good OR bad OR record)
data = tweepy.Cursor(api.search,q = "bitcoin",since = "2017-11-06",until = "2017-11-07",lang = "en").items()
#most recent data is fetched first
while True:
try:
tweet = data.next()
if tweet.user.followers_count > 0: #collecting tweets made by users with min 100k followers
#i+=1
# Write a row to the CSV file. I use encode UTF-8
csvWriter.writerow([tweet.user.name.encode('utf-8', errors='ignore'),tweet.user.followers_count,tweet.created_at, tweet.text.encode('utf-8', errors='ignore'),tweet.id])
#print("------wrote a tweet-----")
except tweepy.TweepError:
#print("---------------------In sleep In sleep In sleep In sleep----------------------------")
time.sleep(600)
continue
except StopIteration:
#print("---------------------Something is wrong----------------------------")
break
csvFile.close()