def get_pcrd_news(): global current_title global timer_interval # File Input f = open('pcrd_titles.txt','r+', encoding="utf-8") readTitles = f.readlines() writeTitles = readTitles # Crawler r = requests.get("http://www.princessconnect.so-net.tw/news") soup = BeautifulSoup(r.text, 'html.parser') divObjects = soup.find_all("dd") dtObjects = soup.find_all("dt") isUpdated = False count = 9 for div in reversed(divObjects): title = div.findAll("a", recursive=False)[0] event_type = dtObjects[count].findAll("span", recursive=False)[0].get_text() # tag color tag_color = 16077457 if event_type == '活動': tag_color = 3775462 elif event_type == '系統': tag_color = 10512325 current_title = title['title'] find_news = False for line in readTitles: if current_title in line: find_news = True break if find_news == False: writeTitles.insert(0, current_title + '\n') current_link = title['href'] r = requests.get(base_Url + current_link) soup = BeautifulSoup(r.text, 'html.parser') section = soup.select('body > main > article > article > section > p') content = '' for e in section: content += e.get_text('\n', '<br/>') content = content.replace('【', '\n**【') content = content.replace('】', '】**') content = (content[:1850] + ' ......') if len(content) > 1850 else content news_link = urllib.parse.urljoin(base_Url, current_link) embed = DiscordEmbed() embed.set_author(name='超異域公主連結☆Re:Dive', url='https://www.facebook.com/SonetPCR/', icon_url='http://www.princessconnect.so-net.tw/images/pc-icon.png') embed.title = event_type + ':' + current_title embed.description = content embed.add_embed_field(name='官網連結', value=news_link) embed.color = tag_color for link in webhook_links: new_embed = embed webhook = DiscordWebhook(url=link) webhook.add_embed(new_embed) if "外掛停權" in current_title: break webhook.execute() #print("已發布:" + current_title) # print(embed.title) # print(embed.description) print("已更新:" + current_title) isUpdated = True else: print("未更新:" + current_title) count -= 1 while len(writeTitles) > 20: writeTitles.pop() if isUpdated: print("已儲存檔案") f.seek(0) f.truncate(0) f.writelines(writeTitles) f.close()
def post_discord( rolling, old_rolling, case_data, previous_case_data, date, dashboard_url, graph ): global WEBHOOKS global PSA global QUIET if WEBHOOKS is None: return print("Skipping posting to discord as no webhooks supplied") positive_thumbnails = [ "https://www.continentalmessage.com/wp-content/uploads/2015/09/123rf-alert2.jpg", "https://i.kym-cdn.com/photos/images/newsfeed/000/675/645/2c7.gif", "https://media.discordapp.net/attachments/783375197604413445/790625854202839100/image0.png", ] negative_thumbnails = [ "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/clans/5671259/7923c9b8e0a5799d4d422208b31f5ca0f4f49067.png", "https://static01.nyt.com/images/2020/01/28/science/28VIRUS-BATS1/28VIRUS-BATS1-videoSixteenByNineJumbo1600.jpg", "https://media.tenor.com/images/6603c0a47ff16ad8d3682e481e727f76/tenor.gif", "https://ih1.redbubble.net/image.1877589148.0162/ur,mask_flatlay_front,wide_portrait,750x1000.jpg", "https://media.giphy.com/media/KHEgvyrgYnL9RW08h6/giphy.gif", "https://media.giphy.com/media/WS0MDT0DITCTLwcNNx/giphy.gif", ] emojis = ["❤️", "✨", "🥓", "🍺", "🧻", "🐍", "☃️", "😷"] if QUIET and case_data[0] == 0: return embed = DiscordEmbed() if case_data[0] > 0: embed.set_color(15158332) embed.set_thumbnail(url=choice(positive_thumbnails)) else: embed.set_color(3066993) embed.set_thumbnail(url=choice(negative_thumbnails)) if PSA is not None: embed.add_embed_field(name="ANNOUNCEMENT", value=PSA, inline=False) embed.color = 15844367 embed.add_embed_field( name="New Positive Tests", value=f"{case_data[0]}", inline=False, ) embed.add_embed_field( name="Positive Tests (7 days)", value=case_value_to_string(case_data, previous_case_data, 1), inline=False, ) embed.add_embed_field( name="Positive Tests (14 days)", value=case_value_to_string([rolling], [old_rolling], 0), inline=False, ) embed.add_embed_field( name="Weekly Test Count", value=case_value_to_string(case_data, previous_case_data, 3), inline=False, ) if case_data[1] != 0: # Calculate weekly positivity rate pcr = (case_data[1] / case_data[3]) * 100 embed.add_embed_field(name="Weekly Positivity Rate", value=f"{round(pcr, 4)}%") embed.add_embed_field( name="Total Positive Tests", value=case_value_to_string(case_data, previous_case_data, 2), ) embed.add_embed_field( name="Total Tests", value=case_value_to_string(case_data, previous_case_data, 4), ) embed.set_author( name="Click for dashboard", url=dashboard_url, icon_url="https://i.redd.it/14nqzc0hswy31.png", ) embed.set_footer( text=f"{date}\nMade with {choice(emojis)} - https://github.com/johnnyapol/RPICovidScraper {get_git_hash()}" ) hook = DiscordWebhook( url=WEBHOOKS, content=choice( [ "The RPI Covid Dashboard has been updated!", "I got yer COVID cases right here!", "Special delivery!", "Beep beep boop", "I found some data!", ] ), username="******", avatar_url="https://www.minnpost.com/wp-content/uploads/2020/03/coronavirusCDC640.png", ) hook.add_file(file=graph.read(), filename="graph.png") embed.set_image(url="attachment://graph.png") hook.add_embed(embed) hook.execute()
def get_pcrd_news(): global current_title global timer_interval now = datetime.now() time = now.strftime("%Y/%m/%d %H:%M:%S") # File Input f = open('pcrd_titles.txt','r+', encoding="utf-8") readTitles = f.readlines() writeTitles = readTitles # Crawler r = requests.get("http://www.princessconnect.so-net.tw/news") soup = BeautifulSoup(r.text, 'html.parser') divObjects = soup.find_all("dd") dtObjects = soup.find_all("dt") r = requests.get("http://www.princessconnect.so-net.tw/news?page=2") # Page 2 soup = BeautifulSoup(r.text, 'html.parser') divObjects += soup.find_all("dd") dtObjects += soup.find_all("dt") isUpdated = False for i in range(len(divObjects)): title = list(reversed(divObjects))[i].findAll("a", recursive=False)[0] event_type = list(reversed(dtObjects))[i].findAll("span", recursive=False)[0].get_text() date = list(reversed(dtObjects))[i].text # tag color if event_type == '更新': tag_color = 16077457 elif event_type == '系統': tag_color = 10512325 else: tag_color = 3775462 current_title = title['title'] find_news = False if "外掛停權" in current_title: continue for line in readTitles: if current_title in line: find_news = True break if (find_news == False) and (now.strftime("%Y.%m.%d") in date): writeTitles.insert(0, current_title + '\n') current_link = title['href'] r = requests.get(base_Url + current_link) soup = BeautifulSoup(r.text, 'html.parser') news_link = urllib.parse.urljoin(base_Url, current_link) section = soup.select('body > main > article > article > section > p') content = "" for e in section: for x in e.find_all(): if len(x.get_text(strip=True)) == 0 and x.name != 'br': x.extract() for div in e.find_all('div'): div.unwrap() brtag = str(e).replace("<br/>", "\n") multiblank = re.sub("([ \t]*\n){3,}", "\n\n", brtag) content += BeautifulSoup(multiblank, "html.parser").get_text() content = content.replace('*', '×') content = (content[:550] + ' ......\n[詳細內容](' + news_link + ')') if len(content) > 550 else content embed = DiscordEmbed() embed.set_author(name='超異域公主連結☆Re:Dive', icon_url='http://www.princessconnect.so-net.tw/images/pc-icon.png') embed.title = current_title embed.url = news_link embed.description = content embed.color = tag_color for link in webhook_links: new_embed = embed webhook = DiscordWebhook(url=link) webhook.add_embed(new_embed) webhook.execute() print(f'[{time}] - 已更新:{current_title}') isUpdated = True else: print(f'[{time}] - 未更新:{current_title}') while len(writeTitles) > 20: writeTitles.pop() if isUpdated: print(f'[{time}] - 已儲存檔案') f.seek(0) f.truncate(0) f.writelines(writeTitles) f.close()
def post_discord(rolling, old_rolling, case_data, previous_case_data, date, dashboard_url, graph): global WEBHOOKS global PSA global QUIET if WEBHOOKS is None: return print("Skipping posting to discord as no webhooks supplied") positive_thumbnails = [ "https://www.continentalmessage.com/wp-content/uploads/2015/09/123rf-alert2.jpg", "https://i.kym-cdn.com/photos/images/newsfeed/000/675/645/2c7.gif", "https://media.discordapp.net/attachments/783375197604413445/790625854202839100/image0.png", "https://media.tenor.com/images/6603c0a47ff16ad8d3682e481e727f76/tenor.gif", ] neutral_thumbnails = [ "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/clans/5671259/7923c9b8e0a5799d4d422208b31f5ca0f4f49067.png", "https://static01.nyt.com/images/2020/01/28/science/28VIRUS-BATS1/28VIRUS-BATS1-videoSixteenByNineJumbo1600.jpg", "https://ih1.redbubble.net/image.1877589148.0162/ur,mask_flatlay_front,wide_portrait,750x1000.jpg", "https://media.giphy.com/media/KHEgvyrgYnL9RW08h6/giphy.gif", ] negative_thumbnails = [ "https://media.giphy.com/media/WS0MDT0DITCTLwcNNx/giphy.gif", "https://cdn.vox-cdn.com/thumbor/iuL4QWaANcy5lyeCDXxIrBq7_uQ=/0x0:3000x2000/1400x1050/filters:focal(1436x422:1916x902):no_upscale()/cdn.vox-cdn.com/uploads/chorus_image/image/68718659/AP_20331457642255.0.jpg", ] emojis = ["❤️", "🍺", "🐱💻", "😷", "🏖️", "🦠", "🍉"] if QUIET and case_data[0] == 0: return embed = DiscordEmbed() if case_data[0] > 4: embed.set_color(15158332) embed.set_thumbnail(url=choice(positive_thumbnails)) elif case_data[0] > 0: embed.set_color(0xFFFF00) embed.set_thumbnail(url=choice(neutral_thumbnails)) else: embed.set_color(3066993) embed.set_thumbnail(url=choice(negative_thumbnails)) if PSA is not None: embed.add_embed_field(name="ANNOUNCEMENT", value=PSA, inline=False) embed.color = 15844367 embed.add_embed_field( name="New Positive Tests", value=f"{case_data[0]}", inline=False, ) embed.add_embed_field( name="Positive Tests (7 days)", value=case_value_to_string(case_data, previous_case_data, 1), inline=False, ) embed.add_embed_field( name="Positive Tests (14 days)", value=case_value_to_string([rolling], [old_rolling], 0), inline=False, ) embed.add_embed_field( name="Weekly Test Count", value=case_value_to_string(case_data, previous_case_data, 3), inline=False, ) if case_data[1] != 0: # Calculate weekly positivity rate pcr = (case_data[1] / case_data[3]) * 100 embed.add_embed_field(name="Weekly Positivity Rate", value=f"{round(pcr, 4)}%") embed.add_embed_field( name="Total Positive Tests", value=case_value_to_string(case_data, previous_case_data, 2), ) # Since discord footers don't support "rich" content, hack on a footer to the last field date = "".join(date.split("\n")) embed.add_embed_field( name="Total Tests", value= f"{case_value_to_string(case_data, previous_case_data, 4)}\n{date} Made with {choice(emojis)} - [source]({get_source_url()})", inline=False, ) embed.set_author( name="Click for dashboard", url=dashboard_url, icon_url="https://i.redd.it/14nqzc0hswy31.png", ) hook = DiscordWebhook( url=WEBHOOKS, content=choice([ "The RPI Covid Dashboard has been updated!", "I got yer COVID data right here!", "Special delivery!", "Beep beep boop", "I found some data!", ]), username="******", avatar_url= "https://www.minnpost.com/wp-content/uploads/2020/03/coronavirusCDC640.png", ) if graph != None: hook.add_file(file=graph.read(), filename="graph.png") embed.set_image(url="attachment://graph.png") hook.add_embed(embed) hook.execute()