Last active
January 15, 2018 16:58
-
-
Save lucasrcezimbra/a2425f9bca2d2e68971a1e491426c0c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import os | |
| import random | |
| import tempfile | |
| import requests | |
| TEMPDIR = tempfile.gettempdir() | |
| TEMP_WORDS_FILE = os.path.join(TEMPDIR, 'ista_words') | |
| def main(): | |
| words = _read_words_from_cache() | |
| if not words: | |
| words = _get_words_from_web() | |
| print(random.choice(words)) | |
| def _get_words_from_web(): | |
| URL = 'https://rhymit-3232c.firebaseio.com/v6/pt/termination/{page}/common.json' | |
| page = 'ista-1' | |
| words = [] | |
| while page: | |
| json_response = requests.get(URL.format(page=page)).json() | |
| words += json_response['data'].split(', ') | |
| page = json_response.get('next', None) | |
| _save_words_in_cache(words) | |
| return words | |
| def _save_words_in_cache(words): | |
| with open(TEMP_WORDS_FILE, 'w') as file: | |
| file.write('\n'.join(words)) | |
| def _read_words_from_cache(): | |
| if not os.path.exists(TEMP_WORDS_FILE): | |
| return None | |
| with open(TEMP_WORDS_FILE, 'r') as file: | |
| words = file.read().splitlines() | |
| return words | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or 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
| requests==2.18.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment