dragon-bot/app/river_stats.py

40 lines
1.1 KiB
Python

import discord
from bs4 import BeautifulSoup
import requests
import threading
results = {}
def get_river_stats(river_url):
url = requests.get(river_url)
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():
rivers = [
'https://waterdata.usgs.gov/usa/nwis/uv?06752260',
'https://waterdata.usgs.gov/co/nwis/uv/?site_no=07091200&PARAmeter_cd=00065,00060',
'https://waterdata.usgs.gov/co/nwis/uv/?site_no=06719505&PARAmeter_cd=00065,00060',
]
threads = []
for river in rivers:
t = threading.Thread(target=get_river_stats, args=(river,))
threads.append(t)
t.start()
for x in threads:
x.join()
final_string = ''
for key, value in results.items():
final_string += "\n" + key + ": " + value
return "```" + final_string + "```"