-
Notifications
You must be signed in to change notification settings - Fork 0
/
launches.py
105 lines (76 loc) · 3.17 KB
/
launches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import argparse
from typing import NewType, Tuple, Iterable
import urllib3
from bs4 import BeautifulSoup
"""
This module contains code to bring back info about rocket launches.
You can have this display on your Macbook Pro Touchbar using BetterTouchTool > TouchBar > Add Widget > Run Applescript > code like this:
return do shell script "/Users/will/miniconda3/envs/TEST/bin/python /Users/will/PycharmProjects/launches/launches.py --rocket 'Falcon 9' --alias 'F9'"
"""
# to suppress the warning: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised
urllib3.disable_warnings()
month = dict(
January='Jan',
February='Feb',
March='Mar',
April='Apr',
May='May',
June='Jun',
July='Jul',
August='Aug',
September='Sep',
October='Oct',
November='Nov',
December='Dec',
)
Launch_Name = NewType('Launch_Name', str)
Launch_Date = NewType('Launch_Date', str)
Launch = NewType('Launch', Tuple[Launch_Date, Launch_Name])
def launches() -> Iterable[Launch]:
url = 'http://spaceflightnow.com/launch-schedule/'
http = urllib3.PoolManager()
response = http.request('GET', url)
soup = BeautifulSoup(response.data, 'lxml')
missions = soup.findAll('div', attrs={'class': 'datename'})
spacex_missions = [(list(m.children)[0].text, list(m.children)[1].text) for m in missions]
clean = [(d.replace("NET ", "").replace(".", ""), n.replace(u' \u2022', ":")) for d, n in spacex_missions]
return clean
def max_length(items):
return max(len(i) for i in items)
def next_launch(rocket: str = None) -> Launch:
for date, name in launches():
if rocket: # Are we filtering on rocket name?
if rocket in name:
return (date, name)
else: # no filter
return (date, name)
def print_next_launch_for_rocket(rocket: str, alias: str = None) -> None:
"""
Print a string indicating the next launch for the given rocket.
:param rocket: If this string is contained in the mission name, we match.
:param alias: When printing results, this alias will be used instead of the rocket name, if supplied.
"""
if alias:
prefix = alias
else:
prefix = rocket
print_launch_date(launch=next_launch(rocket=rocket), prefix=prefix + ": ")
def print_launch_date(launch: Launch, prefix: str = ""):
date, name = launch
print("{}{}".format(prefix, date))
def print_launches():
space_for_date = 3 + max_length(d for d, n in launches())
template = "{: <" + str(space_for_date) + "} {}"
for date, name in launches():
print(template.format(date, name))
def print_next_launch_date():
print_launch_date(launch=next_launch())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Find out about upcoming rocket launches.')
parser.add_argument('--rocket', help='Get next launch for the specified rocket.')
parser.add_argument('--alias', help='Use this name for the rocket when printing results.')
args = vars(parser.parse_args())
if args['rocket']:
print_next_launch_for_rocket(rocket=args['rocket'], alias=args['alias'])
else:
print_launches()