Exemple #1
0
    def plugin_update(self, plugin: BasePlugin):
        plugin.validate_settings()

        while True:
            try:
                new_plugin_src = plugin.get_update(smarthash_version)
                self.clear_error()
                break
            except (requests.exceptions.ConnectionError, ServerError):
                self.init_error(
                    "Connection error: plugin could not check for updates. retrying..."
                )
                if self.early_return:
                    return

                time.sleep(1)

            except UpdateError as e:
                cprint(e.error, 'red')
                return

        if new_plugin_src != "":
            # noinspection PyBroadException
            try:
                plugin_path = self.get_plugin_path()
                with open(os.path.join(plugin_path, '__temp__.py'),
                          'w+') as plugin_file:
                    plugin_file.write(new_plugin_src)
                new_plugin_module = importlib.import_module(
                    "Plugins.__temp__").SmarthashPlugin()

                os.remove(os.path.join(plugin_path, plugin.get_filename()))
                os.rename(
                    os.path.join(plugin_path,
                                 new_plugin_module.get_filename()),
                    os.path.join(plugin_path, plugin.get_filename()))
                cprint("'{0}' plugin updated from {1} to {2}".format(
                    new_plugin_module.description, plugin.plugin_version,
                    new_plugin_module.plugin_version))
                self.plugins[plugin.get_filename()] = new_plugin_module
            except Exception:
                cprint("Failed updating to new version of '{0}'".format(
                    plugin.description))
                sys.exit(1)
Exemple #2
0
 def __init__(self, options, event, url=None, params=None):
     """ Init method of sample plugin. """
     BasePlugin.__init__(self, options, event, url, params)
 def __init__(self, *args):
     BasePlugin.__init__(self, *args)
     self.next_trigger_autoconnect = False
Exemple #4
0
 def __init__(self, *args):
     BasePlugin.__init__(self, *args)
     self.last_status = {}
Exemple #5
0
 def __init__(self, options, event, params=None):
     """ Init method of sample plugin. """
     BasePlugin.__init__(self, options, event, params)
 def __init__(self, *args):
     BasePlugin.__init__(self, *args)
     global get_all_by_type
     get_all_by_type = self.daemon.interface_manager.get_all_by_type
     self.location_manager = LocationManager('/etc/wicd/locations.conf')
Exemple #7
0
    def process_folder(self, path: str, plugin: BasePlugin):

        logging.info("----------------------------\n{0}".format(path))
        print("\n{0}".format(path))

        self.total_media_size, total_duration, smarthash_path_info = extract_metadata(
            path)

        params = {
            'blacklist_file_extensions':
            [x.lower() for x in blacklist_file_extensions],
            'blacklist_path_matches':
            [x.lower() for x in blacklist_path_matches],
            'comment':
            "Generated with SmartHash {0}".format(smarthash_version),
            'smarthash_version':
            smarthash_version,
        }

        plugin.early_validation(
            path, {
                'args': self.args,
                'smarthash_info': smarthash_path_info,
                'title': os.path.basename(path),
                'params': params
            })

        # hash the folder
        metainfo = make_meta_file(path,
                                  None,
                                  params=params,
                                  progress=self.hash_progress_callback)

        pricker = Pricker(self.pricker_progress_callback)

        # lookup gathered metadata and insert into the torrent file metainfo
        for file in metainfo['info']['files']:
            file_path = os.path.join(os.path.basename(path), *file['path'])

            if file_path in smarthash_path_info:
                file['smarthash_info'] = json.dumps(
                    smarthash_path_info[file_path])

                # calculate a pricker hash for audio files
                ext = os.path.splitext(file_path)[1].lower()
                mime_prefix = smarthash_path_info[file_path][
                    'mime_type'].split('/')[0]

                if mime_prefix == 'audio' or ext in whitelist_audio_extensions or \
                        (not self.args.skip_video_rehash and
                         (mime_prefix == 'video' or ext in whitelist_audio_extensions)):

                    try:
                        pricker.open(os.path.join(path, *file['path']))
                        file['pricker'] = pricker.hexdigest()
                        metainfo['pricker_version'] = pricker.version()
                    except PrickError:
                        pass

        formatted_mediainfo = ""
        extracted_images = []

        screenshot_files = []

        # extract MediaInfo
        for file in metainfo['info']['files']:
            file_path = os.path.join(path, *file['path'])
            ext = os.path.splitext(file_path)[1].lower()
            path_key = os.path.join(metainfo['info']['name'], *file['path'])
            mime_type = smarthash_path_info[path_key]['mime_type'] \
                if path_key in smarthash_path_info else get_mime_type(file_path)
            mime_prefix = mime_type.split("/")[0]

            # for video files, compose a standard(ish) MediaInfo text output
            if (mime_prefix == "video" or ext in whitelist_video_extensions
                ) and ext not in blacklist_media_extensions:
                if formatted_mediainfo != "":
                    formatted_mediainfo += "\n{0}\n".format("-" * 70)
                formatted_mediainfo += MIFormat.mediainfo_to_string(
                    smarthash_path_info[os.path.join(
                        os.path.basename(path), *file['path'])]['mediainfo'])

                screenshot_files.append(file_path)

        if "video-screenshots" in plugin.options:
            extracted_images = self.extract_images(screenshot_files)

        # collect the dataset for the plugin
        data = {
            'smarthash_version': smarthash_version,
            'args': self.args,
            'path': path,
            'title': os.path.split(path)[-1],
            'total_duration': total_duration,
            'mediainfo': formatted_mediainfo,
            'extracted_images': extracted_images,
            'torrent_file': metainfo.gettorrent(),
        }

        print("\rCalling plugin '{0}'...".format(plugin.get_title()), end='')
        plugin.handle(data)

        # if an operation succeeded, write out the config
        self.save_config()