dragon-bot/app/river_stats.py

41 lines
1.1 KiB
Python
Executable File

import discord
from bs4 import BeautifulSoup
import requests
import threading
results = {}
def get_river_stats(river_id):
url = requests.get("https://waterdata.usgs.gov/usa/nwis/uv?" + river_id)
soup = BeautifulSoup(url.content, "lxml")
river_name = ' '.join(soup.find('h2').text.split()[2:])
table = soup.find('table', border=1, align='left')
title = table.find('caption').text.strip().split('--')[0].strip()
for rows in table.findAll('tr'):
flow_value = rows.findAll('td',{'class': 'highlight2'})
results[river_name] = flow_value[0].text
def get_stats():
river_ids = [
'06752260',
'07091200',
'06719505',
]
threads = []
for river in river_ids:
t = threading.Thread(target=get_river_stats, args=(river,))
threads.append(t)
t.start()
for x in threads:
x.join()
final_string = ''
embed = discord.Embed(description=":man_rowing_boat: Flows :man_rowing_boat:", color=0x428bca, type="rich")
embed.set_author(name="River stats")
for key, value in results.items():
embed.add_field(name=key, value=value, inline=False)
return embed