cli-pokeprint.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # A simpler version of Phoney Badger's pokemon-colorscripts tool:
  3. # https://gitlab.com/phoneybadger/pokemon-colorscripts
  4. import requests
  5. import re
  6. import os
  7. import argparse
  8. import random
  9. # A place where you store pokemon sprites. You can get these sprites using the
  10. # -g/--generate option.
  11. dir = "C:\\SGZ_Pro\\Hobbys\\Media\\pokemon\\pokedex\\pixel"
  12. # A program that you can find on github for terminal image printing. I
  13. # found imcat because it works on windows but you can use whatever you
  14. # want like ueberzug and whatever else.
  15. cli_image_printer = "imcat"
  16. def get_arguments():
  17. parser = argparse.ArgumentParser(description='CLI Pokeprint')
  18. parser.add_argument('-g', '--generate', type=str, metavar='DIR', help='Download all of the pokemon sprites to a specified directory.')
  19. parser.add_argument('-n', '--name', type=str, metavar='NAME', help='Select pokemon based on it\'s name.')
  20. parser.add_argument('-l', '--list', action="store_true", default=False, help='Copy the link to the pasted file')
  21. parser.add_argument('--no-title', action="store_true", default=False, help='Copy the link to the pasted file')
  22. parser.add_argument('-r', '--random', action="store_true", default=False, help='Copy the link to the pasted file')
  23. args = parser.parse_args()
  24. return args
  25. def main():
  26. args = get_arguments()
  27. if args.generate:
  28. headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121201 icecat/17.0.1'}
  29. database = "https://pokemondb.net/pokedex/all"
  30. request = requests.get(database, headers=headers)
  31. data = request.text
  32. print(data)
  33. images = re.findall('<img class=\"img-fixed icon-pkmn\" src=\"(.*?)\"', data)
  34. print(images)
  35. if os.path.isdir(args.generate) == False:
  36. os.mkdir(args.generate)
  37. for i, image in enumerate(images):
  38. url = image
  39. print(url)
  40. name = url.split("/")[-1].split(".png")[0]
  41. if "\\" in name:
  42. name = name.split("\\")[0]
  43. print(name)
  44. thumbnail_data = requests.get(url)
  45. png = name + ".png"
  46. cwd = os.getcwd()
  47. print(os.path.join(cwd, args.generate, png))
  48. with open(os.path.join(cwd, args.generate, png), 'wb') as f:
  49. f.write(thumbnail_data.content)
  50. elif args.list:
  51. for file in os.listdir(dir):
  52. print(file.replace(".png", ""))
  53. elif args.name:
  54. matches = []
  55. for file in os.listdir(dir):
  56. if file.startswith(args.name):
  57. matches.append(file)
  58. if matches != []:
  59. if not args.no_title:
  60. print(matches[0].replace(".png", ""))
  61. os.system(f'{cli_image_printer} "{os.path.join(dir,matches[0])}"')
  62. else:
  63. print(f"ERROR: Could not find {args.name}. This could be because of a spelling mistake. Or it was not saved on your computer.")
  64. elif args.random:
  65. pokemon = os.listdir(dir)[random.randint(0, len(os.listdir(dir)))]
  66. if not args.no_title:
  67. print(pokemon.replace(".png", ""))
  68. os.system(f'{cli_image_printer} "{os.path.join(dir,pokemon)}"')
  69. if __name__ == '__main__':
  70. main()