-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_trends.py
81 lines (64 loc) · 2.5 KB
/
twitter_trends.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
import twitter
import json
import time
import datetime
"""
Twitter track which uses the python-twitter package to get tweets and calculates the
tweet rate at any given time
@author Tyler Lloyd
"""
# Setup your keys
api = twitter.Api(consumer_key='YOUR_CONSUMER_KEY',
consumer_secret='YOUR_CONSUMER_SECRET_KEY',
access_token_key='YOUR_ACCESS_TOKEN_KEY',
access_token_secret='YOUR_ACCESS_TOKEN_SECRET_KEY')
# Baseline data
since = 0
requests = 0
stack = {}
while True:
try:
trends = api.GetSearch(raw_query='q=%40wojespn&count=100&result_type=recent', since_id=since, result_type="recent")
except twitter.error.TwitterError:
print("Error. Waiting for 2 min and re-trying")
time.sleep(120)
continue
print("Most current ID: {}".format(since))
prev_stack_size = len(stack)
# Sets the top result as the most recent Twitter id to use on the next search
# and loads each tweet into the stack object as a dictionary
for trend in trends:
id = trend.id
obj = trend.AsDict()
stack[id] = obj
since = max(id, since)
# calculate the size between the previous search and the current search
# and uses that as the rate
per_second_rate = (len(stack) - prev_stack_size) / 6.0
rate = per_second_rate * 60.0
# record the results of the rate in a file in the same directory
rateFile = open("log.txt", 'a')
rateFile.write("{time}, {rate}\n".format(time=datetime.datetime.now(), rate=rate))
rateFile.close()
print("Current @wojespn Tweet Rate: {} tweets/min".format(rate))
print("Current stack is: {}".format(len(stack)))
# After 1k new tweets have eventually been loaded this
# stores the oldest 900 into a new file. It keeps the most recent 100
# so that the rate stays consistent and doesn't jump up simply by loading
# a brand new 100 instantly on the next pass
if len(stack) >= 1000:
temp_stack = {}
stack_keys = list(stack.keys())
for _ in range(100):
k = stack_keys.pop()
temp_stack[k] = stack.pop(k)
now = datetime.datetime.now()
print("{}\tBuilding new batch. {} tweets saved".format(now, len(stack)))
fp = open("data/{0}_datadump.json".format(time.time()), 'x')
fp.write(json.dumps(stack))
fp.close()
stack = temp_stack
requests = requests + 1
print("Total Calls: {}".format(requests))
print("")
time.sleep(6)