Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- encoding: utf-8 -*-
- # -*- coding: utf-8 -*-
- # -*- tabstop: 2 -*-
- from os import path as ph
- H = ph.expanduser('~') # Home dir
- hh = lambda s: s.replace(H, '~')
- from sys import stdout as sto
- _p = lambda _str: sto.write(hh(str(_str)))
- debug = (False, True)[0]
- def _d(_str):
- if debug: _p(_str)
- url_base_wdm_crv = r'https://www.worldometers.info/coronavirus/'
- view_countries = "Poland", "New Zealand", "USA", "MS Zaandam"
- from urllib.request import Request as ulrq, urlopen as urlo
- from urllib.parse import urlparse as urlp
- from lxml import html as lxHtml
- url_base_wdm_crv_p = urlp(url_base_wdm_crv)
- url_base_wdm_crv_path_l = len(url_base_wdm_crv_p.path)
- class URLparse():
- def __init__(it, url):
- purl = urlp(url)
- for _attr in dir(purl):
- r_atrr = getattr(purl, _attr)
- if not(callable(r_atrr)):
- try:
- setattr(it, _attr, r_atrr[:])
- except TypeError:
- setattr(it, _attr, r_atrr)
- get_no_query_url = lambda it: f"{it.scheme}://{it.netloc}{it.path}"
- class CoroWorldMeter:
- def __init__(mn):
- mn.go()
- def go(mn):
- response = None
- url_p = URLparse(f"{url_base_wdm_crv}")
- r_url = ulrq(url_p.get_no_query_url(), headers={'User-Agent': 'Coronzilla/5.0'})
- h_url = urlo(r_url)
- if h_url.code == 200:
- response = h_url.read()
- h_url.close()
- if response:
- html = response.decode('utf-8')
- tree = lxHtml.fromstring(html)
- for cntr in view_countries:
- cnt, act = mn.wordmeter_row(tree, cntr)
- if cnt:
- _p(f"{cntr} total: {cnt}, active: {act}\n")
- def wordmeter_row(mn, tree, country):
- rowElement = tree.xpath(f'//table[@id="main_table_countries_today"]/tbody[1]/tr/td//text()[contains(.,"{country}")]/..')
- if len(rowElement):
- rowElement = rowElement[0]
- if hasattr(rowElement, 'tag'):
- tag = rowElement.tag.lower()
- _d(f"Tag:{tag}\n")
- while tag!='tr':
- if hasattr(rowElement, 'getparent'):
- rowElement = rowElement.getparent()
- else:
- _p(f"Strange situation, ")
- if hasattr(rowElement, 'tag'):
- tag = rowElement.tag.lower()
- _p(f"tag:{tag} wthout parent…\n")
- else:
- _p(f"no tag, no parent…\n")
- break
- if hasattr(rowElement, 'tag'):
- tag = rowElement.tag.lower()
- _d(f"Tag:{tag}\n")
- if tag=='tr':
- cntry_stats = tuple(map(lambda s: s.text if s.text else '0', rowElement.getchildren()))
- _d(f"Childs:\n")
- for idx, txt in enumerate(cntry_stats):
- _d(f"\t[{idx}]{txt}\n")
- CoronaTotal = cntry_stats[2]
- CoronaActive = cntry_stats[8]
- return CoronaTotal.replace(',', ' '), CoronaActive.replace(',', ' ')
- return '', ''
- # Entry point
- if __name__ == "__main__":
- CoroWorldMeter()
Add Comment
Please, Sign In to add comment