import contextlib from datetime import datetime, timedelta from defs.glover import consumer_key, consumer_secret, access_token_key, access_token_secret import twitter from pyrogram.enums import ParseMode from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InputMediaPhoto, InputMediaVideo twitter_api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token_key=access_token_key, access_token_secret=access_token_secret, tweet_mode='extended') def twitter_link(status_id, qid, uid): if qid: return InlineKeyboardMarkup([[ InlineKeyboardButton(text="Source", url=f"https://twitter.com/{uid}/status/{status_id}"), InlineKeyboardButton(text="RSource", url=f"https://twitter.com/{qid}"), InlineKeyboardButton(text="Author", url=f"https://twitter.com/{uid}")]]) else: return InlineKeyboardMarkup([[ InlineKeyboardButton(text="Source", url=f"https://twitter.com/{uid}/status/{status_id}"), InlineKeyboardButton(text="Author", url=f"https://twitter.com/{uid}")]]) def twitter_user_link(user_username, status_link): if not status_link: return InlineKeyboardMarkup([[ InlineKeyboardButton(text="Author", url=f"https://twitter.com/{user_username}")]]) else: return InlineKeyboardMarkup([[ InlineKeyboardButton(text="Author", url=f"https://twitter.com/{user_username}"), InlineKeyboardButton(text="Status", url=status_link) ]]) def twitter_media(text, media_model, media_list): media_lists = [] for ff in range(0, len(media_model)): if media_model[ff] == 'photo': media_lists.append(InputMediaPhoto( media_list[ff], caption=text if ff == 0 else None, parse_mode=ParseMode.HTML )) else: media_lists.append( InputMediaVideo( media_list[ff], caption=text if ff == 0 else None, parse_mode=ParseMode.HTML )) return media_lists def get_twitter_time(date: str) -> str: try: date = datetime.strptime(date, "%a %b %d %H:%M:%S +0000 %Y") + timedelta(hours=8) return date.strftime("%Y-%m-%d %H:%M:%S") except Exception: return date def get_twitter_user(url_json): user_name = url_json.name user_username = url_json.screen_name verified = '' protected = '' status = '' status_link = None if url_json.verified: verified = '๐Ÿ’Ž' if url_json.protected: protected = '๐Ÿ”’' if url_json.status: status_link = f"https://twitter.com/{user_username}/{url_json.status.id_str}" status = f'๐Ÿ†• New Status: {get_twitter_time(url_json.status.created_at)}\n' text = f'Twitter User Info\n\n' \ f'Name: {verified}{protected}{user_name}\n' \ f'Username: @{user_username}\n' \ f'Bio: {url_json.description}\n' \ f'Joined: {get_twitter_time(url_json.created_at)}\n' \ f'{status}' \ f'๐Ÿ“ค {url_json.statuses_count} โค๏ธ{url_json.favourites_count} ' \ f'็ฒ‰ไธ {url_json.followers_count} ๅ…ณๆณจ {url_json.friends_count}' return text, user_username, status_link def get_twitter_status(url_json): created_at = get_twitter_time(url_json.created_at) favorite_count = url_json.favorite_count if hasattr(url_json, 'favorite_count') else 0 retweet_count = url_json.retweet_count if hasattr(url_json, 'retweet_count') else 0 user_name = url_json.user.name user_username = url_json.user.screen_name text = url_json.full_text if hasattr(url_json, 'full_text') else 'ๆš‚ ๆ—  ๅ†… ๅฎน' text = f'{text}' verified = '' protected = '' if url_json.user.verified: verified = '๐Ÿ’Ž' if url_json.user.protected: protected = '๐Ÿ”’' user_text = f'{verified}{protected}{user_name} ๅ‘่กจไบŽ {created_at}' \ f'\n๐Ÿ‘ {favorite_count} ๐Ÿ” {retweet_count}' media_model = [] media_list = [] media_alt_list = [] with contextlib.suppress(Exception): media_info = url_json.media for i in media_info: if i.type == 'photo': media_model.append('photo') media_list.append(i.media_url_https) elif i.type == 'animated_gif': media_model.append('gif') media_list.append(i.video_info['variants'][0]['url']) else: media_model.append('video') media_list.append(i.video_info['variants'][1]['url']) try: media_alt_list.append(i.ext_alt_text) except: media_alt_list.append('') quoted_status = False with contextlib.suppress(Exception): quoted = url_json.quoted_status quoted_status = quoted.user.screen_name + '/status/' + url_json.quoted_status_id_str quoted_created_at = get_twitter_time(quoted.created_at) quoted_favorite_count = quoted.favorite_count if hasattr(quoted, 'favorite_count') else 0 quoted_retweet_count = quoted.retweet_count if hasattr(quoted, 'retweet_count') else 0 quoted_user_name = quoted.user.name quoted_user_username = quoted.user.screen_name quoted_text = quoted.full_text if hasattr(quoted, 'full_text') else 'ๆš‚ ๆ—  ๅ†… ๅฎน' text += f'\n\n> {quoted_text}' quoted_verified = '' quoted_protected = '' if quoted.user.verified: quoted_verified = '๐Ÿ’Ž' if quoted.user.protected: quoted_protected = '๐Ÿ”’' user_text += f'\n> {quoted_verified}{quoted_protected}' \ f'{quoted_user_name} ๅ‘่กจไบŽ {quoted_created_at}' \ f'\n๐Ÿ‘ {quoted_favorite_count} ๐Ÿ” {quoted_retweet_count}' with contextlib.suppress(Exception): quoted_media_info = quoted.media for i in quoted_media_info: if i.type == 'photo': media_model.append('photo') media_list.append(i.media_url_https) elif i.type == 'animated_gif': media_model.append('gif') media_list.append(i.video_info['variants'][0]['url']) else: media_model.append('video') media_list.append(i.video_info['variants'][0]['url']) try: media_alt_list.append(i.ext_alt_text) except: media_alt_list.append('') return text, user_text, media_model, media_list, quoted_status