forked from ptwobrussell/Recipes-for-Mining-Twitter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
recipe__get_search_results_for_trending_topic.py
55 lines (37 loc) · 1.21 KB
/
recipe__get_search_results_for_trending_topic.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
# -*- coding: utf-8 -*-
import os
import sys
import json
import twitter
from recipe__extract_tweet_entities import get_entities
MAX_PAGES = 15
RESULTS_PER_PAGE = 100
# Get the trending topics
t = twitter.Twitter(domain='api.twitter.com', api_version='1')
trends = [
trend['name']
for trend in t.trends._(1)()[0]['trends']
]
idx = 0
for trend in trends:
print '[%i] %s' % (idx, trend,)
idx += 1
# Prompt the user
trend_idx = int(raw_input('\nPick a trend: '))
q = trends[trend_idx]
# Search
print >> sys.stderr, 'Fetching tweets for %s...' % (q, )
twitter_search = twitter.Twitter(domain="search.twitter.com")
search_results = []
for page in range(1,MAX_PAGES+1):
search_results += \
twitter_search.search(q=q, rpp=RESULTS_PER_PAGE, page=page)['results']
# Exract tweet entities and embed them into search results
for result in search_results:
result['entities'] = get_entities(result)
if not os.path.isdir('out'):
os.mkdir('out')
f = open(os.path.join(os.getcwd(), 'out', 'search_results.json'), 'w')
f.write(json.dumps(search_results, indent=1))
f.close()
print >> sys.stderr, "Entities for tweets about trend '%s' saved to %s" % (q, f.name,)