fix timezone representation for TWINT tweets
This commit is contained in:
+30
-14
@@ -2,6 +2,7 @@
|
|||||||
# Continuously listen for cross-company interactions.
|
# Continuously listen for cross-company interactions.
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import traceback
|
||||||
import tweepy
|
import tweepy
|
||||||
from talenttweet import TalentTweet
|
from talenttweet import TalentTweet
|
||||||
|
|
||||||
@@ -10,6 +11,8 @@ import ttweetqueue as ttq
|
|||||||
import api_secrets
|
import api_secrets
|
||||||
import talent_lists as tl
|
import talent_lists as tl
|
||||||
|
|
||||||
|
errors_encountered = 0
|
||||||
|
|
||||||
def on_response(resp):
|
def on_response(resp):
|
||||||
ttweet = TalentTweet.create_from_v2api_response(resp)
|
ttweet = TalentTweet.create_from_v2api_response(resp)
|
||||||
|
|
||||||
@@ -21,20 +24,33 @@ def on_response(resp):
|
|||||||
print(f'Tweet {ttweet.tweet_id} is not cross-company.')
|
print(f'Tweet {ttweet.tweet_id} is not cross-company.')
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
sc = tweepy.StreamingClient(api_secrets.bearer_token())
|
global errors_encountered
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
sc = tweepy.StreamingClient(api_secrets.bearer_token())
|
||||||
|
|
||||||
# clear rules
|
# clear rules
|
||||||
rules_resp = sc.get_rules()
|
rules_resp = sc.get_rules()
|
||||||
if rules_resp.data:
|
if rules_resp.data:
|
||||||
sc.delete_rules(rules_resp.data)
|
sc.delete_rules(rules_resp.data)
|
||||||
|
|
||||||
# create new rules
|
# create new rules
|
||||||
for rule in tl.get_twitter_rules():
|
for rule in tl.get_twitter_rules():
|
||||||
sc.add_rules(tweepy.StreamRule(rule))
|
sc.add_rules(tweepy.StreamRule(rule))
|
||||||
|
|
||||||
sc.on_response=on_response
|
sc.on_response=on_response
|
||||||
sc.filter(
|
sc.filter(
|
||||||
expansions=TwAPI.TWEET_EXPANSIONS,
|
expansions=TwAPI.TWEET_EXPANSIONS,
|
||||||
media_fields=TwAPI.TWEET_MEDIA_FIELDS,
|
media_fields=TwAPI.TWEET_MEDIA_FIELDS,
|
||||||
tweet_fields=TwAPI.TWEET_FIELDS
|
tweet_fields=TwAPI.TWEET_FIELDS
|
||||||
)
|
)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('Interrupt signal received. Exiting listen mode.')
|
||||||
|
print(f'{errors_encountered} errors encountered throughout session.')
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
errors_encountered += 1
|
||||||
|
print('Ran into an error while in listen mode.')
|
||||||
|
traceback.print_exc()
|
||||||
|
print('Re-running listen mode...')
|
||||||
|
print(f'(Had {errors_encountered} errors so far.)')
|
||||||
+1
-1
@@ -71,7 +71,7 @@ async def async_main():
|
|||||||
print('RUNNING IN CATCH UP MODE')
|
print('RUNNING IN CATCH UP MODE')
|
||||||
if await catchup.run(PROGRAM_ARGS) and PROGRAM_ARGS.auto_listen:
|
if await catchup.run(PROGRAM_ARGS) and PROGRAM_ARGS.auto_listen:
|
||||||
print('CATCH UP MODE DONE, GOING INTO LISTEN MODE')
|
print('CATCH UP MODE DONE, GOING INTO LISTEN MODE')
|
||||||
await listen.run()
|
listen.run()
|
||||||
elif mode in ['d', 'delete-all']:
|
elif mode in ['d', 'delete-all']:
|
||||||
print('WARNING: SELF-DESTRUCT MODE')
|
print('WARNING: SELF-DESTRUCT MODE')
|
||||||
await self_destruct()
|
await self_destruct()
|
||||||
|
|||||||
+5
-2
@@ -1,4 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
@@ -69,9 +70,11 @@ class TalentTweet:
|
|||||||
if quoted_id == -1:
|
if quoted_id == -1:
|
||||||
quoted_id = util.get_user_id_online(quoted_username)
|
quoted_id = util.get_user_id_online(quoted_username)
|
||||||
|
|
||||||
# FIXME: resultant tweets don't show timezone properly
|
# NOTE: strptime doesn't attach timezone info.
|
||||||
|
# tweet's datetime will be in local time
|
||||||
date_time = datetime.datetime.strptime(tweet.datetime, '%Y-%m-%d %H:%M:%S %Z')
|
date_time = datetime.datetime.strptime(tweet.datetime, '%Y-%m-%d %H:%M:%S %Z')
|
||||||
print(date_time)
|
LOCAL_TIMEZONE = datetime.datetime.now().astimezone().tzinfo
|
||||||
|
date_time = date_time.replace(tzinfo=LOCAL_TIMEZONE) # attach system local timezone
|
||||||
return TalentTweet(tweet_id=tweet.id, author_id=tweet.user_id, date_time=date_time, mrq=(mentions, reply_to, quoted_id))
|
return TalentTweet(tweet_id=tweet.id, author_id=tweet.user_id, date_time=date_time, mrq=(mentions, reply_to, quoted_id))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Reference in New Issue
Block a user