Skip to content

Instantly share code, notes, and snippets.

@pncnmnp
Last active April 11, 2023 08:04
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 pncnmnp/62f9775103c4166f861777d5108f3c9b to your computer and use it in GitHub Desktop.
Save pncnmnp/62f9775103c4166f861777d5108f3c9b to your computer and use it in GitHub Desktop.
edit the 'path' variable to the place where your tv show directories are, then edit command line options accordingly.
from random import choice
from os import listdir, chdir, system
from os.path import isdir
from glob import glob
#from subprocess import Popen
from readline import parse_and_bind
from pprint import pprint
from sys import argv
'''
NOTE: This code supports 2-level directory structure.
Episodes can be either in some sort of 'seasons' dir
or they can all be in the root dir.
'''
# enter your media library path
path = '/home/parthparikh/parth/seasons/'
# command line options
names = {'f':'Friends/', 'h':'HIMYM/', 't':'The Big Bang Theory/', 's':'Seinfeld/'}
lock = False
try:
if argv[1] in names:
path += names[argv[1]]
lock = True
except:
pass
if not lock:
print('Enter \' q \' or \' ↵ \' to exit!')
while(not lock):
try:
chdir(path)
parse_and_bind("tab: complete")
title = input('Enter name of show: ')
if isdir(path+title+'/'):
path += title + '/'
break
elif title == 'q':
exit(0)
else:
print('Available options: ')
pprint(glob('*'))
except:
break
seasons = choice(listdir(path))
if seasons[-1] != '/':
seasons += '/'
path += seasons
try:
if path[:-1].split('.')[-1] in ['mkv', 'mp4', 'm4v', 'avi']:
system("vlc '"+path[:-1]+"'")
else:
episodes = glob(path+'*.mkv')+glob(path+'*.mp4')+glob(path+'*.m4v')+glob(path+'*.avi')
episode = choice(episodes)
print(episode)
# Put your vlc-media-player path ( different for mac, windows and linux )
# Popen(["/usr/bin/vlc","%s" % episode])
system("vlc '"+episode+"'")
lock = True
except:
if lock:
print('Error! Terminating! Check your vlc path or directory path!')
@pncnmnp
Copy link
Author

pncnmnp commented Feb 7, 2019

Change your .bashrc file to something like:
alias tv='python3.5 /file_path/random_episode.py'
Then use tv <command_line_show_shortcut>

@pncnmnp
Copy link
Author

pncnmnp commented Apr 11, 2023

Years later, I realized that this can be done in 1 line of bash script:

cd ~/shows/ && cd "$(ls | sort -R | tail -1)" && cd "$(ls | sort -R | tail -1)" && vlc "$(ls ./*.mp4 ./*.mkv | sort -R | tail -1)"

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