-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitter_Trends_Script_5.14.py
108 lines (86 loc) · 3.81 KB
/
Twitter_Trends_Script_5.14.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
Created on Wednesday May 13 13:00:14 2015
@author: getTrends function modified from trends.py
original script: https://gist.github.com/alienone/9349702
Modified by Tracy to create and update a CSV with top 10 Twitter trends.
Index refers to the 1 - 10 trending topic ranking. 15 is 5th, 23 is 3rd, etc.
You don't have to start with a CSV, program will create one in your folder.
Once it exists, the program will append new data to the bottom of the sheet.
Use Task Scheduler to arrange to have this run every 15 minutes, which is the
current rate limit for Twitter.
"""
# Pip install these modules if you don't already have them
from twython import Twython
from urllib2 import unquote
import pandas as pd
# Please use your own Twitter credentials, found at apps.twitter.com.
APP_KEY = "REPLACE_WITH_YOUR_APP_KEY"
APP_SECRET = "REPLACE_WITH_YOUR_APP_SECRET"
OAUTH_TOKEN = "REPLACE_WITH_YOUR_OAUTH_TOKEN"
OAUTH_TOKEN_SECRET = "REPLACE_WITH_YOUR_OAUTH_TOKEN_SECRET"
# Yahoo WOEIDs, you can specify by city, address, zip code, state country...
# See more here: http://woeid.rosselliot.co.nz/lookup/
woeid_list = [('World', 1),
('USA', 23424977)]
#('UK', 23424975),
#('France', 23424819),
#('Italy', 23424853),
#('Germany', 23424829),
#('Spain', 23424950),
#('Mexico', 23424900),
#('Australia', 23424748),
#('New Zealand', 23424916),
#('Canada', 23424775),
#('Brazil', 23424768)
# ]
def getTrends(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET, woeid_list):
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
list_of_tuples = []
for item in woeid_list:
place_name = item[0]
place_woeid = item[1]
place_trend_list = twitter.get_place_trends(id=place_woeid)
for item in place_trend_list:
for element in item['trends']:
place_tuple =(place_name, place_woeid, element['name'], item['created_at'])
list_of_tuples.append(place_tuple)
return list_of_tuples
def trend_list_parsing():
list_of_trend_results=[]
for trend in get_trends_results:
trend_date = trend[3]
trend_location = trend[0]
trend_woeid = trend[1]
trend_name = (unquote(trend[2]))
trend_tuple = (trend_date, trend_location, trend_woeid, trend_name)
list_of_trend_results.append(trend_tuple)
return list_of_trend_results
def is_it_trending(name_of_trend):
hashtag_trend = '#' + name_of_trend
x = item_dataframe.loc[item_dataframe.Hashtag.values == (name_of_trend)]
if x.empty:
y = item_dataframe.loc[item_dataframe.Hashtag.values == (hashtag_trend)]
if y.empty:
print('Not currently trending!')
else:
print(name_of_trend + ' is currently trending in the following places')
print(y)
else:
print(name_of_trend + ' is currently trending in the following places')
print(x)
# Getting the data
get_trends_results = getTrends(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET, woeid_list)
trend_data = trend_list_parsing()
# Throwing it in a dataframe
item_dataframe = pd.DataFrame(trend_data)
item_dataframe.columns = ['Date', 'Place', 'WOEID', 'Hashtag']
# To see what's in the dataframe without checking the CSV,
#just type "item_dataframe" into the console.
# Appending the dataframe to a CSV. Edit the CSV name here.
with open('Testingtesting.csv', 'a') as fd:
item_dataframe.index += 1
item_dataframe.to_csv(fd, mode='a',header=False, encoding='utf-8')
fd.close()
# Fill in your trend here. It should work with and without hashtags.
is_it_trending('ThinkContent')