async def on_member_remove(self, member): if (guild := hkd.get_wug_guild(self.bot.guilds)) == member.guild: channel = disc_utils.get(guild.channels, id=hkd.WELCOME_CHANNEL_ID) try: await member.guild.fetch_ban(member) return except discord.NotFound: await channel.send(embed=hkd.create_embed(title='{0} ({1}) has left the server.'.format(member.display_name, member)))
async def check_instagram(self): channel = hkd.get_updates_channel(self.bot.guilds) with suppress(Exception): for instagram_id in self.firebase_ref.child('last_instagram_posts').get().keys(): last_post_id = int(self.firebase_ref.child('last_instagram_posts/{0}'.format(instagram_id)).get()) profile = Profile.from_username(self.insta_api.context, instagram_id) user_name = profile.full_name posted_updates = [] for post in profile.get_posts(): if post.mediaid <= last_post_id: break post_text = post.caption post_pic = post.url post_link = 'https://www.instagram.com/p/{0}/'.format(post.shortcode) posted_updates.append(post.mediaid) if instagram_id in hkd.WUG_INSTAGRAM_IDS.values(): colour = hkd.get_oshi_colour(hkd.get_wug_guild(self.bot.guilds), hkd.dict_reverse(hkd.WUG_INSTAGRAM_IDS)[instagram_id]) else: colour = Colour(0x242424) author = {} author['name'] = '{0} (@{1})'.format(user_name, instagram_id) author['url'] = 'https://www.instagram.com/{0}/'.format(instagram_id) author['icon_url'] = profile.profile_pic_url await channel.send(embed=hkd.create_embed(author=author, title='Post by {0}'.format(user_name), description=post_text, colour=colour, url=post_link, image=post_pic)) if posted_updates: self.firebase_ref.child('last_instagram_posts/{0}'.format(instagram_id)).set(str(max(posted_updates))) self.check_instagram.change_interval(minutes=random.randint(20, 30))
async def check_live_streams(self): channel = hkd.get_seiyuu_channel(self.bot.guilds) now = datetime.utcnow().isoformat() + 'Z' with suppress(Exception): events = self.calendar.events().list(calendarId='primary', timeMin=now, maxResults=10, singleEvents=True, orderBy='startTime').execute().get('items', []) first_event = True for event in events: start = parser.parse(event['start'].get('dateTime', event['start'].get('date'))) if start.timestamp() - time.time() < 900 and event['description'][0] != '*': with suppress(Exception): split_index = event['description'].find(';') wug_members_str, stream_link = event['description'][:split_index], event['description'][split_index + 1:] wug_members = wug_members_str.split(',') if (link_start := stream_link.find('<a')) > 0: stream_link = stream_link[:link_start] elif stream_link.startswith('<a'): stream_link = BeautifulSoup(stream_link, 'html.parser').find('a').contents[0] colour = hkd.get_oshi_colour(hkd.get_wug_guild(self.bot.guilds), wug_members[0]) if len(wug_members) == 1 else Colour.teal() embed_fields = [] embed_fields.append(('Time', '{0:%Y}-{0:%m}-{0:%d} {0:%H}:{0:%M} JST'.format(start.astimezone(pytz.timezone('Japan'))))) embed_fields.append(('WUG Members', ', '.join(wug_members))) content = '**Starting in 15 Minutes**' if first_event else '' await channel.send(content=content, embed=hkd.create_embed(title=event['summary'], colour=colour, url=stream_link, fields=embed_fields)) first_event = False event['description'] = '*' + event['description'] self.calendar.events().update(calendarId='primary', eventId=event['id'], body=event).execute()
async def check_instagram_stories(self): channel = hkd.get_updates_channel(self.bot.guilds) with suppress(Exception): instaloader_args = ['instaloader', '--login={0}'.format(self.config['instagram_user']), '--sessionfile=./.instaloader-session', '--quiet', '--dirname-pattern={profile}', '--filename-pattern={profile}_{mediaid}', ':stories'] proc = subprocess.Popen(args=instaloader_args) while proc.poll() is None: await asyncio.sleep(1) for instagram_id in self.firebase_ref.child('last_instagram_stories').get().keys(): if not os.path.isdir(instagram_id): continue story_videos = [v for v in os.listdir(instagram_id) if v.endswith('.mp4')] last_story_id = int(self.firebase_ref.child('last_instagram_stories/{0}'.format(instagram_id)).get()) uploaded_story_ids = [] stories_to_upload = [] for vid in story_videos: video_id = int(vid[:-4].split('_')[-1]) if video_id > last_story_id: stories_to_upload.append(vid) uploaded_story_ids.append(video_id) story_pics = [p for p in os.listdir(instagram_id) if p.endswith('.jpg')] for pic in story_pics: pic_id = int(pic[:-4].split('_')[-1]) if pic_id > last_story_id and pic_id not in uploaded_story_ids: stories_to_upload.append(pic) uploaded_story_ids.append(pic_id) if uploaded_story_ids: profile = Profile.from_username(self.insta_api.context, instagram_id) user_name = profile.full_name if instagram_id in hkd.WUG_INSTAGRAM_IDS.values(): colour = hkd.get_oshi_colour(hkd.get_wug_guild(self.bot.guilds), hkd.dict_reverse(hkd.WUG_INSTAGRAM_IDS)[instagram_id]) else: colour = Colour(0x242424) author = {} author['name'] = '{0} (@{1})'.format(user_name, instagram_id) author['url'] = 'https://www.instagram.com/{0}/'.format(instagram_id) author['icon_url'] = profile.profile_pic_url story_link = 'https://www.instagram.com/stories/{0}/'.format(instagram_id) first_upload = True for story in sorted(stories_to_upload): if first_upload: await channel.send(embed=hkd.create_embed(author=author, title='Instagram Story Updated by {0}'.format(user_name), colour=colour, url=story_link)) first_upload = False await channel.send(file=File('./{0}/{1}'.format(instagram_id, story))) if uploaded_story_ids: self.firebase_ref.child('last_instagram_stories/{0}'.format(instagram_id)).set(str(max(uploaded_story_ids))) self.check_instagram.change_interval(minutes=random.randint(30, 50))
async def check_tweets(self): channel = hkd.get_updates_channel(self.bot.guilds) with suppress(Exception): twitter_user_ids = self.firebase_ref.child('last_userid_tweets').get().keys() for user_id_str in twitter_user_ids: user_id = int(user_id_str) last_tweet_id = int(self.firebase_ref.child('last_userid_tweets/{0}'.format(user_id)).get()) posted_tweets = [] tweets = self.twitter_api.GetUserTimeline(user_id=user_id, since_id=last_tweet_id, count=40, include_rts=False) for tweet in reversed(tweets): user = tweet.user name = user.name username = user.screen_name if tweet.in_reply_to_user_id and str(tweet.in_reply_to_user_id) not in twitter_user_ids: continue await asyncio.sleep(0.5) tweet_id = tweet.id posted_tweets.append(tweet_id) tweet_content = unescape(tweet.full_text) if user_id in hkd.WUG_TWITTER_IDS.values(): colour = hkd.get_oshi_colour(hkd.get_wug_guild(self.bot.guilds), hkd.dict_reverse(hkd.WUG_TWITTER_IDS)[user_id]) else: colour = Colour(0x242424) author = {} author['name'] = '{0} (@{1})'.format(name, username) author['url'] = 'https://twitter.com/{0}'.format(username) author['icon_url'] = user.profile_image_url_https image = '' if (expanded_urls := tweet.urls) and (expanded_url := expanded_urls[0].expanded_url) and hkd.is_blog_post(expanded_url): soup = hkd.get_html_from_url(expanded_url) blog_entry = soup.find(attrs={'class': 'skin-entryBody'}) if blog_images := [p['src'] for p in blog_entry.find_all('img') if '?caw=' in p['src'][-9:]]: image = blog_images[0] if media := tweet.media: image = media[0].media_url_https await channel.send(embed=hkd.create_embed(author=author, title='Tweet by {0}'.format(name), description=tweet_content, colour=colour, url='https://twitter.com/{0}/status/{1}'.format(username, tweet_id), image=image))
async def on_member_join(self, member): if member.guild == hkd.get_wug_guild(self.bot.guilds): for pattern in hkd.BANNED_USER_PATTERNS: if pattern.lower() in member.name.lower(): await member.ban(delete_message_days=1) return