-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitter_handler.py
38 lines (33 loc) · 1.36 KB
/
twitter_handler.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
import twitter
from util import COUNTRIES, ICOUNTRY_ECOUNTRY_MAP
class TwitterHandler:
def __init__(
self, token: str, token_secret: str, consumer_key: str, consumer_secret: str
) -> None:
self.token = token
self.token_secret = token_secret
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
def post(self, text: str) -> None:
auth = twitter.OAuth(
self.token, self.token_secret, self.consumer_key, self.consumer_secret
)
t = twitter.Twitter(auth=auth)
t.statuses.update(status=text)
@staticmethod
def create_text(d: dict) -> str:
title = d["ja_translated"]["title"]
country = ""
ecountry = ICOUNTRY_ECOUNTRY_MAP[d["displayed_country"]]
for country_dict in COUNTRIES:
if ecountry == country_dict["country"]:
country = country_dict["name"]["ja"]
assert country != ""
domain = d["ja_domain_label"]
if d["topics"]:
sorted_topics = sorted(d["topics"].items(), key=lambda x: -x[1])
topic = sorted_topics[0][0]
content = f"{title}({country},{topic}のニュース,{domain})"
else:
content = f"{title}({country}のニュース,{domain})"
return content + "\n" + "https://lotus.kuee.kyoto-u.ac.jp/NLPforCOVID-19"