Created
June 18, 2017 16:06
-
-
Save pgcd/a065b76dfb0c9180546cf07de51463d4 to your computer and use it in GitHub Desktop.
Save STRM for all the videos in a gdrive directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: python save_all_strms.py gdrive:your_video_folder | |
import os | |
import sys | |
import re | |
from subprocess import Popen, PIPE | |
import logging | |
log = logging.getLogger('save_all_strms') | |
log.addHandler(logging.StreamHandler()) | |
gdrive_username = '<your username>' # eg. youremail@gmail.com | |
destination = '<full (absolute) path to the destination folder>' # eg. /home/pgcd/movies | |
min_size = '500M' # Only files bigger than 500M are considered valid, adjust to taste | |
rclone = '/usr/bin/rclone' # Please note that this has to include support for https://github.com/ncw/rclone/issues/1473 | |
def make_strm(item, debug_out): | |
strm_template = 'plugin://plugin.video.gdrive/?mode=video&username=%(user)s&filename=%(dest_id)s&title=%(filename)s' | |
id_match = re.search(r'DEBUG : %s: ID = "(?P<id>.*)"' % re.escape(item), debug_out) | |
if id_match is None: | |
log.debug('No ID in %s, skipping', item) | |
return | |
final_id = id_match.groupdict()['id'] | |
title = os.path.basename(item) | |
strm_filename = os.path.join(destination, "%s.strm" % title) | |
strm_data = {'user': gdrive_username, 'dest_id': final_id, | |
'filename': os.path.basename(title)} | |
log.debug('Creating STRM with data: %s', strm_data) | |
if os.path.exists(strm_filename): | |
log.debug("%s exists; skipping", strm_filename) | |
return | |
with open(strm_filename, 'w') as f: | |
f.write(strm_template % strm_data) | |
return strm_filename, strm_data | |
def get_all_streams(root_folder): | |
p = Popen([ | |
rclone, 'ls', '-vv', | |
'--filter', '"+ *.mkv"', | |
'--filter', '"+ *.avi"', | |
'--filter', '"+ *.mp4"', | |
'--filter', '"- *"', | |
'--min-size', min_size, | |
root_folder], stdout=PIPE, stderr=PIPE, universal_newlines=True) | |
out, err = p.communicate() | |
for item in out.splitlines(): | |
make_strm(item.partition(' ')[2], err) | |
if __name__ == '__main__': | |
get_all_streams(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment