-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchoochoogen.py
executable file
·273 lines (219 loc) · 9.94 KB
/
choochoogen.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python3
import random
from datetime import datetime, timedelta
import pytz
import astral
import xml.etree.ElementTree as ET
import requests
from astral import moon as amoon
from astral.sun import sun
ENGINES = ["🚂"]
CARS = ["🚃"]
SUN = "☀"
MOONS = ["🌑","🌒","🌔","🌕","🌖","🌘"]
DESERT_TILES = ["🌵","🌵","🌴","🌴","🐪","🐢","🐎"]
FOREST_TILES = ["🌲","🌲","🌲","🌲","🐇","🌳","🌳"]
BEACH_TILES = ["🌴","🌴","🍍","🐢","🗿","🐚"]
FIELD_TILES = ["🌾","🌾","🌾","🌻","🐍","🐈"]
WILDFLOWERS_TILES = ["🌼","🌺","🏵️","🌷","🌷","🐝","🦋"]
SEA_TILES =["🐬","🐳","🐙"]
HELL_TILES = ["🔥","👻","😈","💀"]
HEAVEN_TILES = ["📯👼","✨","🐕","👼"]
SPACE_TILES = ["👾","👽","💫","🚀","🛰"]
UNDERSEA_TILES = ["🐟","🐙","🐬","🐋"]
class Scene():
def __init__(self, mode, height = 4, item_rarity = 10, top_border = None, bottom_border = None):
self.mode = mode
self.height = height
self.item_rarity = item_rarity
self.top_border = top_border
self.bottom_border = bottom_border
self.sky = ""
self.landscape = []
self.train = self.pick_engine() + self.pick_body()
def pick_engine(self):
leading_spaces = random.randint(0,9)
self.engine = ""
for _ in range(leading_spaces):
self.engine += " "
self.engine += random.choice(ENGINES)
return self.engine
def pick_body(self):
self.body = ""
cars = random.randint(3,8)
for _ in range(cars):
self.body += random.choice(CARS)
return self.body
def add_clouds(self):
self.sky = self.sky.replace(SUN, "⛅ ")
for _ in range(len(self.sky) - 1):
if self.sky[_] == "\u2800" and random.randint(1,5) == 1:
self.sky = self.sky[:_] + "☁️" + self.sky[_ + 1:]
def get_weather(self):
cloud_terms = ["Mostly Cloudy", "Mostly Cloudy with Haze", "Mostly Cloudy and Breezy", "A Few Clouds", "A Few Clouds with Haze", "A Few Clouds and Breezy", "Partly Cloudy", "Partly Cloudy with Haze", "Partly Cloudy and Breezy", "Overcast", "Overcast with Haze", "Overcast and Breezy", "Fog/Mist", "Fog", "Freezing Fog", "Shallow Fog", "Partial Fog", "Patches of Fog", "Fog in Vicinity", "Freezing Fog in Vicinity", "Shallow Fog in Vicinity", "Partial Fog in Vicinity", "Patches of Fog in Vicinity", "Showers in Vicinity Fog", "Light Freezing Fog", "Heavy Freezing Fog"]
rain_terms = ["Rain Showers", "Light Rain Showers", "Light Rain and Breezy", "Heavy Rain Showers", "Rain Showers in Vicinity", "Light Showers Rain", "Heavy Showers Rain", "Showers Rain", "Showers Rain in Vicinity", "Rain Showers Fog/Mist", "Light Rain Showers Fog/Mist", "Heavy Rain Showers Fog/Mist", "Rain Showers in Vicinity Fog/Mist", "Light Showers Rain Fog/Mist", "Heavy Showers Rain Fog/Mist", "Showers Rain Fog/Mist", "Showers Rain in Vicinity Fog/Mist", "Light Rain", "Drizzle", "Light Drizzle", "Heavy Drizzle", "Light Rain Fog/Mist", "Drizzle Fog/Mist", "Light Drizzle Fog/Mist", "Heavy Drizzle Fog/Mist", "Light Rain Fog", "Drizzle Fog", "Light Drizzle Fog", "Heavy Drizzle Fog Rain", "Heavy Rain", "Rain Fog/Mist", "Heavy Rain Fog/Mist", "Rain Fog", "Heavy Rain Fog"]
try:
res = requests.get("https://forecast.weather.gov/xml/current_obs/KNYC.xml")
xml_tree = ET.fromstring(res.text)
weather = xml_tree.find('weather').text
if weather in cloud_terms:
self.add_clouds()
return self.sky
elif weather in rain_terms:
self.sky = self.fill_row(tileset = ["🌧️","🌧️","☁️"], item_rarity = 5)
return self.sky
elif "Thunderstorm" in weather:
self.sky = self.fill_row(tileset = ["🌧️","⛈️","⛈️"], item_rarity = 5)
return self.sky
elif "Snow" in weather:
self.sky = self.fill_row(tileset = ["🌨️","❄️"], item_rarity = 5)
return self.sky
else:
return None
except:
return None
def make_daysky(self):
day_length = self.sun['sunset'] - self.sun['sunrise']
day_so_far = self.dt - self.sun['sunrise']
sun_placement = 14 - int((day_so_far.seconds/day_length.seconds) * 15)
for _ in range(15):
if _ == sun_placement:
self.sky += SUN + "\uFE0F"
self.sky += "\u2800"
def make_nightsky(self):
tomorrow = self.dt + timedelta(days = 1)
yesterday = self.dt - timedelta(days = 1)
if self.dt > self.sun['sunset']:
moon_phase = int(amoon.phase(self.dt.date()))
night_length = (sun(self.loc.observer, tomorrow)['sunrise']
- sun(self.loc.observer)['sunset'])
night_so_far = self.dt - self.sun['sunset']
elif self.dt < self.sun['sunrise']:
moon_phase = int(amoon.phase(yesterday.date()))
night_length = (self.sun['sunrise']
- sun(self.loc.observer, yesterday)['sunset'])
night_so_far = self.dt - sun(self.loc.observer, yesterday)['sunset']
if moon_phase == 0:
moon = MOONS[0]
elif moon_phase <= 7:
moon = MOONS[1]
elif moon_phase < 14:
moon = MOONS[2]
elif moon_phase == 14:
moon = MOONS[3]
elif moon_phase <= 21:
moon = MOONS[4]
else:
moon = MOONS[5]
moon_placement = 14 - int((night_so_far.seconds/night_length.seconds) * 15)
for _ in range(moon_placement):
self.sky += "\u2800"
self.sky += moon + "\uFE0F"
def make_sky(self):
self.sky = ""
self.dt = pytz.timezone('America/New_York').localize(datetime.now())
self.loc = astral.LocationInfo(name='New York',
region='USA',
timezone='US/Eastern',
latitude=40.71666666666667,
longitude=-74.0)
self.sun = sun(self.loc.observer,
date=self.dt,
tzinfo=self.loc.timezone)
if self.dt >= self.sun["sunrise"] and self.dt <= self.sun["sunset"]:
self.make_daysky()
self.get_weather()
else:
self.make_nightsky()
return self.sky
def make_sea(self):
return self.fill_row(tileset = SEA_TILES, space_char = "🌊", length = 12)
def fill_row(self, tileset = None, item_rarity = None, space_char = " ", length = 20):
row = ""
if not tileset:
tileset = self.tileset
if not item_rarity:
item_rarity = self.item_rarity
for spot in range(length):
tile = random.randint(1, item_rarity)
if tile == 1:
row += random.choice(tileset)
else:
row += space_char
return row
def generate(self):
self.landscape = []
if self.top_border:
self.landscape.append(self.top_border)
else:
self.make_sky()
self.landscape.append(self.fill_row())
self.landscape.extend([self.fill_row(), self.fill_row()])
if self.bottom_border:
self.landscape.append(self.bottom_border)
else:
self.landscape.append(self.fill_row())
tweet = ""
if self.sky:
tweet += self.sky + "\n"
tweet += self.landscape[0] + "\n" + \
self.landscape[1] + "\n" + \
self.train + "\n" + \
self.landscape[2] + "\n" + \
self.landscape[3]
return tweet
class Desert(Scene):
def __init__(self):
super(Desert, self).__init__("desert")
self.tileset = DESERT_TILES
class Forest(Scene):
def __init__(self):
super(Forest, self).__init__("forest")
self.tileset = FOREST_TILES
class Field(Scene):
def __init__(self):
super(Field, self).__init__("field")
self.tileset = FIELD_TILES
class Wildflowers(Scene):
def __init__(self):
super(Wildflowers, self).__init__("wildflowers")
self.tileset = WILDFLOWERS_TILES
class Beach(Scene):
def __init__(self):
super(Beach, self).__init__("beach")
self.tileset = BEACH_TILES
self.bottom_border = self.make_sea()
class Space(Scene):
def __init__(self):
super(Space, self).__init__("space")
self.top_border = "⭐🌟⭐🌟⭐🌟⭐🌟⭐🌟⭐🌟"
self.bottom_border = "⭐🌟⭐🌟⭐🌟⭐🌟⭐🌟⭐🌟"
self.tileset = SPACE_TILES
class Hell(Scene):
def __init__(self):
super(Hell, self).__init__("hell")
self.top_border = "🔥👹🔥👹🔥👹🔥👹🔥👹🔥👹"
self.bottom_border = "🔥👹🔥👹🔥👹🔥👹🔥👹🔥👹"
self.tileset = HELL_TILES
class Heaven(Scene):
def __init__(self):
super(Heaven, self).__init__("heaven")
self.top_border = "☁👼☁👼☁👼☁👼☁👼☁👼"
self.bottom_border = "☁👼☁👼☁👼☁👼☁👼☁👼"
self.tileset = HEAVEN_TILES
class Undersea(Scene):
def __init__(self):
super(Undersea, self).__init__("undersea")
self.top_border = "🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊"
self.bottom_border = "🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊🌊"
self.tileset = UNDERSEA_TILES
def maketrain():
standard_scenes = [Desert, Beach, Forest, Field, Wildflowers]
special_scenes = [Space, Undersea, Heaven, Hell]
if random.randint(1,20) == 20:
scene = random.choice(special_scenes)()
else:
scene = random.choice(standard_scenes)()
return scene.generate()
if __name__ == "__main__":
maketrain()