Skip to content

Instantly share code, notes, and snippets.

@iamevn
Created March 18, 2020 22:38
Show Gist options
  • Save iamevn/3e564e453b6edb07d3f1b322dfb5b6f1 to your computer and use it in GitHub Desktop.
Save iamevn/3e564e453b6edb07d3f1b322dfb5b6f1 to your computer and use it in GitHub Desktop.
import re
import webbrowser
from tweet import gen_tweet
from tweepy_api import get_api
def last_rt(user, tweet_id):
api = get_api()
timeline = api.user_timeline(user, max_id=tweet_id, count=100)
for status in timeline:
tweet = gen_tweet(status)
if tweet.is_rt:
return tweet
print('no rts on timeline')
return None
def main():
m = None
while not m:
orig = input('url of tweet to find LRT of: ')
m = re.search(r'twitter.com/(?P<user>[^/]*)/status/(?P<id>[0-9]*)', orig)
if not m:
print(f'is this a twitter url? input: {orig}')
lrt = last_rt(m.group('user'), m.group('id'))
print(f'found rt: {lrt.pretty_print()}')
i = input('\n[o]pen in webbrowser? ')
if any(c in i for c in 'oOyY'):
print(f'Opening {lrt.url}')
webbrowser.open(lrt.url)
if __name__ == '__main__':
main()
import tweepy
def get_api():
consumer_key = 'https:/developer.twitter.com/'
consumer_secret = 'https:/developer.twitter.com/'
access_token = 'https:/developer.twitter.com/'
access_token_secret = 'https:/developer.twitter.com/'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(auth)
from typing import List, Optional
from dataclasses import dataclass
from datetime import datetime
import tweepy
from tweepy_api import get_api
@dataclass
class Tweet:
'''Class to track tweet id, author, text, media urls, reply type'''
id: int
author: str
text: str
date: datetime
media_urls: List[str]
in_reply_to: Optional[int]
retweet_of: Optional['Tweet']
@property
def url(self) -> str:
return f'https://twitter.com/{self.author}/status/{self.id}'
@property
def is_rt(self) -> bool:
return self.retweet_of is not None
def pretty_print(self) -> str:
return f'[{self.date.strftime("%Y-%m-%d")}] {self.text}'
def __hash__(self):
return hash(self.id)
def __eq__(self, other):
return isinstance(other, Tweet) and self.id == other.id
def is_retweet(status) -> bool:
return getattr(status, 'retweeted_status', False)
def gen_tweet(status) -> Tweet:
if not isinstance(status, tweepy.models.Status):
api = get_api()
status = api.get_status(status, tweet_mode='extended')
rt = None
if is_retweet(status):
rt = gen_tweet(status.retweeted_status)
return Tweet(
status.id,
status.author.screen_name,
getattr(status, 'full_text', status.text),
status.created_at,
[entity['media_url'] for entity in status.entities['media']] if 'media' in status.entities else [],
status.in_reply_to_status_id,
rt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment