-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsdqs_fetch.py
57 lines (49 loc) · 1.47 KB
/
nsdqs_fetch.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
"""
NSDQS - NASDAQ Stream Dataset
Contens of this file:
Fetch from twitter
Create sentiment label
authors: Christoph Raab
"""
import twint
import datetime
from textblob import TextBlob
# Function to get sentiment
import pandas as pd
import os
import datetime
def sentiment_anylsis(sentence):
temp = TextBlob(sentence).sentiment[0]
if temp == 0.0:
return 0.0 # Neutral
elif temp >= 0.0:
return 1.0 # Positive
else:
return 2.0 # Negative
def fetch_tweets(hastag,date,limit=1):
c = twint.Config()
c.Search = "#"+ hastag
c.Store_object = True
c.Since = date
c.Lang = "en"
c.Limit = limit
twint.run.Search(c)
tweets = twint.output.tweets_list
return [t.tweet for t in tweets]
def create_dates(numdays):
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]
return date_list
def main_fetch():
hashtags = ['ADBE', 'GOOGL', 'AMZN', 'AAPL', 'ADSK', 'BKNG', 'EXPE', 'INTC', 'MSFT', 'NFLX', 'NVDA', 'PYPL', 'SBUX',
'TSLA', 'XEL']
dates = create_dates(300)
date = dates[-1].strftime("%Y-%m-%d")
for hashtag in hashtags:
tweets = fetch_tweets(hashtag,date,limit=10000)
tweets = list(set(tweets))
sentiments = [ [t,sentiment_anylsis(t)] for t in tweets ]
df = pd.DataFrame(sentiments,columns=["Tweets","Sentiment"])
df.to_csv("Tweets"+datetime.date.today().strftime("%d_%m_%y")+".csv")
if __name__ == '__main__':
main_fetch()