This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (39 loc) · 1.73 KB
/
main.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
from apscheduler.schedulers.blocking import BlockingScheduler
import constant
from modules.pickle import Pickle
from modules.twitter import Twitter
class Home2:
__slots__ = ("api", "pickle", "list_id")
def __init__(self) -> None:
self.api = Twitter(*constant.TWITTER_CREDENTIALS)
self.pickle = Pickle(path="data.pkl", default_data=set[int]())
self.list_id = constant.HOME2_LIST_ID
def get_list_member_ids(self) -> set[int]:
count = self.api.get_list(list_id=self.list_id).member_count
members = self.api.get_list_members(list_id=self.list_id, count=count)
return set(map(lambda member: member.id, members))
def get_random_user_ids(self) -> set[int]:
tweets = self.api.search_tweets(
"-filter:retweets -filter:replies", count=200, lang="ja"
)
return set(map(lambda tweet: tweet.user.id, tweets))
def remove_all_list_members(self) -> None:
user_ids = self.get_list_member_ids()
try:
self.api.remove_list_members(list_id=self.list_id, user_id=user_ids)
except Exception:
pass
def add_list_members(self) -> None:
user_ids = self.get_random_user_ids().difference(self.pickle.data)
self.api.add_list_members(list_id=self.list_id, user_id=user_ids)
self.update_added_user_ids(user_ids)
def update_added_user_ids(self, user_ids) -> None:
self.pickle.data.update(user_ids)
self.pickle.save()
def run(self) -> None:
self.remove_all_list_members()
self.add_list_members()
if __name__ == "__main__":
scheduler = BlockingScheduler({"apscheduler.timezone": "Asia/Tokyo"})
scheduler.add_job(Home2().run, "cron", hour=0)
scheduler.start()