/choose_wisely.py

https://github.com/RichardLitt/Other-Codes · Python · 125 lines · 67 code · 19 blank · 39 comment · 14 complexity · 2b88b6e37b59eb361c0d720481682ff0 MD5 · raw file

  1. # This is a movie chooser, for when you're too lazy or have too many movies and
  2. # really can't decide what to pick. Choose wisely, or you'll turn into a Nazi
  3. # skeleton and Elsa will die screaming.
  4. #
  5. # Developed and written by Richard Littauer.
  6. # =============================================================================
  7. # Some stuff that needs changing!
  8. # For automatically getting the movies from a folder.
  9. folder = '/Volumes/ASUKA/Diaz/'
  10. # For preloading in the options, if you'd rather do that.
  11. has_movies = ["Iron man", "Casablanca", "Three days of the condor", "The \
  12. Science of Sleep", "The Quiet Man", "Solaris", "Star Trek", "The \
  13. English Patient", "Syriana", "Dead Poets Society"]
  14. has_episodes = ['Flight of the Conchords', 'Father Ted', 'Spaced']
  15. # =============================================================================
  16. import random
  17. import sys
  18. import os
  19. import subprocess
  20. #print(os.environ['PATH']) # Checks the current Path
  21. # =============================================================================
  22. # The following functions depend not upon an individual list of files, nor on
  23. # having the same type of files in a mineable folder, but rather more than
  24. # that.
  25. ### Note: It would be a lot easier to use:
  26. # find . -name "*.avi" -print
  27. # find . -name "*.mp4" -print
  28. # find . -name "*.mkv" -print
  29. # find . -name "*.AVI" -print
  30. ### Going to need to remove all *sample* files
  31. ### And work for CD1, CD2 entries.
  32. # This lists the folder - global is a bit of a hack, but hey, it works.
  33. def folders():
  34. global folder
  35. cmd = [ 'ls', folder ]
  36. output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
  37. return output
  38. # sort() puts all of the files and folders in that folder in a list, and
  39. # chooses a random one to select and run.
  40. def sort(output):
  41. catalogue = []
  42. output = output.split('\n')
  43. for line in output: catalogue.append(line)
  44. choice = random.randrange(len(catalogue))
  45. return catalogue[choice]
  46. # Let's open up that folder, or return it if it's just a top-level movie file.
  47. def open_folder(choice):
  48. global folder
  49. folder = folder + choice
  50. if folder[-3:] == 'avi':
  51. return
  52. if folder[-3:] == 'mkv':
  53. return
  54. else:
  55. cmd = [ 'ls', folder ]
  56. output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
  57. return output
  58. # This opens up the file in the appropriate player, and should work well if it
  59. # is in a folder or not. This is also the end of these functions.
  60. def open_file(choice):
  61. choice = choice.split('\n')
  62. for line in choice:
  63. global folder
  64. line = folder + '/' + line
  65. print line
  66. line = line.replace(' ', '\ ').replace('(', '\(')\
  67. .replace(')','\)')
  68. if line[-4:] == '.avi':
  69. cmd = 'vlc ' + line
  70. os.system(str(cmd))
  71. if line[-4:] == '.mp4':
  72. cmd = 'open ' + line
  73. os.system(str(cmd))
  74. if line[-4:] == '.mkv':
  75. cmd = 'open ' + line
  76. os.system(str(cmd))
  77. # =============================================================================
  78. # This is another random sorter, but is a bit primitive and depends upon
  79. # preloading in the movies you want to see. Thankfully, we're past this now.
  80. def dice():
  81. if sys.argv[2] == "movie":
  82. choice = random.randrange(len(has_movies))
  83. return has_movies[choice]
  84. if sys.argv[2] == "episode":
  85. choice = random.randrange(len(has_episodes))
  86. return has_episodes[choice]
  87. # This actually plays the movie, if it's in the folder and titled
  88. # appropriately.
  89. def pikachu():
  90. choice = dice()
  91. choice = choice.replace(' ', '\ ').replace('(', '\(').replace(')', '\)')
  92. command = "VLC " + str(choice)
  93. os.system(command)
  94. # =============================================================================
  95. # The main. Not, in fact, the Spanish main.
  96. if __name__ == "__main__":
  97. # If you just want to pick from a random list
  98. if (sys.argv[1] == "choose"):
  99. dice()
  100. # If you want to try and run from that list
  101. if (sys.argv[1] == "pikachu"):
  102. pikachu()
  103. # If you just want a movie to run, period. No arguments, either.
  104. if (sys.argv[1] == 'Diaz'):
  105. open_file(open_folder(sort(folders())))
  106. # Don't you just love comments?