Files
NijiHolo_EN_ID_Bot/src/talenttweet.py
T

141 lines
5.6 KiB
Python
Raw Normal View History

2023-08-14 22:39:47 -07:00
from datetime import datetime
from zoneinfo import ZoneInfo
2022-09-24 17:56:58 -07:00
import platform
import pytz
2023-08-15 17:33:29 -07:00
from tweety.types import *
2022-09-24 17:56:58 -07:00
2023-08-15 17:33:29 -07:00
from talent_lists import is_cross_company
2022-09-27 15:09:09 -07:00
import util
2022-09-24 17:56:58 -07:00
class TalentTweet:
2023-08-14 22:39:47 -07:00
# Serialized one-liner format:
# {tweet} {author} {time in seconds since epoch} m {mention set} r {reply to author} q {quote tweet author} rt {retweeted tweet's id}
def serialize(self):
s = f'{self.tweet_id} {self.author_id} {self.date_time.timestamp()} '
2023-08-15 17:33:29 -07:00
if self.rt_author_id != None:
s += f'rt {self.rt_id} {self.rt_author_id}'
2023-08-14 22:39:47 -07:00
return s[:-1] # stop here since retweets can't have other info
if len(self.mentions) > 0:
s += 'm '
for id in self.mentions:
s += f'{id} '
if self.reply_to:
s += f'r {self.reply_to} '
if self.quote_retweeted:
s += f'q {self.quote_retweeted} '
return s[:-1]
@staticmethod
def deserialize(serialized_str: str):
tokens = serialized_str.split()
if len(tokens) < 3:
raise ValueError('not enough tokens to reconstruct a TalentTweet')
tweet_id, author_id = int(tokens[0]), int(tokens[1])
2023-08-14 22:39:47 -07:00
date_time = datetime.fromtimestamp(float(tokens[2]), tz=pytz.utc)
mentions = set()
reply_to = None
quote_retweeted = None
mode = ''
for i in range(3, len(tokens)):
if len(tokens[i]) == 1 and not tokens[i].isnumeric(): # mode switch
mode = tokens[i]
continue
if tokens[i].isnumeric():
if mode == 'm': # mentions
mentions.add(int(tokens[i]))
continue
if mode == 'r': # reply_to
reply_to = int(tokens[i])
continue
if mode == 'q': # quote_retweeted
quote_retweeted = int(tokens[i])
return TalentTweet(
tweet_id=tweet_id, author_id=author_id,
date_time=date_time, mrq=(mentions, reply_to, quote_retweeted)
)
2023-08-15 17:33:29 -07:00
## Creates a TalentTweet from a Tweety-library Tweet.
@staticmethod
def create_from_tweety(tweety: Tweet):
return TalentTweet(
tweet_id=int(tweety.id), author_id=int(tweety.author.id),
date_time=tweety.date, text=tweety.text,
mrq=(
[int(x.id) for x in tweety.user_mentions],
int(tweety._original_tweet['in_reply_to_user_id_str']) if tweety.is_reply else None,
int(tweety.quoted_tweet.author.id) if tweety.quoted_tweet is not None else None
),
rt_author_id=tweety.retweeted_tweet.author.id if tweety.is_retweet else None,
rt_mentions=[int(x.id) for x in tweety.retweeted_tweet.user_mentions] if tweety.is_retweet else list()
)
2022-09-27 02:49:03 -07:00
2023-08-15 17:33:29 -07:00
def __init__(self, tweet_id: int, author_id: int, date_time: datetime, text: str = None, mrq: tuple[list[int], int|None, int|None]=None, rt_author_id: int=None, rt_mentions: list[int]=None):
# basic information
self.tweet_id, self.author_id = tweet_id, author_id
2023-08-15 17:33:29 -07:00
self.username = util.get_username_local(self.author_id)
self.date_time = date_time
2023-08-15 17:33:29 -07:00
self.text = text
# filter twitter users to only be cross-company
self.mentions = {x for x in mrq[0] if is_cross_company(author_id, x)}
self.reply_to = mrq[1] if mrq[1] is not None and is_cross_company(author_id, mrq[1]) else None
self.quote_retweeted = mrq[2] if mrq[2] is not None and is_cross_company(author_id, mrq[2]) else None
self.rt_mentions = {x for x in rt_mentions if is_cross_company(author_id, x)} if rt_mentions is not None else None
self.rt_author_id = rt_author_id if (rt_author_id is not None and is_cross_company(author_id, rt_author_id)) or (len(self.rt_mentions) > 0) else None
# all users involved, except for the author
self.all_parties = {self.reply_to, self.quote_retweeted}
self.all_parties.update(self.mentions)
try:
self.all_parties.remove(None)
2022-10-01 13:33:20 -07:00
except: pass
try:
self.all_parties.remove(self.author_id)
2022-10-01 13:33:20 -07:00
except: pass
2022-09-24 17:56:58 -07:00
def __repr__(self) -> str:
return (
2023-08-15 17:33:29 -07:00
f'======================================================'
f'{self.tweet_id} from {self.username}:\n'
2022-09-24 17:56:58 -07:00
f'{self.get_datetime_str()}\n'
2023-08-15 17:33:29 -07:00
f'parties: {self.get_all_parties_usernames()}\n'
f'mentions: {self.mentions}\n'
f'reply_to: {self.reply_to}\n'
f'quote_retweeted: {self.quote_retweeted}\n'
2023-08-15 17:33:29 -07:00
f'cross-company? {self.is_cross_company()}\n'
f'{self.serialize()}\n'
2023-08-15 17:33:29 -07:00
f'{self.url()}'
2022-09-24 17:56:58 -07:00
)
2023-08-15 17:33:29 -07:00
def url(self):
return f'https://www.twitter.com/{self.username}/status/{self.tweet_id}'
def is_cross_company(self):
for other_id in self.all_parties:
2023-08-15 17:33:29 -07:00
if is_cross_company(self.author_id, other_id):
2023-08-14 22:39:47 -07:00
return True
2022-09-24 17:56:58 -07:00
return False
def get_all_parties_usernames(self):
if len(self.all_parties) > 0:
2022-09-24 17:56:58 -07:00
s = str()
for id in self.all_parties:
2022-09-27 02:49:03 -07:00
s += f'{util.get_username_local(id)}, '
2022-09-24 17:56:58 -07:00
return s[0:-2]
return 'none'
def get_datetime_str(self):
unpad = '#' if platform.system() == 'Windows' else '-'
2023-01-23 22:37:21 -08:00
return self.date_time.strftime(f'%b %{unpad}d %Y, %{unpad}I:%M%p (%Z)')