fixed a fucky
This commit is contained in:
+6
-4
@@ -5,6 +5,7 @@ holo_id = dict()
|
|||||||
niji_en = dict()
|
niji_en = dict()
|
||||||
niji_exid = dict()
|
niji_exid = dict()
|
||||||
talents = dict()
|
talents = dict()
|
||||||
|
talents_company = dict()
|
||||||
|
|
||||||
test_talents = dict()
|
test_talents = dict()
|
||||||
|
|
||||||
@@ -17,9 +18,10 @@ def __create_dict(file, _dict, company):
|
|||||||
words = line.split()
|
words = line.split()
|
||||||
if len(words) == 2 and line[0] != '#':
|
if len(words) == 2 and line[0] != '#':
|
||||||
name, id = line.split()
|
name, id = line.split()
|
||||||
name = f'{util.get_username_online(id, default=name)} ({company})' # attempt to get updated name
|
name = f'{util.get_username_online(id, default=name)}' # attempt to get updated name
|
||||||
talents[int(id)] = name
|
talents[int(id)] = name
|
||||||
_dict[int(id)] = name
|
_dict[int(id)] = name
|
||||||
|
talents_company[int(id)] = company
|
||||||
def init():
|
def init():
|
||||||
global holo_en
|
global holo_en
|
||||||
global holo_id
|
global holo_id
|
||||||
@@ -44,12 +46,12 @@ def get_twitter_rules():
|
|||||||
rules = list()
|
rules = list()
|
||||||
|
|
||||||
names = list(talents.values())
|
names = list(talents.values())
|
||||||
curr_rule = f'from:{names[0].split()[0]}'
|
curr_rule = f'from:{names}'
|
||||||
for name in list(talents.values())[1:]:
|
for name in list(talents.values())[1:]:
|
||||||
test_rule = curr_rule + f' OR from:{name.split()[0]}'
|
test_rule = curr_rule + f' OR from:{name}'
|
||||||
if len(test_rule) > 512:
|
if len(test_rule) > 512:
|
||||||
rules.append(curr_rule)
|
rules.append(curr_rule)
|
||||||
curr_rule = f'from:{name.split()[0]}'
|
curr_rule = f'from:{name}'
|
||||||
else:
|
else:
|
||||||
curr_rule = test_rule
|
curr_rule = test_rule
|
||||||
return rules
|
return rules
|
||||||
+53
-49
@@ -7,6 +7,7 @@ import tweepy
|
|||||||
import api_secrets
|
import api_secrets
|
||||||
import talenttweet as tt
|
import talenttweet as tt
|
||||||
import talent_lists as tl
|
import talent_lists as tl
|
||||||
|
import ttweetqueue as ttq
|
||||||
import util
|
import util
|
||||||
|
|
||||||
class TwAPI:
|
class TwAPI:
|
||||||
@@ -170,7 +171,7 @@ class TwAPI:
|
|||||||
|
|
||||||
async def post_tweet(self, text='', media_ids: list=None, reply_to_tweet: int=None, quote_tweet_id: int=None):
|
async def post_tweet(self, text='', media_ids: list=None, reply_to_tweet: int=None, quote_tweet_id: int=None):
|
||||||
try:
|
try:
|
||||||
tweet = self.client.create_tweet(text=text, media_ids=media_ids, in_reply_to_tweet_id=reply_to_tweet, quote_tweet_id=str(quote_tweet_id))
|
tweet = self.client.create_tweet(text=text, media_ids=media_ids, in_reply_to_tweet_id=reply_to_tweet, quote_tweet_id=quote_tweet_id)
|
||||||
return tweet
|
return tweet
|
||||||
except tweepy.TooManyRequests as e:
|
except tweepy.TooManyRequests as e:
|
||||||
wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1
|
wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1
|
||||||
@@ -194,7 +195,7 @@ class TwAPI:
|
|||||||
RETWEET = '{0} retweeted {1}!\n'
|
RETWEET = '{0} retweeted {1}!\n'
|
||||||
|
|
||||||
def create_text():
|
def create_text():
|
||||||
author_username = f'@/{util.get_username_local(ttweet.author_id)}'
|
author_username = f'@/{util.get_username_with_company(ttweet.author_id)}'
|
||||||
print_mention_ids = set(ttweet.mentions)
|
print_mention_ids = set(ttweet.mentions)
|
||||||
ret = str()
|
ret = str()
|
||||||
if is_catchup:
|
if is_catchup:
|
||||||
@@ -203,14 +204,14 @@ class TwAPI:
|
|||||||
|
|
||||||
# Tweet types
|
# Tweet types
|
||||||
if ttweet.rt_target is not None: # retweet
|
if ttweet.rt_target is not None: # retweet
|
||||||
ret += RETWEET.format(author_username, f'@/{util.get_username(ttweet.rt_author_id)}')
|
ret += RETWEET.format(f'{author_username}', f'@/{util.get_username_with_company(ttweet.rt_author_id)}')
|
||||||
elif ttweet.reply_to is not None: # reply
|
elif ttweet.reply_to is not None: # reply
|
||||||
reply_username = f'@/{util.get_username(ttweet.reply_to)}'
|
reply_username = f'@/{util.get_username_with_company(ttweet.reply_to)}'
|
||||||
ret += REPLY.format(author_username, reply_username)
|
ret += REPLY.format(author_username, reply_username)
|
||||||
# if qrt, push id into mentions
|
# if qrt, push id into mentions
|
||||||
print_mention_ids.add(ttweet.quote_retweeted)
|
print_mention_ids.add(ttweet.quote_retweeted)
|
||||||
elif ttweet.quote_retweeted is not None: # qrt
|
elif ttweet.quote_retweeted is not None: # qrt
|
||||||
quoted_username = f'@/{util.get_username(ttweet.quote_retweeted)}'
|
quoted_username = f'@/{util.get_username_with_company(ttweet.quote_retweeted)}'
|
||||||
ret += QUOTE_TWEET.format(author_username, quoted_username)
|
ret += QUOTE_TWEET.format(author_username, quoted_username)
|
||||||
elif len(ttweet.mentions) > 0: # standalone tweet
|
elif len(ttweet.mentions) > 0: # standalone tweet
|
||||||
ret += TWEET.format(author_username)
|
ret += TWEET.format(author_username)
|
||||||
@@ -222,64 +223,67 @@ class TwAPI:
|
|||||||
|
|
||||||
# mention line
|
# mention line
|
||||||
if len(print_mention_ids) > 0:
|
if len(print_mention_ids) > 0:
|
||||||
mention_usernames = [f'@/{util.get_username(x)}' for x in print_mention_ids]
|
mention_usernames = [f'@/{util.get_username_with_company(x)}' for x in print_mention_ids]
|
||||||
ret += (
|
ret += (
|
||||||
'mentioning '
|
'mentioning '
|
||||||
f'{", ".join(mention_usernames)}\n'
|
f'{", ".join(mention_usernames)}\n'
|
||||||
)
|
)
|
||||||
ret += '\n'
|
ret += '\n'
|
||||||
ret += '(this is a missed tweet)\n' if is_catchup else ''
|
# ret += '(this is a missed tweet)\n' if is_catchup else ''
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
text = create_text()
|
text = create_text()
|
||||||
ttweet_url = util.ttweet_to_url(ttweet)
|
ttweet_url = util.ttweet_to_url(ttweet)
|
||||||
|
|
||||||
if dry_run: # DRY-RUN: only print tweet
|
if dry_run: print('-------------------- DRY RUN --------------------')
|
||||||
print('--------------- [DRY RUN] ---------------')
|
print(text)
|
||||||
print(text)
|
if dry_run: return
|
||||||
print(f'QRT: {ttweet_url}')
|
|
||||||
else: # NO DRY-RUN: post actual tweet
|
# NO DRY-RUN: actually post tweet
|
||||||
# main tweet: text + screenshot
|
# main tweet: text + screenshot
|
||||||
|
try:
|
||||||
|
print('creating main QRT w/ screenshot...', end='')
|
||||||
|
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
|
||||||
|
twt_resp = await self.post_tweet(text, media_ids=media_ids, quote_tweet_id=ttweet.tweet_id)
|
||||||
|
print('done')
|
||||||
|
except:
|
||||||
|
print('error occurred trying to create main tweet, falling back to URL-main + reply screencap format')
|
||||||
|
text += f"\n{ttweet_url}"
|
||||||
try:
|
try:
|
||||||
print('creating main QRT w/ screenshot...', end='')
|
print('posting main tweet...', end='')
|
||||||
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
|
twt_resp = await self.post_tweet(text)
|
||||||
twt_resp = await self.post_tweet(text, media_ids=media_ids, quote_tweet_id=ttweet.tweet_id)
|
|
||||||
print('done')
|
print('done')
|
||||||
# twt_id = twt_resp.data['id']
|
twt_id = twt_resp.data['id']
|
||||||
# try:
|
# if ttweet.reply_to is not None:
|
||||||
# print('posting reply tweet...', end='')
|
# re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=)
|
||||||
# await self.post_tweet(text=ttweet_url, reply_to_tweet=twt_id)
|
# media_ids.insert(0, await self.get_ttweet_image_media_id())
|
||||||
# print('done')
|
|
||||||
# except:
|
|
||||||
# print('Had trouble posting reply tweet.')
|
|
||||||
except:
|
|
||||||
print('error occurred trying to create main tweet, falling back to URL-main + reply format')
|
|
||||||
text += f"\n{ttweet_url}"
|
|
||||||
try:
|
try:
|
||||||
print('posting main tweet...', end='')
|
print('creating reply img...', end='')
|
||||||
twt_resp = await self.post_tweet(text)
|
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
|
||||||
|
print('posting reply tweet...', end='')
|
||||||
|
await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids)
|
||||||
print('done')
|
print('done')
|
||||||
twt_id = twt_resp.data['id']
|
except:
|
||||||
# if ttweet.reply_to is not None:
|
print('Had trouble posting reply image tweet.')
|
||||||
# re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=)
|
print('successfully posted ttweet!')
|
||||||
# media_ids.insert(0, await self.get_ttweet_image_media_id())
|
return True
|
||||||
|
except tweepy.Forbidden as e:
|
||||||
try:
|
if 'duplicate content' in e.api_messages[0]:
|
||||||
print('creating reply img...', end='')
|
print('Twitter says the TalentTweet is a duplicate; skipping error-free...')
|
||||||
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
|
return False
|
||||||
print('posting reply tweet...', end='')
|
else:
|
||||||
await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids,)
|
raise e
|
||||||
print('done')
|
|
||||||
except:
|
def post_ttweet_by_id(self, tweet_id, is_catchup=False, dry_run=False):
|
||||||
print('Had trouble posting reply image tweet.')
|
ttweet = asyncio.run(tt.TalentTweet.create_from_id(tweet_id))
|
||||||
print('successfully posted ttweet!')
|
print(f'm({ttweet.mentions}), r({ttweet.reply_to}), q({ttweet.quote_retweeted})')
|
||||||
return True
|
if ttweet.is_cross_company():
|
||||||
except tweepy.Forbidden as e:
|
print(f'Tweet {ttweet.tweet_id} is cross-company! Creating post...')
|
||||||
if 'duplicate content' in e.api_messages[0]:
|
asyncio.run(self.post_ttweet(ttweet, is_catchup=is_catchup, dry_run=dry_run))
|
||||||
print('Twitter says the TalentTweet is a duplicate; skipping error-free...')
|
ttq.TalentTweetQueue.instance.add_finished_tweet(ttweet.tweet_id)
|
||||||
return False
|
else:
|
||||||
else:
|
print(f'Tweet {tweet_id} is not cross-company.')
|
||||||
raise e
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+8
-5
@@ -86,9 +86,6 @@ def ttweet_to_url(ttweet):
|
|||||||
username = get_username(ttweet.author_id)
|
username = get_username(ttweet.author_id)
|
||||||
return get_tweet_url(ttweet.tweet_id, username)
|
return get_tweet_url(ttweet.tweet_id, username)
|
||||||
|
|
||||||
def get_username_local(id):
|
|
||||||
return talent_lists.talents.get(id, f'{id}')
|
|
||||||
|
|
||||||
# twint
|
# twint
|
||||||
# May not work with short user IDs (ie. 1354241437)
|
# May not work with short user IDs (ie. 1354241437)
|
||||||
# def get_username_online(id, default=None):
|
# def get_username_online(id, default=None):
|
||||||
@@ -104,8 +101,10 @@ def get_username_local(id):
|
|||||||
# except:
|
# except:
|
||||||
# return str(default) if default is not None else f'{id}'
|
# return str(default) if default is not None else f'{id}'
|
||||||
|
|
||||||
# API v2 (tweepy)
|
def get_username_local(id):
|
||||||
# Short user IDs (ie. 1354241437) apparently don't work with twint
|
return talent_lists.talents.get(id, f'{id}')
|
||||||
|
|
||||||
|
# Retrieve username via API v2 (tweepy)
|
||||||
def get_username_online(id, default=None):
|
def get_username_online(id, default=None):
|
||||||
try:
|
try:
|
||||||
resp = twapi.TwAPI.instance.client.get_user(id=id)
|
resp = twapi.TwAPI.instance.client.get_user(id=id)
|
||||||
@@ -124,6 +123,10 @@ def get_username(id):
|
|||||||
return get_username_online(id)
|
return get_username_online(id)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def get_username_with_company(id):
|
||||||
|
company = talent_lists.talents_company.get(id, None)
|
||||||
|
return f'{get_username(id)} {f"({company})" if company is not None else ""}'
|
||||||
|
|
||||||
def get_user_id_local(username) -> int:
|
def get_user_id_local(username) -> int:
|
||||||
talent_usernames = list(talent_lists.talents.values())
|
talent_usernames = list(talent_lists.talents.values())
|
||||||
for i in range(0, len(talent_usernames)):
|
for i in range(0, len(talent_usernames)):
|
||||||
|
|||||||
Reference in New Issue
Block a user