Skip to content

Instantly share code, notes, and snippets.

@ionutvilie
Last active April 5, 2017 17:42
Show Gist options
  • Save ionutvilie/10eb7570e0b5fb439d4b0e67fe940766 to your computer and use it in GitHub Desktop.
Save ionutvilie/10eb7570e0b5fb439d4b0e67fe940766 to your computer and use it in GitHub Desktop.
kodi tv show library builder
#!/usr/bin/env python2.7
"""
if everything is stored in a Download folder
the script uses regex to filter tv shows based on 'S00E00' pattern
and create a compatible kodi tv shows structure using symlinks
"""
import os
import re
downloads = "/home/ionut/Downloads/"
tv_shows_library = "/home/ionut/Videos/TvShows"
replace = {
'Foo': 'Foo.2015',
'Bar': 'Bar.2015',
}
def main():
for f in os.listdir(downloads):
# print f
m = re.match(r"(.*).(S[0-9]{1,2}E[0-9]{1,2})", f)
if m:
tv_show_name = m.group(1)
tv_season_ep = m.group(2)
# check if you have to rename a tv show, eg: show to show.2017
if tv_show_name in replace.keys():
tv_show_name = replace[tv_show_name]
print tv_show_name + " " + tv_season_ep
# if a folder name with the name of the show is not present create it !
if not os.path.exists(os.path.join(tv_shows_library, tv_show_name)):
os.makedirs(os.path.join(tv_shows_library, tv_show_name))
# try co create sim link, if not present
print " * detected: " + os.path.join(downloads, f)
try:
os.symlink(os.path.join(downloads, f),
os.path.join(tv_shows_library, tv_show_name, tv_show_name + "." + tv_season_ep))
except OSError:
print " - symlink exist: " + \
os.path.join(tv_shows_library, tv_show_name, tv_show_name + "." + tv_season_ep)
else:
print " + symlink added: " + \
os.path.join(tv_shows_library, tv_show_name, tv_show_name + "." + tv_season_ep)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment