Last active
July 23, 2017 07:53
-
-
Save pgcd/dc5c69476fa0e1fbe426becc110c9713 to your computer and use it in GitHub Desktop.
CouchPotato PostProcess Script to move to Google Drive and replace with a STRM for Kodi
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
from .main import PostProcess | |
def autoload(): | |
return PostProcess() |
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
### Note: this script only works if https://github.com/ncw/rclone/issues/1473 is closed with the recommended solution, | |
### or if rclone is built from source integrating that solution, else there's no way of retrieving the ID from Drive. | |
import os | |
import traceback | |
import re | |
from subprocess import Popen, PIPE | |
from couchpotato.core.event import addEvent | |
from couchpotato.core.logger import CPLog | |
from couchpotato.core.plugins.base import Plugin | |
log = CPLog(__name__) | |
gdrive_username = '<your gdrive username here>' | |
gdrive_rootfolder = 'video' # change to taste, no trailing slash | |
rclone = '/usr/bin/rclone' # Should be OK to leave this alone | |
min_file_size = 200 * 1024 * 1024 | |
class PostProcess(Plugin): | |
def __init__(self): | |
addEvent('renamer.after', self.callscript) | |
log.info('[DR]Initialized') | |
def callscript(self, message=None, group=None): | |
log.info('[DR]Started') | |
path = group.get('destination_dir') | |
dir_name = os.path.basename(path) | |
if path is None: | |
log.error('[DR]No destination_dir in group %s' % group) | |
return False | |
realfiles = [x for x in group.get('renamed_files', []) | |
if group.get('filename', 'FILENAME IS MISSING') in x] | |
if not realfiles: | |
log.error( | |
'[DR]No file with the right name has been renamed in group %s' % group) | |
return False | |
log.info('[DR]Moving files to drive from dir "%s". File list: %s' % (path, realfiles)) | |
for renamed in realfiles: | |
if os.path.getsize(renamed) < min_file_size: | |
continue | |
try: | |
replace_with_strm(dir_name, renamed) | |
except: | |
log.error('[DR]Failed to call script: %s' % (traceback.format_exc())) | |
return False | |
log.info('[DR]PostProcess Script was called successfully') | |
return True | |
def replace_with_strm(dest_dir, local_file): | |
dest = "gdrive:%s/%s/%s" % (gdrive_rootfolder, dest_dir, os.path.basename(local_file)) | |
command = [rclone, 'move', local_file, dest] | |
log.info('[DR]Moving file to drive with arguments: %s' % command) | |
p = Popen(command, stdout=PIPE) | |
res = p.wait() | |
if res == 0: | |
strm_name, strm_contents = make_strm(dest, local_file) | |
log.info('[DR]STRM created: %s with contents: %s' % (strm_name, strm_contents)) | |
else: | |
log.error('[DR]PostProcess Script returned an error code: %s' % str(res)) | |
def get_drive_id(path): | |
p = Popen([rclone, 'lsl', '-vv', path], | |
stdout=PIPE, stderr=PIPE, universal_newlines=True) | |
out, err = p.communicate() | |
id_match = re.search( | |
r'%s: ID = "(?P<id>[^"]+)"' % os.path.basename(path), err) | |
if id_match is None: | |
log.error("[DR]No match found for %s; full output was: %s\n\n%s" % (path, out, err)) | |
return None | |
return id_match.groupdict()['id'] | |
def make_strm(dest, local_file): | |
""" | |
:param dest: full rclone path to file | |
:param local_file: full path to local file (maybe removable) | |
""" | |
strm_template = 'plugin://plugin.video.gdrive/?mode=video&username=%(user)s&filename=%(dest_id)s&title=%(filename)s' | |
final_id = get_drive_id(dest) | |
if final_id is None: | |
raise Exception("Unable to find destination ID") | |
strm_filename = '%s.strm' % local_file | |
strm_data = {'user': gdrive_username, 'dest_id': final_id, | |
'filename': os.path.basename(local_file)} | |
log.info('[DR]Creating STRM with data: %s' % strm_data) | |
with open(strm_filename, 'w') as f: | |
f.write(strm_template % strm_data) | |
return strm_filename, strm_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The two files should be placed in a directory inside CouchPotato
$DATA_DIR/custom_plugins
. For instance, if your $DATA_DIR location is/home/pgcd/couchpotato
, the two files should be inside/home/pgcd/couchpotato/custom_plugins/PostProcess
. You can find the location of the $DATA_DIR in CouchPotato "About" page.Please note that. until rclone/rclone#1473 is closed, this script won't work since the ID needed for the STRM cannot be retrieved.