예제 #1
0
class APIHandler:
    """
	The class used to communicate with Trackma.
	"""
    comparision = {
        "mal_ID": "id",
        "title": "title",
        "episodes_done": "my_progress",
        "watch_status": "my_status",
        "score": "my_score",
        "_len": "total"
    }
    watch = None
    accs = None
    engine = None
    adList = []
    tList = []

    def __init__(self, accountnum=1):
        """
		Defaults to the first account.
		"""
        self.watch = Watcher()
        self.accs = dict(AccountManager().get_accounts())
        self.engine = Engine(self.accs.get(accountnum))
        self.engine.start()
        self.tList = list(self.engine.get_list())
        with open(self.watch.WATCH_FILE, 'r') as watch_file:
            self.adList = list(json.load(watch_file))
            watch_file.close()
        self._sort_lists()

    def _sort_lists(self, key="mal_ID"):
        """
		Sorts lists for easier comparision.\n
		Called on initializing the class.
		Mutilates the lists.
		"""
        self.adList.sort(key=lambda val: val[key])
        self.tList.sort(key=lambda val: val[self.comparision[key]])

    def _equalize_lists(self, format=False):
        """
		Strips both the lists to the common categories and returns it
		as two sublists with the animedl list being first.
		"""
        tempList = [[], []]

        for i in range(len(self.tList)):
            entry = [{}, {}]
            for cat in self.comparision:

                entry[0][self.comparision[cat]
                         if format else cat] = self.adList[i][cat]

                entry[1][self.comparision[cat] if format else
                         cat] = "planned" if self.tList[i][self.comparision[
                             cat]] == "plan_to_watch" else self.tList[i][
                                 self.comparision[cat]]

            tempList[0].append(entry[0])
            tempList[1].append(entry[1])
        return tempList

    def _stage_changes(self, preference=True):
        """
		Returns the modified items in the animedl list (compares to Trackma).\n
		Replaces it with the Trackma entry, to reverse this
		set preference to False.
		"""
        (sadList, stList) = self._equalize_lists()
        tempList = []
        for i in range(len(stList)):
            if (sadList[i] != stList[i]):
                tempList.append((sadList[i] if preference else stList[i]))
        return tempList

    def add_staged_to_trackma(self):
        """
		Updates the Trackma queue.
		"""
        qList = self._stage_changes()
        for item in qList:
            self.engine.get_show_info(item["mal_ID"])

    """