Last active
January 5, 2020 21:02
-
-
Save LevitatingBusinessMan/7054edf4a34abf9cc3bbf737ce5d9ce5 to your computer and use it in GitHub Desktop.
https://quipqiup.com/ CLI program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
import sys | |
import requests | |
#import curses | |
import atexit | |
""" def reset_curses(): | |
curses.endwin() """ | |
base_url = "https://6n9n93nlr5.execute-api.us-east-1.amazonaws.com/prod/" | |
mode="statistics" | |
arguments = sys.argv | |
arguments.pop(0) | |
if "--dict" in arguments: | |
mode="dictionary" | |
arguments.remove("--dict") | |
if "--clue" in arguments: | |
ciphertext = " ".join(arguments[0:arguments.index("--clue")]) | |
clues = " ".join(arguments[arguments.index("--clue")+1:]) | |
else: | |
clues = "" | |
ciphertext = " ".join(arguments) | |
if not ciphertext: | |
print("Usage: quipqiup [ciphertext] [options]") | |
print("Options:") | |
print("\t--clue [clue]\t(eg. A=B QVW=THE)") | |
print("\t--dict\t\t(Use dictionary method)") | |
exit() | |
#statistics use solve endpoint and solve-spaces False property | |
#dictionary use dict endpoint (and 2x solve) | |
all_solutions = [] | |
requestsMade = 0 | |
def req(url, json): | |
# print(json) | |
response = requests.post(url, json=json) | |
if response.status_code != 200: | |
print("non 200 received") | |
exit() | |
res_json = response.json() | |
# add removing dupes | |
all_solutions.extend(solution for solution in res_json["solutions"] if solution not in all_solutions) | |
all_solutions.sort(key=(lambda x: x["logp"]),reverse=True) | |
# whatever quipqiup does with it's hamming function is omitted, | |
# it's not actually calculating hamming distance. It's fucking stupid | |
""" | |
stdscr = curses.initscr() | |
atexit.register(reset_curses) | |
for i in range(9): | |
if i >= len(all_solutions): | |
break | |
sol = all_solutions[i] | |
string = "{} {}\t\t\t\t{}".format(round(sol["logp"],3), sol["plaintext"], sol["key"]) | |
stdscr.addstr(i ,0, string) | |
stdscr.refresh() | |
""" | |
global requestsMade | |
requestsMade += 1 | |
# Show results | |
if requestsMade == requestsRequired: | |
#reset line | |
print(" "*len("x/x requests made"),end="\r") | |
#print(all_solutions) | |
#calculate longest text and unknowns in key | |
longest_text = 0 | |
for i, sol in enumerate(all_solutions[:9]): | |
if longest_text < len(sol["plaintext"]): | |
longest_text = len(sol["plaintext"]) | |
for char in sol["key"]: | |
if (char.lower() not in sol["plaintext"].lower()): | |
sol["key"] = sol["key"].replace(char,"?") | |
all_solutions[i] = sol | |
print("9 best results:") | |
for i in range(9): | |
if i >= len(all_solutions): | |
break | |
sol = all_solutions[i] | |
string = "{:.3f} {}\t{}".format(sol["logp"], sol["plaintext"] + " " * (longest_text - len(sol["plaintext"])), sol["key"][:-1]) | |
print(string) | |
exit() | |
else: | |
print("{}/{} requests made\r".format(requestsMade, requestsRequired), end="\r") | |
if mode == "statistics": | |
print("usinsg statistical solver") | |
requestsRequired = 5 | |
def request(time): | |
req(base_url+"solve", {"clues": clues, "ciphertext": ciphertext, "solve-spaces": False, "time": time}) | |
[request(time) for time in [0.5, 1, 2, 3, 4]] | |
if mode == "dictionary": | |
print("usinsg dictionary solver") | |
requestsRequired = 10 | |
for i in range(3): | |
req(base_url+"dict", { | |
"shard": 3, | |
"shardidx": i, | |
"clues": clues, | |
"ciphertext": ciphertext, | |
"time": 3.0 | |
}) | |
for i in range(7): | |
req(base_url+"dict", { | |
"shard": 7, | |
"shardidx": i, | |
"clues": clues, | |
"ciphertext": ciphertext, | |
"time": 7.0 | |
}) | |
# 2 solve calls | |
req(base_url+"solve", {"clues": clues, "ciphertext": ciphertext, "solve-spaces": False, time: 2.0}) | |
req(base_url+"solve", {"clues": clues, "ciphertext": ciphertext, "solve-spaces": False, time: 7.0}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment