Skip to content

Commit b47d938

Browse files
committed
add twitter parse trending and get news script
1 parent 8889ade commit b47d938

File tree

2 files changed

+987
-0
lines changed

2 files changed

+987
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding:utf-8 -*-
2+
"""
3+
This script can get the trending topic of twitter and related news.
4+
5+
Notice : Sometimes there is no news related with one topic because of the frequency limit of token
6+
"""
7+
8+
import requests
9+
from twitter import Twitter, OAuth
10+
11+
12+
"""config value"""
13+
twitter_access_key = "1644320072-YX7aGGIzXVgnBwM0JquHtRandomValueGenerated"
14+
twitter_access_secret = "UejBVkIWeCN2eF2ogDFXmVqZC4DRandomValueGenerated"
15+
twitter_consumer_key = "2vunqZWJJEtRandomValueGenerated"
16+
twitter_consumer_secret = "bcJyPobhfrknBWoRandomValueGenerated"
17+
18+
news_api_key = "fef960ab-aRandomValueGenerated"
19+
20+
21+
def get_twitter_trends(location_id=23424975):
22+
"""
23+
get the trending topic based on location
24+
"""
25+
twitter = Twitter(auth=OAuth(twitter_access_key, twitter_access_secret,
26+
twitter_consumer_key, twitter_consumer_secret))
27+
28+
# this id is for UK , you can change it to any locations you want
29+
# TODO : add a translation tool between location name and location code
30+
results = twitter.trends.place(_id=location_id)
31+
32+
for location in results:
33+
for trend in location["trends"]:
34+
print(
35+
"The trend topic is {} following is the related news :\n -------".format(trend["name"]))
36+
# remove # before trend topic
37+
print(trend["name"][1:] if trend["name"].startswith(
38+
"#") else trend["name"])
39+
get_news(trend["name"][1:] if trend["name"].startswith(
40+
"#") else trend["name"])
41+
42+
43+
def get_news(key_word="SMU"):
44+
"""
45+
Get the news based on https://eventregistry.org/documentation/examples#event-registry-api-examples-articles-get-articles
46+
I don't use the client insided but the restful api ,it is more customized
47+
"""
48+
body = {
49+
"action": "getArticles",
50+
"keyword": key_word,
51+
"articlesPage": 1,
52+
"articlesCount": 100,
53+
"articlesSortBy": "date",
54+
"articlesSortByAsc": False,
55+
"articlesArticleBodyLen": -1,
56+
"resultType": "articles",
57+
"dataType": ["news", "pr"],
58+
"apiKey": "fef960ab-a667-4217-a42e-fa0e45fe1e53",
59+
"forceMaxDataTimeWindow": 31
60+
}
61+
62+
r = requests.post(
63+
'http://eventregistry.org/api/v1/article/getArticles', json=body)
64+
for x in r.json()['articles']['results'][:5]:
65+
try:
66+
print("title is : {}".format(x["title"]))
67+
print("url is : {}".format(x["url"]))
68+
print("dateTime is : {}".format(x["dateTime"]))
69+
print("abstract of content is : {}".format(x["body"][:40]))
70+
print("--------------------------------")
71+
except UnicodeEncodeError as ex:
72+
pass
73+
74+
75+
if __name__ == '__main__':
76+
get_twitter_trends()

0 commit comments

Comments
 (0)