Toggle Navigation
Hatchery
Eggs
nerdjokes
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import wifi, ugTTS, random, keypad, system, audio, display, os, time, machine, uhashlib, binascii, valuestore, appconfig colors=[0xFF0000,0x00FF00,0x0000FF,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF] random_button=0 global_hashes=[] basename="/apps/nerdjokes" settings = appconfig.get("nerdjokes", {"jokes": [ "lol lol limewire", "This joke is a UDP joke. I don't care if you get it.", "I have a TCP joke, but nobody wants to ACK it", "Amazing! I got the TCP joke", "I about have UDP a", "I I have have a a joke joke on on TCP TCP", "adding a rule to firewall about TCP jokes*", "I have a tcp fragmentation joke but", "I have a joke about kubernetes, but it wont scale", "I only acknowledge TCP jokes", "I also tell dns jokes, but it takes up to 24 hours for everyone to get them.", "I was telling my fellow mates a TCP joke the other day; I had to keep repeating it slower and slower until they got it.", "If everyone adds the tcp jokes here does that make it a tcp dump?", "I got a TCP joke did you got it?", "I have a TCP joke, but getting it sometimes requires repeating it", "I'll tell you a TCP joke but you'd have to acknowledge it", "I had a database joke but I dropped it.", "Due to the COVID-19, all TCP applications have been converted to UDP in order to avoid handshakes", "I had a ransomware joke, but before I knew it I was held hostage.", "There are two hard things in computer science: cache invalidation, naming things, and off-by-one errors." ]}) jokes=settings["jokes"] print(jokes) def on_key(key_index, is_pressed): global jokes, random_button, channel_id print('Key ' , key_index , ': ' , is_pressed, '->', random_button) if(is_pressed and key_index==random_button): randombutton(False) jokenr=random.randint(0,len(jokes)-1) print("playing joke: ",jokenr) filename=basename+"/joke"+str(jokenr)+".mp3" if os.path.isfile(filename): audio.play(filename,machine.nvs_getint("system", "volume") or 255) time.sleep(5) randombutton(True) def need_wifi(): if not wifi.status(): audio.play('/cache/system/wifi_connecting.mp3',machine.nvs_getint("system", "volume") or 255) wifi.connect() wifi.wait() if not wifi.status(): audio.play('/cache/system/wifi_failed.mp3',machine.nvs_getint("system", "volume") or 255) time.sleep(5) system.launcher() filename=basename+"/joke_gen.mp3" if not os.path.isfile(filename): need_wifi() print("Generating joke file",filename) ugTTS.text_to_mp3("Generating joke file",filename) if os.path.isfile(filename): audio.play(filename,machine.nvs_getint("system", "volume") or 255) time.sleep(1) def create_jokes(): global global_hashes for i in range(0,len(jokes)): hash=str(i)+":"+str(binascii.hexlify(uhashlib.sha1(jokes[i]).digest())).split("'")[1] filename=basename+"/joke"+str(i)+".mp3" print("joke"+str(i),filename+"\r\n"+jokes[i]) if not os.path.isfile(filename) or not hash in global_hashes: need_wifi() print(jokes[i],filename) ugTTS.text_to_mp3(jokes[i],filename) for i in range(len(jokes),30): filename=basename+"/joke"+str(i)+".mp3" if os.path.isfile(filename): print("erasing: "+filename) os.remove(filename) filename=basename+"/joke_button.mp3" if not os.path.isfile(filename): need_wifi() ugTTS.text_to_mp3("Press the joke button",filename) def randombutton(gen): global random_button if gen: random_button=random.randint(0,15) for i in range(0,16): x = i % 4 y = i // 4 if i==random_button: color = colors[random.randint(0,len(colors)-1)] else: color = 0x000000 display.drawPixel(x, y, color) display.flush() def save_hashes(): global jokes print("saving") allhashes="" for i in range(0,len(jokes)): allhashes+=str(i)+":"+str(binascii.hexlify(uhashlib.sha1(jokes[i]).digest())).split("'")[1]+"\r\n" print(allhashes) valuestore.save("nerdjokes","hashes",allhashes) def load_hashes(): global global_hashes print("loading") if os.path.isfile("/config/nerk-jokes.json"): allhashes=valuestore.load("nerdjokes","hashes") print(allhashes) if not allhashes == None: global_hashes=allhashes.split("\r\n") load_hashes() save_hashes() create_jokes() audio.play(basename+"/joke_button.mp3",machine.nvs_getint("system", "volume") or 255) randombutton(True) keypad.add_handler(on_key)