missing py module bits
This commit is contained in:
parent
c3d3643e7e
commit
52d50c642a
1
osrsscrape/__init__.py
Normal file
1
osrsscrape/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
__version__ = "0.0.0"
|
62
osrsscrape/cli.py
Normal file
62
osrsscrape/cli.py
Normal file
@ -0,0 +1,62 @@
|
||||
import sys
|
||||
import json
|
||||
import logging
|
||||
from urllib.parse import urlparse
|
||||
from influxdb import InfluxDBClient
|
||||
import click
|
||||
from osrsscrape.scrape import get_player
|
||||
|
||||
|
||||
def get_influx(influx_url):
|
||||
url = urlparse(influx_url)
|
||||
|
||||
db_name = url.path[1:]
|
||||
|
||||
influx = InfluxDBClient(url.hostname, url.port, url.username, url.password)
|
||||
influx.create_database(db_name)
|
||||
influx.switch_database(db_name)
|
||||
|
||||
return influx
|
||||
|
||||
|
||||
@click.command(help="tool for scraping osrs highscores")
|
||||
@click.argument('player_name', envvar='RSSCRAPE_PLAYER_NAME')
|
||||
@click.option('--influx-url', envvar='RSSCRAPE_INFLUX_URL', required=True, help="influxdb server url")
|
||||
@click.option('-a', '--alias', envvar='RSSCRAPE_PLAYER_ALIAS', help="value for player alias field instead of username")
|
||||
@click.option('--timeout', '-t', envvar='RSSCRAPE_TIMEOUT', type=int, default=60, help="scrape timeout")
|
||||
@click.option('--dry-run', '-n', is_flag=True, envvar='RSSCRAPE_DRYRUN', help='dry run mode')
|
||||
def main(player_name, alias, influx_url, timeout, dry_run):
|
||||
logging.basicConfig(level=logging.WARNING,
|
||||
format="%(asctime)-15s %(levelname)-8s %(filename)s:%(lineno)d %(message)s")
|
||||
influx = get_influx(influx_url)
|
||||
|
||||
player_name = player_name.lower()
|
||||
stats = get_player(player_name, timeout)
|
||||
|
||||
if stats is None:
|
||||
click.echo("failed to fetch player!")
|
||||
sys.exit(1)
|
||||
|
||||
body = []
|
||||
|
||||
for measurement in ("rank", "level", "xp", "xp_next"):
|
||||
fields = {}
|
||||
for stat in stats:
|
||||
fields[stat.skill] = getattr(stat, measurement)
|
||||
|
||||
blob = {"measurement": measurement,
|
||||
"tags": {"username": player_name},
|
||||
"fields": fields}
|
||||
|
||||
if alias:
|
||||
blob["tags"]["alias"] = alias
|
||||
|
||||
body.append(blob)
|
||||
|
||||
if dry_run:
|
||||
click.echo(json.dumps(body, indent=4))
|
||||
click.echo("not submitting points to influxdb due to dry run mode")
|
||||
else:
|
||||
influx.write_points(body)
|
||||
|
||||
click.echo("done!")
|
Loading…
x
Reference in New Issue
Block a user