Skip to content

Commit

Permalink
Pass screen_name to API
Browse files Browse the repository at this point in the history
  • Loading branch information
wzyboy committed Jun 9, 2022
1 parent 8fdf0ba commit e965b1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
2. [Download a copy of your Twitter archive](https://help.twitter.com/en/managing-your-account/how-to-download-your-twitter-archive).
3. Load the tweets in the archive: `./tbeat.py tweet.js my-tweets`.
4. Copy `tokens.example.json` to `tokens.json` and fill in your [API details](https://developer.twitter.com/en/apps).
5. Use cron / systemd timer to periodically run `./tbeat.py api my-tweets` and keep your tweets updated.
5. Use cron / systemd timer to periodically run `./tbeat.py api:<screen_name> my-tweets` and keep your tweets updated.


## Notes
Expand Down
16 changes: 8 additions & 8 deletions tbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ def __init__(self, screen_name, since_id=0, user_dict=None):
self.screen_name = screen_name

def load(self, source: str):
if source == 'api':
tweets = self.load_tweets_from_api()
if source.startswith('api:'):
screen_name = source.split(':')[1]
tweets = self.load_tweets_from_api(screen_name)
elif source.endswith('.js'):
tweets = self.load_tweets_from_js(source)
elif source.endswith(('.jl', '.jsonl')):
Expand All @@ -40,7 +41,7 @@ def load(self, source: str):
raise RuntimeError(
'source must be a tweet.js file from newer Twitter Archive, '
'a "tweets" directory with monthly js files from older Twitter Archive, '
'a .jl file that consists of one tweet per line, or "api"'
'a .jl file that consists of one tweet per line, or "api:<screen_name>"'
)
return tweets

Expand Down Expand Up @@ -97,7 +98,7 @@ def load_tweets_from_js_dir(self, js_dir):
tweet = self.inject_user_dict(tweet)
yield tweet

def load_tweets_from_api(self, tokens_filename='tokens.json'):
def load_tweets_from_api(self, screen_name, tokens_filename='tokens.json'):
'''Load tweets from Twitter API.'''

# Authenticate against Twitter API
Expand All @@ -109,11 +110,10 @@ def load_tweets_from_api(self, tokens_filename='tokens.json'):
kwargs = {
'tweet_mode': 'extended',
'trim_user': False,
'screen_name': screen_name,
}
if self.since_id:
kwargs['since_id'] = self.since_id
if self.screen_name:
kwargs['screen_name'] = self.screen_name
cursor = tweepy.Cursor(api.user_timeline, **kwargs).items()

def status_iterator(cursor):
Expand Down Expand Up @@ -191,10 +191,10 @@ def gen_actions():

def main():
ap = argparse.ArgumentParser()
ap.add_argument('source', help='source of tweets: "tweet.js", "tweets" dir, *.jl, or "api"')
ap.add_argument('source', help='source of tweets: "tweet.js", "tweets" dir, *.jl, or "api:<screen_name>"')
ap.add_argument('index', help='dest index of tweets')
ap.add_argument('--es', help='Elasticsearch address, default is localhost')
ap.add_argument('--screen-name', help='inject user.screen_name if the value is not present in the archive.')
ap.add_argument('--screen-name', help='inject user.screen_name if the value is not present in the source.')
args = ap.parse_args()

ingester = ElasticsearchIngester(args.es, args.index)
Expand Down

0 comments on commit e965b1d

Please sign in to comment.