{"id":64,"date":"2021-02-17T20:48:08","date_gmt":"2021-02-18T01:48:08","guid":{"rendered":"https:\/\/gryph.one\/?p=64"},"modified":"2021-02-17T20:58:20","modified_gmt":"2021-02-18T01:58:20","slug":"random-code-pokemon-of-the-day-python-script","status":"publish","type":"post","link":"https:\/\/gryph.one\/index.php\/2021\/02\/17\/random-code-pokemon-of-the-day-python-script\/","title":{"rendered":"Random Code: Pok\u00e9mon of the Day Python script"},"content":{"rendered":"\n<p>Wrote this while bored, figured I&#8217;d toss it out there for anyone who cares. Gives you a different Pokemon, and its Pokedex entry, each day. The specific Pokemon is randomly chosen, with the current date as the seed value.<\/p>\n\n\n\n<p>There are probably ways to make it better, but I&#8217;m heavily constrained by the JSON that I&#8217;m retrieving from pokeapi.co on this one and I don&#8217;t feel like putting in the effort.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code>#\tPok\u00e9mon of the Day Python script\n#\tCopyright 2021 Takel\n#\n#\tRedistribution and use in source and binary forms, with or without modification, are permitted provided\n#\tthat the following conditions are met:\n#\n#\t1. Redistributions of source code must retain the above copyright notice, this list of conditions and the\n#\tfollowing disclaimer.\n#\n#\t2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and\n#\tthe following disclaimer in the documentation and\/or other materials provided with the distribution.\n#\n#\tTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED\n#\tWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\n#\tPARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\n#\tANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#\tLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n#\tINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR\n#\tTORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#\tADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nimport json, random, requests, time\n\n#\tget the date as an integer, which we will use as the RNG seed;\n#\tthat way, it's a new pok\u00e9mon each day and only each day, but\n#\twe're not just modulus-ing our way through the list\nrandom.seed(time.strftime('%Y%m%d'))\npokeNum = random.randint(1, 898)\n\npokeJSON = json.loads(json.dumps(requests.get(f'https:\/\/pokeapi.co\/api\/v2\/pokemon-species\/{pokeNum}').json()))\n\n\ndef getPokemonTextAttribute(keytype, key):\n    pokeVersion = \"\"\n    latestVersion = \"\"\n    for i in range(0, 500):\t\t# there's no way to directly request a specific language from\n\t\t\t\t\t\t\t\t# the JSON, so I have to use a for loop to iterate through the\n\t\t\t\t\t\t\t\t# whole goddamn file\n\n        # additional handling code for flavor text because national dex\n        if keytype == \"flavor_text_entries\":\n            versions = &#91;\"sword\", \"ultra-sun\", \"omega-ruby\"]\t\t# one of these three should have the\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t# flavor text in the JSON; searching\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t# for the most recent first\n            for j in range(0, 3):\n                value = versions&#91;j]\n                for k in range(0, 500):\t\t# again, the API won't let me grab just a specific\n\t\t\t\t\t\t\t\t\t\t\t# language, so I have to loop through the whole thing\n                    try:\n                        if value == pokeJSON&#91;\"flavor_text_entries\"]&#91;k]&#91;\"version\"]&#91;\"name\"]:\n                            latestVersion = value\n                            break\n                    except IndexError:  # if k exceeds the number of entries in the JSON,\n\t\t\t\t\t\t\t\t\t\t# start over with the next game version\n                        break\n                if latestVersion == value:\n                    break\n            pokeVersion = pokeJSON&#91;\"flavor_text_entries\"]&#91;i]&#91;\"version\"]&#91;\"name\"]\n\n        pokeLang = pokeJSON&#91;keytype]&#91;i]&#91;\"language\"]&#91;\"name\"]\n        if pokeVersion == latestVersion and pokeLang == \"en\" and keytype == \"flavor_text_entries\":\n            # we found the latest flavor text, and it's in english\n            break\n        elif pokeLang == \"en\" and keytype != \"flavor_text_entries\":\n            # we found the requested key, and it's in english\n            break\n\n    # we didn't find what we need, iterate the for loop again\n\n    return pokeJSON&#91;keytype]&#91;i]&#91;key]\n\n\npokeName = getPokemonTextAttribute(\"names\", \"name\")\npokeGenus = getPokemonTextAttribute(\"genera\", \"genus\")\npokeDesc = getPokemonTextAttribute(\"flavor_text_entries\", \"flavor_text\")\n\nprint(\"The Pok\u00e9mon of the Day is...\")\nprint()\nprint(\"#\" + str(pokeNum) + \":\", pokeName + \", the\", pokeGenus)\nprint()\nprint(pokeDesc)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wrote this while bored, figured I&#8217;d toss it out there for anyone who cares. Gives you a different Pokemon, and its Pokedex entry, each day.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[21,22],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-python","tag-pokemon","tag-random-code"],"_links":{"self":[{"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":5,"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":124,"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/posts\/64\/revisions\/124"}],"wp:attachment":[{"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gryph.one\/index.php\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}