Skip to content

Instantly share code, notes, and snippets.

@earonesty
Created Jan 13, 2022
Embed
What would you like to do?
import requests
import string
import random
import argparse
import json
def main():
parser = argparse.ArgumentParser(description='Post cryptoquips')
parser.add_argument("--generate", action="store_true")
parser.add_argument("--post", action="store_true")
parser.add_argument("--reply", action="store")
parser.add_argument("--really", action="store")
args = parser.parse_args()
if (args.generate):
generate_new()
if (args.post):
post()
if (args.reply):
reply(args.reply, args.really)
def generate_new():
dat = requests.get("https://www.bitcoin-quotes.com/quotes/random.json").json()
body = dat["body"]
author = dat["author"]["name"]
asciidata=body.encode("ascii","ignore")
src = list(string.ascii_lowercase)
dest = src.copy()
random.shuffle(dest)
quip = {k: dest[i] for i, k in enumerate(src)}
res = ""
for let in body:
q = quip.get(let.lower())
if q:
if let.upper() == let:
q = q.upper()
else:
q = let
res += q
output = {
"orig": body,
"author": author,
"quip": res,
"map": quip,
}
print("output to gen.json")
with open("gen.json", "w") as f:
f.write(json.dumps(output))
def post():
dat = json.load(open("gen.json"))
txt = "A bitcoin cryptoquip, solve it (solution posted tomorrow)!\n\n"
txt += '"' + dat["quip"] + '" - ' + dat["author"]
print(txt)
from login import api
stat = api.update_status(txt)
id_str = str(stat.id)
dat["id"] = stat.id
# save for later
with open("post-" + id_str + ".json", "w") as f:
f.write(json.dumps(dat))
def reply(id_str, really):
dat = json.load(open("post-" + id_str + ".json"))
assert dat["id"] == int(id_str)
assert not dat.get("solved")
from login import api
sol = 'Solution:\n\n"' + dat["orig"] + '" - ' + dat["author"]
print(sol)
dat["solved"] = True
if really:
api.update_status(sol, in_reply_to_status_id=dat["id"], auto_populate_reply_metadata=True)
with open("post-" + id_str + ".json", "w") as f:
f.write(json.dumps(dat))
else:
print("### didn't post, specify --really")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment