From 02320b04a47f6149e41b860f43a18e7920405b40 Mon Sep 17 00:00:00 2001 From: Elia El Lazkani Date: Wed, 28 Aug 2019 21:43:31 +0200 Subject: [PATCH] Moving all the needed API calls to easily usable commands --- .gitignore | 3 + cmw/cmw.py | 243 +++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 220 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 2211c42..bd770f4 100644 --- a/.gitignore +++ b/.gitignore @@ -125,3 +125,6 @@ dmypy.json # VSCode .vscode/ + +# MacOS X +.DS_Store diff --git a/cmw/cmw.py b/cmw/cmw.py index 41dfe00..da56eb3 100644 --- a/cmw/cmw.py +++ b/cmw/cmw.py @@ -4,24 +4,54 @@ from . import __version__ import argparse import requests -def generate_link(url, location=None, lang=None, format=None, help=False): - link = f"https://{url}/" + +def generate_format(parser): + + alpha = "?" + png = False + switches = [] + + switches.append("m") if parser.metric else None + switches.append("u") if parser.uscs else None + switches.append("M") if parser.meter_second else None + switches.append("0") if parser.zero else None + switches.append("1") if parser.one else None + switches.append("2") if parser.two else None + switches.append("A") if parser.ignore_user_agent else None + switches.append("F") if parser.follow_link else None + switches.append("n") if parser.narrow else None + switches.append("q") if parser.quiet else None + switches.append("Q") if parser.super_quiet else None + switches.append("T") if parser.no_colors else None + switches.append("p") if parser.add_frame else None + switches.append("t") if parser.mid_transparency else None + + args = f"".join(switches) + + if parser.png: + alpha = "_" + png = True + + if parser.transparency: + args += f"_transparency={parser.transparency}" + + args += ".png" + + return f"{alpha}{args}", png + + +def generate_link(url, v2=None, location=None, lang=None, format=None): + link = f"https://{url}/" if not v2 else f"https://v2.{url}/" headers = {} - if help: - link = link + f"/:help" + if lang: + headers["Accept-Language"] = lang - else: - if lang: - headers = { - "Accept-Language": lang - } + if location: + link = link + f"{location}" - if location: - link = link + f"{location}" - - if format: - link = link + f"?format={format}" + if format: + link = link + f"{format}" return link, headers @@ -30,48 +60,209 @@ def get_weather(link, headers): result = requests.get(link, headers=headers) return result.text + +def download_weather(link, headers): + local_filename = link.split('/')[-1] + with requests.get(link, stream=True) as r: + r.raise_for_status() + with open(local_filename, 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + if chunk: + f.write(chunk) + f.flush() + return local_filename + + def main(): parser = argumentparser() url = "wttr.in" - link, headers = generate_link(url, parser.location, parser.lang, parser.format, parser.format_help) - weather = get_weather(link, headers) - print(weather) + download = False + if parser.format and not parser.format.endswith(".png"): + format = parser.format + else: + format, download = generate_format(parser) + + link, headers = generate_link(url, parser.v2, parser.location, parser.lang, format) + + if download: + weather = download_weather(link, headers) + print(f"Files saved: {weather}") + else: + weather = get_weather(link, headers) + print(weather) def argumentparser(): parser = argparse.ArgumentParser( - description="Get the weather!" + description="""Get the weather!""", + epilog=""" + Supported Location Types + ------------------------ + City name: Paris + Unicode name: Москва + Airport code (3 letters): muc + Domain name: @stackoverflow.com + Area code: 94107 + GPS coordinates: -78.46,106.79 + + Special Location + ---------------- + Moon phase (add ,+US + or ,+France + for these cities): moon + Moon phase for a date: moon@2016-10-25 + + Supported languages + ------------------- + + Supported: af da de el et fr fa hu id it nb nl pl pt-br ro ru tr uk vi + """, + formatter_class=argparse.RawTextHelpFormatter ) + parser.add_argument( - "-l", "--location", + "-L", "--location", default=None, type=str, - help="Location (e.g. NewYork)" + help="""Location (look at epilog for more information)""" ) + parser.add_argument( "-f", "--format", default=None, type=str, help="Query formatting" ) + parser.add_argument( - "--lang", + "-l", "--lang", default=None, type=str, help="The language to use" ) parser.add_argument( - "--format-help", + "-m", "--metric", default=False, action='store_true', - help="Get format help from upstream" + help="Units: Metric (SI) (default outside US)" ) parser.add_argument( - '--version', - action='version', - version=f'%(prog)s {__version__}') + "-u", "--uscs", + default=False, + action="store_true", + help="Units: USCS (default in US)" + ) + + parser.add_argument( + "-M", "--meter-second", + default=False, + action="store_true", + help="Units: Show wind speed in m/s" + ) + + parser.add_argument( + "-z", "--zero", + default=False, + action="store_true", + help="View: Only current weather" + ) + + parser.add_argument( + "-o", "--one", + default=False, + action="store_true", + help="View: Current weather & one day" + ) + + parser.add_argument( + "-w", "--two", + default=False, + action="store_true", + help="View: Current weather & two days" + ) + + parser.add_argument( + "-A", "--ignore-user-agent", + default=False, + action="store_true", + help="View: Force ANSI output format" + ) + + parser.add_argument( + "-F", "--follow-link", + default=True, + action="store_false", + help="View: Show the 'Follow' line from upstream" + ) + + parser.add_argument( + "-n", "--narrow", + default=False, + action="store_true", + help="View: Narrow version" + ) + + parser.add_argument( + "-q", "--quiet", + default=False, + action="store_true", + help="View: Quiet version" + ) + + parser.add_argument( + "-Q", "--super-quiet", + default=False, + action="store_true", + help="View: Super quiet version" + ) + + parser.add_argument( + "-N", "--no-colors", + default=False, + action="store_true", + help="View: Switch terminal sequences off" + ) + + parser.add_argument( + "-P", "--png", + default=False, + action="store_true", + help="PNG: Generate PNG file" + ) + + parser.add_argument( + "-p", "--add-frame", + default=False, + action="store_true", + help="PNG: Add frame around output" + ) + + parser.add_argument( + "-T", "--mid-transparency", + default=False, + action="store_true", + help="PNG: Make transparency 150" + ) + + parser.add_argument( + "-t", "--transparency", + type=int, + help="PNG: Set transparency between 0 and 255" + ) + + parser.add_argument( + "--v2", + default=False, + action="store_true", + help="v2 interface of the day" + ) + + parser.add_argument( + "--version", + action="version", + version=f"%(prog)s {__version__}") return parser.parse_args()