def main(): current_time = datetime.datetime.now().strftime('%d/%m/%Y %H:%M') info = [] try: stats, my_state_stat = scrape_from_url() past_data = load() cur_data = {x[1]: {current_time: x[2:]} for x in stats} changed = find_change(cur_data, past_data, current_time) events_info = '' for event in info: events_info += '\n - ' + event.replace("'", "") # changed = True if changed: for state in cur_data: past_data[state]['latest'] = cur_data[state][current_time] past_data[state][current_time] = cur_data[state][current_time] save(past_data) table = tabulate(stats, headers=my_constants.TABLE_HEADERS, tablefmt='psql') table2 = tabulate(my_state_stat, headers=my_constants.TABLE_HEADERS, tablefmt='psql') slack_text = f'`{my_constants.STATE}`(Your State/UT) details:\n```{table2}```' slacker()(slack_text) slack_text = f'Please find Coronavirus stats for all Indian States/UT below:\n{events_info}\n ```{table}```' slacker()(slack_text) except Exception as e: slacker()(f'Exception occurred: [{e}]')
def main_jokes(): def yoMamaJokeApi(): res = requests.get('https://api.yomomma.info/') # soup = BeautifulSoup(res.text, 'html.parser') joke = json.loads(res.text) yo_mama_joke = joke['joke'] save_json_mama_joke(joke) mycol = my_db["yo_mama_jokes"] mydict = {'joke': joke['joke']} x = mycol.insert_one(mydict) logging.warning('Joke added to DB - Yo Mama') return yo_mama_joke def chuckNorrisJokeApi(self): res1 = requests.get('http://api.icndb.com/jokes/random') # soup1 = BeautifulSoup(res1.text, 'html.parser') joke1 = json.loads(res1.text) chuck_norris_joke = joke1['value']['joke'] mycol = my_db["chuck_norris_joke"] mydict = {'joke': joke1['value']['joke']} x = mycol.insert_one(mydict) logging.warning('Joke added to DB - Chuck Norris') save_json_chuck_norris_joke(joke1['value']) return chuck_norris_joke yo_mama_joke = yoMamaJokeApi() chuck_norris_joke = chuckNorrisJokeApi(self) now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print('Sent jokes to the Slack App - ', dt_string) slacker_file() logging.warning('Attachment has been sent to slack') slack_text = f'Sending 2 Jokes to make your day more cheerful and full of happiness -\n\nJoke 1 - \n{yo_mama_joke}.\nJoke 2 -\n{chuck_norris_joke}. ' slacker()(slack_text) logging.warning('Jokes sent to slack channel #jokes') logging.warning(yo_mama_joke + ", " + chuck_norris_joke)
info.append( f'NEW_STATE {state} got corona virus: {cur_data[state][current_time]}' ) past_data[state] = {} changed = True else: past = past_data[state]['latest'] cur = cur_data[state][current_time] if past != cur: changed = True info.append(f'Change for {state}: {past}->{cur}') events_info = '' for event in info: logging.warning(event) events_info += '\n - ' + event.replace("'", "") if changed: # override the latest one now for state in cur_data: past_data[state]['latest'] = cur_data[state][current_time] past_data[state][current_time] = cur_data[state][current_time] save(past_data) table = tabulate(stats, headers=SHORT_HEADERS, tablefmt='psql') slack_text = f'Please find CoronaVirus Summary for India below:\n{events_info}\n```{table}```' slacker()(slack_text) except Exception as e: logging.exception('oops, corono script failed.') slacker()(f'Exception occured: [{e}]')
save_json_chuck_norris_joke(joke1['value']) return chuck_norris_joke yo_mama_joke = yoMamaJokeApi() chuck_norris_joke = chuckNorrisJokeApi(self) now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print('Sent jokes to the Slack App - ', dt_string) slacker_file() logging.warning('Attachment has been sent to slack') slack_text = f'Sending 2 Jokes to make your day more cheerful and full of happiness -\n\nJoke 1 - \n{yo_mama_joke}.\nJoke 2 -\n{chuck_norris_joke}. ' slacker()(slack_text) logging.warning('Jokes sent to slack channel #jokes') logging.warning(yo_mama_joke + ", " + chuck_norris_joke) main_jokes() except Exception as e: slacker()(f'Exception occurred: [{e}]') logging.error(e) print(f'Exception occurred as {e}') schedule.every().day.at("20:00").do(main_jokes) # send jokes at night 8 schedule.every().day.at("08:00").do(main_jokes) # send jokes at morning 8 while 1: # Checks whether a scheduled task # is pending to run or not # print("Current time ran job at - ",time.time()) schedule.run_pending() time.sleep(1)
def get_news_page(self, page_no): try: res = requests.get('https://news.ycombinator.com/news?p=' + str(page_no)) soup = BeautifulSoup(res.text, 'html.parser') links = soup.select('.storylink') subtext = soup.select('.subtext') if len(subtext) > 0: def create_custom_hackernews(links, subtext): for innx, item in enumerate(links): title = links[innx].getText( ) # getText() will help in getting the title of the link href = links[innx].get('href', None) vote = subtext[innx].select('.score') if len(vote): points = int(vote[0].getText().replace(' points', '')) if points > 99: hn.append({ 'title': title, 'link': href, 'votes': points }) return sort_stories_by_votes(hn) (create_custom_hackernews(links, subtext)) page_no = page_no + 1 get_news_page(self, page_no) else: # print(len(hn)) new_dict = {} for item in hn: title = item['title'] new_dict[title] = item # print(new_dict[title]['title']) now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print('Sent news to the Slack App - ', dt_string) table = tabulate(new_dict.values(), tablefmt='grid') # print(table) FILE_TABLE_NAME = 'final_news_list.txt' with open(FILE_TABLE_NAME, 'w') as file: file.write(table) slacker_file() logging.warning('Attachment has been sent to slack') print('\n') # slack_text = f'Please find News From Hacker-News below:{table}' # slacker()(slack_text) # table = tabulate(hn) # print(table) return except Exception as e: slacker()(f'Exception occurred: [{e}]') logging.error(e) print(f'Exception Occurred :{e}')
#!/usr/bin/env python2 import requests from bs4 import BeautifulSoup import pandas as pd from tabulate import tabulate from slack_client import slacker slacker()("Im running!!!") url = 'https://www.mohfw.gov.in/' response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') rows = soup.findAll('tr') slacker()("Got result from website") df = pd.DataFrame(columns=[ 'S.No.', 'State/UT', 'ConfirmedIndia', 'ConfirmedForeign', 'Recovered', 'Died' ]) i = 0 for row in rows[1:-1]: columns = row.find_all('td') df_row = [col.text for col in columns] df.loc[i] = df_row i = i + 1 slacker()("DF ready") oldDF = pd.read_json('/home/sanchit/Desktop/Sanchit/slack-bot/Data.json',
parser.add_argument('--states', default=',') args = parser.parse_args() interested_states = args.states.split(',') current_time = datetime.datetime.now().strftime('%d/%m/%Y %H:%M') info = [] res = requests.get('https://www.mohfw.gov.in/') soup = BeautifulSoup(res.content, 'html.parser') table = soup.find(class_='data-table') #print(table.prettify()) rows = soup.find_all('tr') #print(rows) stats = [] for row in rows: stat = extract_contents(row.find_all('td')) if stat: if len(stat) < 4: stat = [] elif len(stat) == 4: stat = [' ', 'Total India', stat[1], stat[2], stat[3]] stats.append(stat) elif any([s.lower() in stat[1].lower() for s in interested_states]): stats.append(stat) mytable = tabulate(stats, headers=SHORT_HEADERS, tablefmt='psql') slack_text = f'Please find CoronaVirus Summary for India below:\n```{mytable}```' slacker()(slack_text)
def main_work(): info = [] res = requests.get('https://www.mohfw.gov.in/') soup = BeautifulSoup(res.text, 'html.parser') stats = [] try: my_data_str = '' last_data_updated = soup.find_all('strong')[0].get_text() last_data_updated = last_data_updated.replace("*", "") last_data_updated = last_data_updated.replace("awaited", "updated") for tr in soup.find_all('tbody')[9].find_all('tr'): my_data_str += tr.get_text() my_data_str = my_data_str[1:] stateList = my_data_str.split("\n\n") name_state_list = [] # current data sum_ind, sum_foreign, sum_cured, sum_death, total_ind, total_foreign, total_cured, total_death = 0, 0, 0, \ 0, 0, 0, \ 0, 0 for state in stateList[0:len(stateList) - 5]: data_list = state.split('\n') state_id, state_name, total_ind, total_foreign, total_cured, total_death = data_list[0], data_list[1], \ data_list[2], data_list[3], \ data_list[4], data_list[5] name_state_list.append(data_list[1]) sum_ind += int(total_ind) sum_foreign += int(total_foreign) sum_cured += int(total_cured) sum_death += int(total_death) stats.append([ state_id, state_name, total_ind, total_foreign, total_cured, total_death ]) stats.append([ '', 'Total Number of Confirmed Cases in India', sum_ind, sum_foreign, sum_cured, sum_death ]) name_state_list.append('Total Number of Confirmed Cases in India') past_data = load() # past data count = 0 for item in past_data: if item['Data']['Name'] not in name_state_list: name = item['Data']['Name'] print(f'New State - {name} got corona virus.') else: past_ind, past_for, past_cur, past_death = item['Data']['Indian'], item['Data']['Foreign'], \ item['Data']['Cured'], item['Data']['Death'] cur_ind, cur_for, cur_cur, cur_death = stats[count][2], stats[count][3], stats[count][4], \ stats[count][5] state_name = item['Data']['Name'] past_str, cur_str = '', '' if past_ind != cur_ind: past_str += str(past_ind) + ',' cur_str += str(cur_ind) + ',' if past_for != cur_for: past_str += str(past_for) + ',' cur_str += str(cur_for) + ',' if past_cur != cur_cur: past_str += str(past_cur) + ',' cur_str += str(cur_cur) + ',' if past_death != cur_death: past_str += str(past_death) cur_str += str(cur_death) if len(past_str) > 0 and len(cur_str) > 0: info.append( f'Change for {state_name}: [{past_str}]->[{cur_str}]' ) count = count + 1 save_json(stats) table = tabulate(stats, headers=SHORT_HEADERS, tablefmt='psql') # print(table1) events_info = '' for event in info: logging.warning(event) events_info += '\n - ' + event.replace("'", "") now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print('Sent corona-virus update to the Slack App - ', dt_string) slack_text = f'Please find CoronaVirus Summary for India below:\n{last_data_updated}\n{events_info}\n```{table}```' slacker()(slack_text) print('Sent corona-virus update to the Telegram App - ', dt_string) send_notification(slack_text) except Exception as err: slacker()(f'Exception occurred: [{err}]') send_notification(f'Exception occurred: [{err}]') print(f'Exception occurred {err}')