|
- from twitter import *
-
- import pprint
-
- import json
- import pathlib
- import urllib
-
- import twauth
-
- forcerefetch = False
-
- base_dir = pathlib.Path('content') / 'twitter'
- imgbase_dir = pathlib.Path('content') / 'media' / 'twitter'
-
- # Info for getting alt text.
- # Requires a project, which requires a phone number, which it rejects my home phone
- # https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/media
-
- def write_media(media):
- mediaurl = media['media_url_https']
-
- parts = urllib.parse.urlparse(mediaurl)
- fname = pathlib.PurePosixPath(parts.path).name
- imgpath = imgbase_dir / fname
-
- if not forcerefetch and imgpath.exists():
- print('media exists', fname)
- return
-
- resp = urllib.request.urlopen(mediaurl)
- try:
- if resp.getcode() == 200:
- with open(imgpath, 'wb') as fp:
- d = None
- while d != b'':
- d = resp.read1()
- fp.write(d)
- finally:
- resp.close()
-
- def write_tweet(tweet):
- twpath = base_dir / (tweet['id_str'] + '.yaml')
-
- if not forcerefetch and twpath.exists():
- print('exists', tweet['id_str'])
- return
-
- #pprint.pprint(repr(tweet))
-
- try:
- # Has media to d/l
- for i in tweet['entities']['media']:
- write_media(i)
- except KeyError as e:
- pass
-
- with open(twpath, 'w') as fp:
- print('write:', tweet['id_str'])
-
- # wrap in --- to mark as metadata for hyde/jinja2
- print('---', file=fp)
- json.dump(tweet, fp)
- # dump doesn't terminate w/ nl
- print('', file=fp)
- print('---', file=fp)
-
- def act_my_thread(tw, start_tw_id, stop_tw_id):
- tw_id = int(stop_tw_id)
-
- while True:
- # https://alexwlchan.net/2022/11/tweet-alt-text/
- base_tw = tw.statuses.show(_id=tw_id, tweet_mode='extended', include_ext_alt_text=True)
-
- write_tweet(base_tw)
-
- if start_tw_id == tw_id:
- break
-
- tw_id = base_tw['in_reply_to_status_id']
-
- if __name__ == '__main__':
- args, kwargs = twauth.twit_argskw
- tw = Twitter(*args, **kwargs)
-
- # https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/get-statuses-show-id
- #print(repr(tw.statuses.show(_id=1556349580932833280)))
-
- with open('tweets.txt') as fp:
- for i in fp:
- action, tw_id, *extra = i.split()
- tw_id = int(tw_id)
-
- locals()['act_%s' % action](tw, tw_id, *extra)
-
- print('done')
|