def get_images_in_gfx(jpg_url): """Gets the 5 images, distributed in noncontiguous 5*n indexes""" gfx_url = jpg_url.replace("1", "2").replace("jpg", "gfx") gfx = read_url(gfx_url) for i in range(5): img = Image.open(BytesIO(bytes(gfx[i::5]))) yield image_to_text(img)
def get_next_jpg_images(jpg_url, from_to): """Gets next images by changing the number in the URL""" for i in range(*from_to): jpg_content = read_url(jpg_url.replace("1", f"{i}")) try: jpg = Image.open(BytesIO(jpg_content)) yield image_to_text(jpg, 10, 8) except UnidentifiedImageError: yield jpg_content.decode()
def load_pixels(img_url): """Returns a list of (x, y) coordinates of all pixels from all frames of the GIT at `img_url`""" image = Image.open(BytesIO(read_url(img_url))) pixels = [frame.getbbox()[:2] for frame in ImageSequence.Iterator(image)] min_x, min_y = min(p[0] for p in pixels), min(p[1] for p in pixels) max_x, max_y = max(p[0] for p in pixels), max(p[1] for p in pixels) assert image.width == image.height and min_x == min_y and max_x == max_y return pixels, min_x, max(p[0] for p in pixels) - min_x
def split_deltas(deltas_url): """Opens and decompress a gzipped text file and splits each line into two deltas""" left, right = [], [] with gzip.open(BytesIO(read_url(deltas_url))) as deltas: for line in deltas: line = line.decode().rsplit(" ", 1) left.append(line[0].strip()) right.append(line[1].strip()) return left, right
def advance_rewind_and_extract(img_url, pwd, size): """Finds the correct range start and use the password to ultimately extract and display next mission's content""" msg = read_riddle(img_url, range_header(size)).strip() msg = read_riddle(img_url, range_header(size - len(msg) - 2)).strip() start = msg.rstrip(".").rsplit(maxsplit=1)[-1] zip_content = read_url(img_url, range_header(start)).strip() with ZipFile(BytesIO(zip_content), "r") as zip_file: readme, package = zip_file.namelist() print(zip_file.read(readme, pwd=pwd.encode()).decode()) print(zip_file.extract(package, pwd=pwd.encode()))
def read_waves(url): """Reads all the WAVE files there are available under similar URLs""" i, waves = 1, [] while True: try: payload = read_url(url.replace("1", str(i))) with wave.open(io.BytesIO(payload)) as wave_read: waves.append(wave_read.readframes(wave_read.getnframes())) i += 1 except HTTPError: return waves
def read_comments_from_zip(url): with ZipFile(BytesIO(read_url(url))) as channel: with channel.open(channel.namelist()[-1]) as readme: for line in readme.readlines(): if "start" in line.decode(): curr = line.decode().strip().rsplit(maxsplit=1)[-1] break else: raise RuntimeError("Could not find start") comments = "" while True: try: comments += channel.getinfo(f"{curr}.txt").comment.decode() with channel.open(f"{curr}.txt") as current_fp: curr = current_fp.read().decode().rsplit(maxsplit=1)[-1] except KeyError: return comments except KeyboardInterrupt: exit(0) except Exception as e: print(f"Bang! {e} ({curr})") exit(1)
#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of Python Challenge Solutions # https://github.com/scorphus/PythonChallengeSolutions # Licensed under the BSD-3-Clause license: # https://opensource.org/licenses/BSD-3-Clause # Copyright (c) 2018-2020, Pablo S. Blum de Aguiar <*****@*****.**> # http://www.pythonchallenge.com/pc/ring/bell.html from auth import get_last_src_url from auth import read_url from io import BytesIO from itertools import islice from PIL import Image as Image img_url = get_last_src_url("http://www.pythonchallenge.com/pc/ring/bell.html") greens = islice(Image.open(BytesIO(read_url(img_url))).tobytes(), 1, None, 3) for curr, prev in zip(greens, greens): if abs(curr - prev) != 42: # 😱 the ultimate answer! Boring mission 😒 print(chr(abs(curr - prev)), end="") print(" (hint: ‘it’ is Python)")
new_image = Image.new(image.mode, (100, 100)) for pixel in reversed(image.getdata()): if -50 < x <= 50 and -50 < y <= 50: new_image.putpixel((x + 49, 50 - y), pixel) if x == y or x < 0 and x == -y or x > 0 and x == 1 - y: dx, dy = -dy, dx x, y = x + dx, y + dy return new_image class CatNameSayer(HTMLParser): """Parses an HTML and display data for <b> tags""" def handle_starttag(self, tag, _): self.show = tag == "b" def handle_data(self, data): if self.show: print(data) self.show = False url = "http://www.pythonchallenge.com/pc/return/italy.html" image_content = read_url(get_last_src_url(url)) image = Image.open(BytesIO(image_content)) spiral_and_transform(image).save("14-italy.png", "PNG") print("Open 14-italy.png only to see a cat") cat = read_riddle(url.replace("italy", "cat")) CatNameSayer().feed(cat)
def read_image(url): img_url = get_last_src_url(url).replace("1", "2").replace("jpg", "png") img = Image.open(BytesIO(read_url(img_url))) return img.mode, img.getdata()
def read_riddle_data(url): img_url = get_last_src_url(url) return (Image.open(BytesIO(read_url(img_url))), *read_riddle_numbers(url))
#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of Python Challenge Solutions # https://github.com/scorphus/PythonChallengeSolutions # Licensed under the BSD-3-Clause license: # https://opensource.org/licenses/BSD-3-Clause # Copyright (c) 2018-2020, Pablo S. Blum de Aguiar <*****@*****.**> # http://www.pythonchallenge.com/pc/def/peak.html # Source mentions banner.p from auth import get_last_src_url from auth import read_url import pickle url = "http://www.pythonchallenge.com/pc/def/peak.html" for row in pickle.loads(read_url(get_last_src_url(url))): print("".join(char * times for char, times in row))
#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of Python Challenge Solutions # https://github.com/scorphus/PythonChallengeSolutions # Licensed under the BSD-3-Clause license: # https://opensource.org/licenses/BSD-3-Clause # Copyright (c) 2018-2020, Pablo S. Blum de Aguiar <*****@*****.**> # http://www.pythonchallenge.com/pc/def/integrity.html from auth import get_last_href_url from auth import read_url import bz2 url = "http://www.pythonchallenge.com/pc/def/integrity.html" page_source = read_url(url).decode("unicode_escape").encode("latin1") page_data = page_source.split(b"<!--", 1)[1].split(b"-->", 1)[0] _, un, _, pw, _ = page_data.split(b"'") print(f"next: {get_last_href_url(url)}") print(f"user: {bz2.decompress(un).decode()}") print(f"pass: {bz2.decompress(pw).decode()}")
def load_maze(url): """Fetches and loads maze.png then returns its pixels and size""" maze_url = get_last_src_url(url) maze = Image.open(BytesIO(read_url(maze_url))) return maze.load(), maze.size
#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of Python Challenge Solutions # https://github.com/scorphus/PythonChallengeSolutions # Licensed under the BSD-3-Clause license: # https://opensource.org/licenses/BSD-3-Clause # Copyright (c) 2018-2020, Pablo S. Blum de Aguiar <*****@*****.**> # http://www.pythonchallenge.com/pc/return/5808.html from auth import get_last_src_url from auth import read_url from io import BytesIO from PIL import Image img_url = get_last_src_url( "http://www.pythonchallenge.com/pc/return/5808.html") img = Image.open(BytesIO(read_url(img_url))).convert("L") for y in range(0, 2 * img.height // 5, 4): for x in range(img.width // 2, img.width, 4): if (x + y) % 2 == 0: if img.getpixel((x, y)) > 14: print("##", end="") else: print(" ", end="") print()