Created
October 4, 2017 12:46
-
-
Save mnemocron/584f282546ef73f23470c0a96a0c1218 to your computer and use it in GitHub Desktop.
Selects a random word from the json word list (for a Telegram bot)
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/python | |
#_*_ coding: utf-8 _*_ | |
''' | |
@author Simon Burkhardt - simonmartin.ch - github.com/mnemocron | |
@date 2017 | |
''' | |
import random | |
import sys # args | |
import os # files, directories | |
import json | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
# very hacky solution for directory independent executions (cronjobs) | |
dir = "/home/simon/workspace/berndeutsch/" | |
json_data = open(dir + 'words.all.json').read() | |
words = json.loads(json_data)['words'] | |
def randomWord(): | |
return random.choice(words) | |
try: | |
while(True): # well probably that much of a good idea -> all words on used-id.list | |
x = randomWord() | |
if 'true' in x['listed']: | |
with open(dir + 'used-ids.list', 'r') as idfile: | |
ids = idfile.read() | |
if not (str(x['index']) in ids): | |
with open(dir + "used-ids.list", "a") as idfile: | |
idfile.write(str(x['index'])) | |
idfile.write('\n') | |
print "_\"" + str(x['word']) + "\"_" | |
print x['explanation'] | |
break | |
# else: | |
# print('NOT LISTED') | |
except IndexError: | |
print('found nothing') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More about the entire project on my website.
simonmartin.ch