272 lines
6.4 KiB
Python
272 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from . import __version__
|
|
import argparse
|
|
import requests
|
|
|
|
|
|
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 lang:
|
|
headers["Accept-Language"] = lang
|
|
|
|
if location:
|
|
link = link + f"{location}"
|
|
|
|
if format:
|
|
link = link + f"{format}"
|
|
|
|
return link, headers
|
|
|
|
|
|
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"
|
|
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!""",
|
|
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",
|
|
default=None,
|
|
type=str,
|
|
help="""Location (look at epilog for more information)"""
|
|
)
|
|
|
|
parser.add_argument(
|
|
"-f", "--format",
|
|
default=None,
|
|
type=str,
|
|
help="Query formatting"
|
|
)
|
|
|
|
parser.add_argument(
|
|
"-l", "--lang",
|
|
default=None,
|
|
type=str,
|
|
help="The language to use"
|
|
)
|
|
|
|
parser.add_argument(
|
|
"-m", "--metric",
|
|
default=False,
|
|
action='store_true',
|
|
help="Units: Metric (SI) (default outside US)"
|
|
)
|
|
|
|
parser.add_argument(
|
|
"-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()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|