Exemple #1
0
    def wrapper(*args, **kwarg):
        try:
            response = func(*args, **kwarg)

            if response.status_code in [200, 201, 204]:
                return response

            g.log(
                "OMDb returned a {} ({}): while requesting {}".format(
                    response.status_code,
                    OMDB_STATUS_CODES[response.status_code],
                    "&".join(x for x in response.url.split('&')
                             if not x.lower().startswith("apikey")),
                ),
                "error",
            )

            return None
        except requests.exceptions.ConnectionError as e:
            g.log("Connection Error to OMDb: {} - {}".format(args, kwarg),
                  "error")
            g.log(e, "error")
            return None
        except Exception:
            xbmcgui.Dialog().notification(
                g.ADDON_NAME,
                g.get_language_string(30025).format("OMDb"))
            if g.get_runtime_setting("run.mode") == "test":
                raise
            else:
                g.log_stacktrace()
            return None
    def wrapper(*args, **kwarg):
        try:
            response = func(*args, **kwarg)
            if response.status_code in [200, 201]:
                return response

            if response.status_code == 404:
                g.log('FanartTv failed to find {}'.format(response.url),
                      'debug')
                return None
            else:
                g.log(
                    'FanartTv returned a {} ({}): while requesting {}'.format(
                        response.status_code,
                        FanartTv.http_codes[response.status_code],
                        response.url), 'error')
            return response
        except requests.exceptions.ConnectionError:
            return None
        except Exception:
            xbmcgui.Dialog().notification(
                g.ADDON_NAME,
                g.get_language_string(30025).format('Fanart'))
            if g.get_runtime_setting("run.mode") == "test":
                raise
            else:
                g.log_stacktrace()
            return None
    def _handle_bookmark(self):
        if g.get_runtime_setting("marked_watched_dialog_open"):
            return

        try:
            g.clear_kodi_bookmarks()
        except Exception:
            g.log_stacktrace()
            pass

        if self.current_time == 0 or self.total_time == 0:
            self.bookmark_sync.remove_bookmark(self.trakt_id)
            return

        if (
                self.watched_percentage < self.playCountMinimumPercent
                and self.current_time >= self.ignoreSecondsAtStart
                and not self._force_marked_watched
         ):
            self.bookmark_sync.set_bookmark(
                self.trakt_id,
                int(self.current_time),
                self.mediatype,
                self.watched_percentage,
                )
        else:
            self.bookmark_sync.remove_bookmark(self.trakt_id)
Exemple #4
0
    def _trakt_stop_watching(self):
        if (not self.trakt_enabled or not self.scrobbling_enabled
                or self.scrobbled
                or g.get_runtime_setting("marked_watched_dialog_open")
                or self.current_time < self.ignoreSecondsAtStart):
            return

        post_data = self._build_trakt_object()

        if (post_data["progress"] >= self.playCountMinimumPercent
                or self._force_marked_watched):
            post_data["progress"] = (80 if post_data["progress"] < 80 else
                                     post_data["progress"])
            try:
                scrobble_response = self._trakt_api.post(
                    "scrobble/stop", post_data)
            except Exception:
                g.log_stacktrace()
                return
            if scrobble_response.status_code in (201, 409):
                self._trakt_mark_playing_item_watched()
                self.scrobbled = True
        elif self.current_time > self.ignoreSecondsAtStart:
            try:
                scrobble_response = self._trakt_api.post(
                    "scrobble/pause", post_data)
            except Exception:
                g.log_stacktrace()
                return
        else:
            return

        if not scrobble_response.status_code == 201:
            g.log("Error scrobbling item to Trakt")
Exemple #5
0
    def wrapper(*args, **kwarg):
        import requests
        try:
            response = func(*args, **kwarg)
            if response.status_code in [200, 201]:
                return response

            g.log(
                "TMDb returned a {} ({}): while requesting {}".format(
                    response.status_code,
                    TMDBAPI.http_codes[response.status_code],
                    "&".join(x for x in response.url.split('&')
                             if not x.lower().startswith("api_key")),
                ), "warning" if not response.status_code == 404 else "debug")
            return None
        except requests.exceptions.ConnectionError:
            return None
        except Exception:
            xbmcgui.Dialog().notification(
                g.ADDON_NAME,
                g.get_language_string(30024).format("TMDb"))
            if g.get_runtime_setting("run.mode") == "test":
                raise
            else:
                g.log_stacktrace()
            return None
 def _get_download_index(self):
     """
     Refreshes download IDS from window index
     :return:
     """
     index = g.get_runtime_setting("SDMIndex")
     self.download_ids = [i for i in index.split(",")
                          if i] if index is not None else []
 def __init__(self):
     self.limiter = g.get_runtime_setting("threadpool.limiter")
     self.workers = self.scaled_workers[g.get_int_setting("general.threadpoolScale", -1) + 1]
     self.tasks = ClearableQueue(2 * self.workers)
     self.stop_event = threading.Event()
     self.results = None
     self.worker_pool = []
     self.max_workers = 1 if self.limiter else self.workers
     self.exception = None
     self.result_threading_lock = threading.Lock()
     self.workers_threading_lock = threading.Lock()
Exemple #8
0
 def get(self, cache_id, checksum=None):
     cached = g.get_runtime_setting(cache_id)
     cur_time = self._get_timestamp()
     if cached:
         cached = pickle.loads(codecs.decode(cached.encode(), "base64"))
         if cached[0] > cur_time:
             if not checksum or checksum == cached[2]:
                 return cached[1]
         else:
             g.clear_runtime_setting(cache_id)
     return self.NOT_CACHED
 def get_task_info(self, url_hash):
     """
     Takes a task hash and returns the information stored in the Window property
     :param url_hash: Sting
     :return: dict
     """
     try:
         return tools.deconstruct_action_args(
             g.get_runtime_setting("sdm.{}".format(url_hash)))
     except Exception:
         raise TaskDoesNotExist(url_hash)
 def do_cleanup(self):
     if self._exit or g.abort_requested():
         return
     cur_time = datetime.datetime.utcnow()
     if g.get_runtime_setting(self._create_key("cache.db.clean.busy")):
         return
     g.set_runtime_setting(self._create_key("cache.db.clean.busy"), "busy")
     query = "DELETE FROM {} where expires < ?".format(
         self.cache_table_name)
     self.execute_sql(query, (self._get_timestamp(), ))
     g.set_runtime_setting(self._create_key("cache.mem.clean.busy"),
                           repr(cur_time))
     g.clear_runtime_setting(self._create_key("cache.mem.clean.busy"))
Exemple #11
0
    def _mark_watched_dialog(self):
        if g.get_runtime_setting("marked_watched_dialog_open"):
            return

        if (self.getPlayingFile() and self._running_path
                and self._running_path != self.getPlayingFile()
                and self.watched_percentage < self.playCountMinimumPercent
                and (time.time() - self.playback_timestamp) > 600):
            xbmc.sleep(10000)
            g.set_runtime_setting("marked_watched_dialog_open", True)
            if xbmcgui.Dialog().yesno(g.ADDON_NAME,
                                      g.get_language_string(30526)):
                self._force_marked_watched = True
            g.set_runtime_setting("marked_watched_dialog_open", False)
 def check_cleanup(self):
     """
     Check if a cleanup should be run according to auto_clean_interval and process if required
     :return:
     :rtype:
     """
     cur_time = datetime.datetime.utcnow()
     lastexecuted = g.get_runtime_setting(
         self._create_key("clean.lastexecuted"))
     if not lastexecuted:
         g.set_runtime_setting(self._create_key("clean.lastexecuted"),
                               repr(cur_time))
     elif self._cleanup_required_check(lastexecuted, cur_time):
         self.do_cleanup()
    def do_cleanup(self):
        if self._exit or g.abort_requested():
            return
        if g.get_runtime_setting(self._create_key("clean.busy")):
            return
        g.set_runtime_setting(self._create_key("clean.busy"), "busy")

        cur_time = datetime.datetime.utcnow()

        self._db_cache.do_cleanup()
        self._mem_cache.do_cleanup()

        g.set_runtime_setting(self._create_key("clean.lastexecuted"),
                              repr(cur_time))
        g.clear_runtime_setting(self._create_key("clean.busy"))
    def do_cleanup(self):
        if self._exit or g.abort_requested():
            return
        cur_time = datetime.datetime.utcnow()
        cur_timestamp = self._get_timestamp()
        if g.get_runtime_setting(self._create_key("cache.mem.clean.busy")):
            return
        g.set_runtime_setting(self._create_key("cache.mem.clean.busy"), "busy")

        self._get_index()
        for cache_id, expires in self._index:
            if expires < cur_timestamp:
                g.clear_runtime_setting(cache_id)

        g.set_runtime_setting(self._create_key("cache.mem.clean.busy"),
                              repr(cur_time))
        g.clear_runtime_setting(self._create_key("cache.mem.clean.busy"))
        def _decorated(*args, **kwargs):
            method_class = args[0]

            for a in args[1:]:
                if isinstance(a, types.GeneratorType):
                    raise UnsupportedCacheParamException("generator")

            for k, v in kwargs.items():
                if isinstance(v, types.GeneratorType):
                    raise UnsupportedCacheParamException("generator")

            try:
                global_cache_ignore = g.get_runtime_setting("ignore.cache")
            except Exception:
                global_cache_ignore = False
            checksum = _get_checksum(method_class.__class__.__name__,
                                     func.__name__)
            ignore_cache = kwargs.pop("ignore_cache", False)
            if ignore_cache or global_cache_ignore:
                return func(*args, **kwargs)
            overwrite_cache = kwargs.pop("overwrite_cache", False)
            hours = kwargs.pop("cache_hours", cache_hours)
            cache_str = "{}.{}.{}.{}".format(method_class.__class__.__name__,
                                             func.__name__,
                                             tools.md5_hash(args[1:]),
                                             tools.md5_hash(kwargs))
            cached_data = g.CACHE.get(cache_str, checksum=checksum)
            if cached_data == CacheBase.NOT_CACHED or overwrite_cache:
                fresh_result = func(*args, **kwargs)
                if func.__name__ == "get_sources" and (not fresh_result or len(
                        fresh_result[1]) == 0):
                    return fresh_result
                try:
                    g.CACHE.set(
                        cache_str,
                        fresh_result,
                        expiration=datetime.timedelta(hours=hours),
                        checksum=checksum,
                    )
                except TypeError:
                    g.log_stacktrace()
                    pass
                return fresh_result
            else:
                return cached_data
Exemple #16
0
    def _get_sort_methods(self):
        """
        Get Seren settings for sort methods
        """
        sort_methods = []
        sort_method_settings = {
            0: None,
            1: self._get_quality_sort_key,
            2: self._get_type_sort_key,
            3: self._get_debrid_priority_key,
            4: self._get_size_sort_key,
            5: self._get_low_cam_sort_key,
            6: self._get_hevc_sort_key,
            7: self._get_hdr_sort_key,
            8: self._get_audio_channels_sort_key
        }

        if self.mediatype == g.MEDIA_EPISODE and g.get_bool_setting("general.lastreleasenamepriority"):
            self.last_release_name = g.get_runtime_setting(
                "last_resolved_release_title.{}".format(self.item_information['info']['trakt_show_id'])
            )
            if self.last_release_name:
                sort_methods.append((self._get_last_release_name_sort_key, False))

        for i in range(1, 9):
            sm = g.get_int_setting("general.sortmethod.{}".format(i))
            reverse = g.get_bool_setting("general.sortmethod.{}.reverse".format(i))

            if sort_method_settings[sm] is None:
                break

            if sort_method_settings[sm] == self._get_type_sort_key:
                self._get_type_sort_order()
            if sort_method_settings[sm] == self._get_debrid_priority_key:
                self._get_debrid_sort_order()
            if sort_method_settings[sm] == self._get_hdr_sort_key:
                self._get_hdr_sort_order()

            sort_methods.append((sort_method_settings[sm], reverse))

        self.sort_methods = sort_methods
Exemple #17
0
    def wrapper(*args, **kwarg):
        method_class = args[0]
        try:
            response = func(*args, **kwarg)
            if response.status_code in [200, 201]:
                return response

            if response.status_code == 401:
                try:
                    with GlobalLock("tvdb.oauth",
                                    run_once=True,
                                    check_sum=method_class.jwToken) as lock:
                        if method_class.jwToken is not None:
                            method_class.try_refresh_token(True)
                except RanOnceAlready:
                    pass
                if method_class.refresh_token is not None:
                    return func(*args, **kwarg)

            g.log(
                "TVDB returned a {} ({}): while requesting {}".format(
                    response.status_code,
                    TVDBAPI.http_codes[response.status_code]
                    if not response.status_code == 404 else
                    response.json()["Error"],
                    response.url,
                ),
                "warning" if not response.status_code == 404 else "debug",
            )
            return None
        except requests.exceptions.ConnectionError:
            return None
        except Exception:
            xbmcgui.Dialog().notification(
                g.ADDON_NAME,
                g.get_language_string(30025).format("TVDB"))
            if g.get_runtime_setting("run.mode") == "test":
                raise
            else:
                g.log_stacktrace()
            return None
Exemple #18
0
    def wrapper(*args, **kwarg):
        try:
            response = func(*args, **kwarg)
            if response.status_code in [200, 201]:
                return response

            if "Retry-After" in response.headers:
                # API REQUESTS Are not being throttled anymore but we leave it here for if the re-enable it again
                throttle_time = response.headers["Retry-After"]
                g.log(
                    "TMDb Throttling Applied, Sleeping for {} seconds".format(
                        throttle_time),
                    "",
                )
                xbmc.sleep((int(throttle_time) * 1000) + 1)
                return wrapper(*args, **kwarg)

            g.log(
                "TMDb returned a {} ({}): while requesting {}".format(
                    response.status_code,
                    TMDBAPI.http_codes[response.status_code],
                    "&".join(x for x in response.url.split('&')
                             if not x.lower().startswith("api_key")),
                ), "warning" if not response.status_code == 404 else "debug")
            return None
        except requests.exceptions.ConnectionError:
            return None
        except Exception:
            xbmcgui.Dialog().notification(
                g.ADDON_NAME,
                g.get_language_string(30024).format("TMDb"))
            if g.get_runtime_setting("run.mode") == "test":
                raise
            else:
                g.log_stacktrace()
            return None
Exemple #19
0
 def _get_index(self):
     index = g.get_runtime_setting(self._index_key)
     if index:
         self._index = set(index)
Exemple #20
0
 def _check_ran_once_already(self):
     if g.get_bool_runtime_setting(self._create_key("RunOnce")) and \
            g.get_runtime_setting(self._create_key("CheckSum")) == self._check_sum:
         g.clear_runtime_setting(self._create_key("Running"))
         raise RanOnceAlready("Lock name: {}, Checksum: {}".format(self._lock_name, self._check_sum))