Skip to content

Instantly share code, notes, and snippets.

@mnemocron
Created October 4, 2017 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnemocron/584f282546ef73f23470c0a96a0c1218 to your computer and use it in GitHub Desktop.
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)
#!/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')
@mnemocron
Copy link
Author

More about the entire project on my website.

simonmartin.ch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment