Exemplo n.º 1
0
async def save_thumb(comic_id, c_image_source):
    def got_thumb(results):
        pass

    fetch_data = ComicServerConn()
    app = App.get_running_app()
    id_folder = app.store_dir
    my_thumb_dir = Path(os.path.join(id_folder, "comic_thumbs"))

    if not my_thumb_dir.is_dir():
        os.makedirs(my_thumb_dir)
    file_name = f"{comic_id}.jpg"
    t_file = os.path.join(my_thumb_dir, file_name)
    fetch_data.get_server_file_download(
        c_image_source,
        callback=lambda req, results: got_thumb(results),
        file_path=os.path.join(my_thumb_dir, t_file),
    )
Exemplo n.º 2
0
class ComicReadingList(EventDispatcher):

    # ids = DictProperty({})
    name = StringProperty()
    comics = ListProperty()
    data = DictProperty()
    slug = StringProperty()
    comic_db = ObjectProperty()
    comic_json = ListProperty()
    cb_only_read_active = BooleanProperty(False)
    cb_only_read_active = BooleanProperty(False)
    cb_purge_active = BooleanProperty(False)
    cb_optimize_size_active = BooleanProperty(False)
    cb_limit_active = BooleanProperty(False)
    limit_num = NumericProperty(25)
    sw_syn_this_active = BooleanProperty(False)
    comic_db_in = BooleanProperty(False)
    db = ObjectProperty()
    comics_loaded = ObjectProperty(False)
    last_comic_read = NumericProperty()
    start_last_sync_num = NumericProperty(0)
    end_last_sync_num = NumericProperty(0)
    totalCount = NumericProperty()
    pickled_data = ObjectProperty()
    sync_list = ListProperty()

    def __init__(self, name="", data=None, slug="", mode="Server"):
        self.slug = slug
        self.name = name
        self.event = None
        if data != "db_data":

            self.pickled_data = pickle.dumps(data, -1)
            self.data = pickle.loads(self.pickled_data)
            self.comic_json = self.data["items"]
            if mode != "FileOpen":
                if name == "Single_FileLoad":
                    self.totalCount = 0
                else:
                    self.totalCount = self.data["totalCount"]
        if mode != "FileOpen":
            self.get_or_create_db_item(mode=mode)

    def add_comic(self, comic, index=0):
        """
            Add Single comic book to this colection
        """
        self.comics.insert(0, comic)

    def get_or_create_db_item(self, mode):
        tmp_defaults = {}
        try:
            for key in READINGLIST_DB_KEYS:
                if key == "data":
                    new_dict = {k: self.data[k] for k in self.data.keys()}
                    tmp_defaults["data"] = new_dict
                else:
                    tmp_defaults[key] = getattr(self, key)
            db_item, created = ReadingList.get_or_create(
                slug=self.slug, defaults=tmp_defaults
            )
            self.db = db_item
            if db_item:
                for key in READINGLIST_DB_KEYS:
                    setattr(self, key, getattr(db_item, key))
                if created is True:
                    len_dbcomics = len(db_item.comics)
                    if (
                        len_dbcomics == len(self.comic_json)
                        and len(self.comic_json) != 0
                    ):
                        self.comic_db_in = True
                        self.comics = self.db.comics.order_by(
                            -Comic.comic_index.index
                        )
                else:
                    self.comic_db_in = True
                    comicindex_db = ComicIndex.get(
                        ComicIndex.readinglist == self.slug
                    )
                    if mode == "local_file":
                        list_comics = self.db.comics.where(
                            Comic.is_sync == True, Comic.local_file != ""
                        ).order_by(  # noqa
                            comicindex_db.index
                        )
                        print(f"len:{len(list_comics)}")
                    else:
                        list_comics = self.db.comics.order_by(
                            comicindex_db.index
                        )
                    for comic in list_comics:
                        new_comic = ComicBook(
                            comic_Id=comic.Id,
                            readlist_obj=self,
                            mode="db_data",
                        )
                        self.comics.append(new_comic)
                    self.comics_loaded = True
        except peewee.OperationalError:
            Logger.critical(
                "Somthing happened in get_or_create of readinglist"
            )

    def save_settings(self, *args, **kwargs):
        try:
            rl = ReadingList.get(ReadingList.slug == self.slug)
            for key in READINGLIST_SETTINGS_KEYS:
                setattr(rl, key, kwargs[key])
                setattr(self, key, kwargs[key])
            rl.save()
        except peewee.OperationalError:
            pass

    def do_db_refresh(self, screen=None):
        def __finish_toast(dt):
            app = App.get_running_app()
            screen = app.manager.get_screen("server_readinglists_screen")
            screen.refresh_callback()
            toast("DataBase Refresh Complete")

        def __got_readlist_data(results):
            def __updated_progress(results):
                pass

            the_keys = [
                "Id",
                "Series",
                "Number",
                "Volume",
                "Year",
                "Month",
                "UserCurrentPage",
                "UserLastPageRead",
                "PageCount",
                "Summary",
                "FilePath",
            ]
            for server_comic in results["items"]:
                for db_comic in self.comics:
                    if db_comic.Id == server_comic["Id"]:
                        for key in the_keys:
                            if getattr(db_comic, key) != server_comic[key]:
                                if key in (
                                    "UserCurrentPage",
                                    "UserLastPageRead",
                                ) and (db_comic.is_sync):
                                    if (
                                        db_comic.UserLastPageRead
                                        > server_comic["UserLastPageRead"]
                                    ) or (
                                        db_comic.UserCurrentPage
                                        > server_comic["UserCurrentPage"]
                                    ):
                                        if (
                                            db_comic.UserCurrentPage
                                            > db_comic.UserLastPageRead
                                        ):
                                            current_page = (
                                                db_comic.UserCurrentPage
                                            )  # noqa
                                        else:
                                            current_page = (
                                                db_comic.UserLastPageRead
                                            )  # noqa
                                        update_url = "{}/Comics/{}/Progress".format(
                                            api_url, db_comic.Id
                                        )
                                        self.fetch_data.update_progress(
                                            update_url,
                                            current_page,
                                            callback=lambda req, results: __updated_progress(
                                                results
                                            ),
                                        )
                                    else:
                                        x_str = db_comic.__str__
                                        Logger.info(
                                            "Updating DB Record for {} of {}".format(
                                                key, x_str
                                            )
                                        )
                                        toast(
                                            "Updating DB Record for {} of {}".format(
                                                key, x_str
                                            )
                                        )
                                        db_item = Comic.get(
                                            Comic.Id == db_comic.Id
                                        )
                                        if db_item:
                                            setattr(
                                                db_item, key, server_comic[key]
                                            )
                                            db_item.save()
                                            setattr(self, key, db_item)

            Clock.schedule_once(__finish_toast, 3)

        self.fetch_data = ComicServerConn()
        app = App.get_running_app()
        api_url = app.api_url
        server_url = f"{api_url}/Lists/{self.slug}/Comics/"

        self.fetch_data.get_server_data_callback(
            server_url,
            callback=lambda req, results: __got_readlist_data(results),
        )

    def get_last_comic_read(self):
        last_read_comic = 0
        for comic in self.comics:
            if (
                comic.UserLastPageRead == comic.PageCount - 1
                and comic.PageCount > 1
            ):
                last_read_comic = self.comics.index(comic)
        return last_read_comic

    def do_sync(self):
        def _syncrun_callback(*args):
            pass

        app = App.get_running_app()
        if app.sync_is_running is True:
            self.please_wait_dialog = MDDialog(
                title="Sync Already in Progress",
                size_hint=(0.8, 0.4),
                text_button_ok="Ok",
                text=f"Please wait till current Sync is done",
                events_callback=_syncrun_callback,
            )
            self.please_wait_dialog.open()
            return
        self.num_file_done = 0
        sync_range = 0
        self.fetch_data = ComicServerConn()
        rl_db = ReadingList.get(ReadingList.slug == self.slug)

        end_last_sync_num = rl_db.end_last_sync_num
        if end_last_sync_num != 0:
            end_last_sync_num = end_last_sync_num - 1
        comicindex_db = ComicIndex.get(ComicIndex.readinglist == self.slug)
        last_read_comic_db = self.db.comics.where(
            (Comic.UserLastPageRead == Comic.PageCount - 1)
            & (Comic.PageCount > 1)
        ).order_by(comicindex_db.index)
        if len(last_read_comic_db) > 1:
            last_read_index = ComicIndex.get(
                ComicIndex.comic == last_read_comic_db[-1].Id,
                ComicIndex.readinglist == self.slug,
            ).index
        elif len(last_read_comic_db) != 0:
            last_read_index = ComicIndex.get(
                ComicIndex.comic == last_read_comic_db[0].Id,
                ComicIndex.readinglist == self.slug,
            ).index
        else:
            last_read_index = 0
        if self.cb_limit_active:
            if self.cb_only_read_active:
                list_comics = self.db.comics.where(
                    ~(Comic.UserLastPageRead == Comic.PageCount - 1)
                    & (Comic.PageCount > 1)
                    & (Comic.been_sync)
                    != True
                ).order_by(
                    comicindex_db.index
                )  # noqa: E712
                if last_read_index < end_last_sync_num:
                    sync_range = int(self.limit_num)
                    tmp_comic_list = list_comics[0:int(sync_range)]
                else:
                    sync_range = int(end_last_sync_num) + int(self.limit_num)
                    tmp_comic_list = list_comics[end_last_sync_num:int(sync_range)]
                purge_list = self.db.comics.where(
                    (Comic.UserLastPageRead == Comic.PageCount - 1)
                    & (Comic.PageCount > 1)
                    & (Comic.is_sync == True)
                ).order_by(
                    comicindex_db.index
                )  # noqa: E712
            else:
                list_comics = (
                    Comic.select()
                    .where(
                        (Comic.is_sync == False) & (Comic.been_sync == False)
                    )
                    .order_by(comicindex_db.index)
                )  # noqa: E712,E501
                sync_range = int(self.limit_num)
                tmp_comic_list = list_comics[0:int(sync_range)]
                purge_list = self.db.comics.where(
                    Comic.is_sync == True
                ).order_by(
                    comicindex_db.index
                )  # noqa: E712
        else:
            sync_range = int(len(self.comics))
            # rl_db.end_last_sync_num = new_end_last_sync_num
            # rl_db.save()
            if self.cb_only_read_active:
                list_comics = self.db.comics.where(
                    ~(Comic.UserLastPageRead == Comic.PageCount - 1)
                    & (Comic.PageCount > 1)
                ).order_by(
                    comicindex_db.index
                )  # noqa: E712
                tmp_comic_list = list_comics[0:int(sync_range)]
            else:
                list_comics = self.db.comics.where(
                    (Comic.is_sync == False) & (Comic.been_sync == False)
                ).order_by(
                    comicindex_db.index
                )  # noqa: E712,E501
                tmp_comic_list = list_comics
        db_item = ReadingList.get(ReadingList.slug == self.slug)
        for key in READINGLIST_SETTINGS_KEYS:
            v = getattr(db_item, key)
            globals()["%s" % key] = v
        app = App.get_running_app()
        id_folder = os.path.join(app.sync_folder, self.slug)
        my_comic_dir = Path(os.path.join(id_folder, "comics"))
        if os.path.isdir(my_comic_dir):
            print(f"{get_size(my_comic_dir)/1000000} MB")
        sync_comic_list = []
        for comic in tmp_comic_list:
            if comic.is_sync is False:
                sync_comic_list.append(comic)
        if self.cb_purge_active:
            for item in purge_list:
                os.remove(item.local_file)
                db_comic = Comic.get(Comic.Id == item.Id)
                db_comic.is_sync = False
                db_comic.local_file = ""
                db_comic.save()
                server_readinglists_screen = app.manager.get_screen(
                    "server_readinglists_screen"
                )
                server_readinglists_screen.file_sync_update(item.Id, False)
        self.sync_readinglist(comic_list=sync_comic_list)

    def get_server_file_download(self, req_url, callback, file_path):
        def is_finished(dt):
            if req.is_finished is True:
                app = App.get_running_app()
                screen = app.manager.get_screen("server_readinglists_screen")
                screen.ids.sync_button.enabled = True
                Clock.schedule_once(self.download_file)
            else:
                Clock.schedule_once(is_finished, 0.25)

        app = App.get_running_app()
        username = app.config.get("General", "username")
        api_key = app.config.get("General", "api_key")
        str_cookie = f"API_apiKey={api_key}; BCR_username={username}"
        head = {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Cookie": str_cookie,
        }

        req = UrlRequest(
            req_url, req_headers=head, on_success=callback, file_path=file_path
        )
        app = App.get_running_app()
        screen = app.manager.get_screen("server_readinglists_screen")
        if len(self.sync_list) != 0:
            screen.ids.sync_status_lbl.text = (
                f"Sync is Running Left in Que: {len(self.sync_list)}"
            )
            Clock.schedule_once(is_finished, 0.25)
        else:
            toast("Reading List has been Synced, Refreshing Screen")
            screen.ids.sync_status_lbl.text = ""
            screen.ids.sync_button.enabled = True
            app.sync_is_running = False
            # screen.refresh_callback()

    def got_file(self, comic_obj, comic_file="", *args, **kwargs):
        def file_finished_toast(dt):
            toast(f"{os.path.basename(comic_file)} Synced")
        self.num_file_done += 1
        Clock.schedule_once(file_finished_toast)
        self.file_download = True
        db_comic = Comic.get(Comic.Id == comic_obj.Id)
        db_comic.is_sync = True
        db_comic.save()
        db_comic = Comic.get(Comic.Id == comic_obj.Id)
        db_comic.local_file = comic_file
        db_comic.been_sync = True
        db_comic.save()
        rl_db = ReadingList.get(ReadingList.slug == self.slug)
        rl_db.end_last_sync_num += 1
        rl_db.save()
        app = App.get_running_app()
        server_readinglists_screen = app.manager.get_screen(
            "server_readinglists_screen"
        )
        server_readinglists_screen.file_sync_update(comic_obj.Id, True)

    def download_file(self, dt):
        def got_thumb(results):
            pass

        app = App.get_running_app()
        screen = app.manager.get_screen("server_readinglists_screen")
        screen.ids.sync_button.enabled = False
        if len(self.sync_list) == 0:
            toast("Reading List has been Synced, Refreshing Screen")
            app = App.get_running_app()
            screen = app.manager.get_screen("server_readinglists_screen")
            screen.ids.sync_status_lbl.text = ""
            screen.ids.sync_button.enabled = True
            app.sync_is_running = False
            # screen.refresh_callback()
            return
        comic = self.sync_list.pop(0)
        self.file_download = False
        file_name = ntpath.basename(comic.FilePath)
        y = 240
        part_url = f"/Comics/{comic.Id}/Pages/0?"
        app = App.get_running_app()
        part_api = f"&apiKey={app.api_key}&height={round(dp(y))}"
        thumb_url = f"{app.api_url}{part_url}{part_api}"

        if self.cb_optimize_size_active is False:
            sync_url = f"{app.api_url}/Comics/{comic.Id}/Sync/File/"
        elif self.cb_optimize_size_active is True:
            sync_url = f"{app.api_url}/Comics/{comic.Id}/Sync/Webp"
        app = App.get_running_app()
        id_folder = os.path.join(app.sync_folder, self.slug)
        self.my_comic_dir = Path(os.path.join(id_folder, "comics"))
        self.my_thumb_dir = Path(os.path.join(id_folder, "thumb"))
        if not self.my_comic_dir.is_dir():
            os.makedirs(self.my_comic_dir)
        if not self.my_thumb_dir.is_dir():
            os.makedirs(self.my_thumb_dir)
        t_file = os.path.join(self.my_comic_dir, file_name)
        self.get_server_file_download(
            sync_url,
            callback=self.got_file(comic, comic_file=t_file),
            file_path=t_file,
        )
        thumb_name = f"{comic.Id}.jpg"
        self.fetch_data.get_server_file_download(
            thumb_url,
            callback=lambda req, results: got_thumb(results),
            file_path=os.path.join(self.my_thumb_dir, thumb_name),
        )

    def _finish_sync(self, comic_list, *largs):
        def __finish_toast(dt):
            toast("Reading List has been Synced, Refreshing Screen")
            # app = App.get_running_app()
            # screen = app.manager.get_screen("server_readinglists_screen")
            # screen.refresh_callback()

        list_comics = comic_list
        num_comic = len(list_comics)
        if self.num_file_done == num_comic:
            Clock.schedule_once(__finish_toast, 3)
            self.event.cancel()
            self.event = None

    def sync_readinglist(self, comic_list=[]):
        app = App.get_running_app()
        self.sync_list = comic_list
        app = App.get_running_app()
        screen = app.manager.get_screen("server_readinglists_screen")
        screen.ids.sync_status_lbl.text = (
            f"Sync is Running Comics to Sync: {len(self.sync_list)}"
        )
        app.sync_is_running = True
        screen.ids.sync_button.enabled = False
        Clock.schedule_once(self.download_file)
Exemplo n.º 3
0
class SyncReadingListObject(object):
    cb_only_read_state = 'normal'
    cb_keep_last_read_state = 'normal'
    cb_optimize_size_state = 'normal'
    cb_limit_state = 'normal'
    limit_num = 25
    sw_syn_this_active = False

    def __init__(self, reading_list=None, **kwords):
        self.reading_list = reading_list

        self.app = App.get_running_app()
        self.api_url = self.app.api_url
        self.fetch_data = ComicServerConn()
        self.num_file_done = 0
        self.comic_thumb_height = 240
        self.comic_thumb_width = 156
        id_folder = os.path.join(self.app.sync_folder, self.reading_list.slug)
        my_data_dir = Path(os.path.join(id_folder, 'data'))
        self.my_comic_dir = Path(os.path.join(id_folder, 'comics'))
        self.my_thumb_dir = Path(os.path.join(self.my_comic_dir, 'thumb'))
        if not self.my_comic_dir.is_dir():
            os.makedirs(self.my_comic_dir)
        if not self.my_thumb_dir.is_dir():
            os.makedirs(self.my_thumb_dir)
        # for item in SYNC_SETTINGS_ITEMS:
        #    val = ""
        #    tmp_defaults[key] = getattr(self, key)
        # NOTE: can be removed once DB functions added
        if not my_data_dir.is_dir():
            os.makedirs(my_data_dir)
        settings_json = os.path.join(my_data_dir, 'settings.json')
        comics_json = os.path.join(my_data_dir, 'sync_comics.json')
        self.this_test = "Test"
        self.sync_data = JsonStore(settings_json)
        self.sync_comics = JsonStore(comics_json)
        self.last_read_comic = self.get_last_comic_read()
        if self.sync_data.exists('options'):
            self.cb_only_read_state = self.sync_data.get(
                "options")["cb_only_read_state"]
            self.cb_keep_last_read_state = self.sync_data.get(
                "options")["cb_keep_last_read_state"]
            self.cb_optimize_size_state = self.sync_data.get(
                "options")["cb_optimize_size_state"]
            self.cb_limit_state = self.sync_data.get("options")[
                "cb_limit_state"]
            self.limit_num = self.sync_data.get("options")["limit_num"]
            self.sw_syn_this_active = self.sync_data.get("options")[
                'sw_syn_this_active']
        else:
            self.cb_only_read_state = 'normal'
            self.cb_keep_last_read_state = 'normal'
            self.cb_optimize_size_state = 'normal'
            self.cb_limit_state = 'normal'
            self.limit_num = 25
            self.sw_syn_this_active = False
        # end note

        self.last = 0
        self.limit = 25
        self.sync_range = int(self.last) + int(self.limit_num)
        self.api_key = self.app.config.get('General', 'api_key')

    def get_comics(self):
        self.sync_comics.get()

    def get_last_comic_read(self):
        last_read_comic = "None Read"
        for comic in self.reading_list.comics:
            if comic.UserLastPageRead == comic.PageCount-1:
                last_read_comic = self.reading_list.comics.index(comic)

        return last_read_comic

    def save_values(self,
                    cb_limit_state=cb_limit_state,
                    limit_num=limit_num,
                    cb_only_read_state=cb_only_read_state,
                    cb_keep_last_read_state=cb_keep_last_read_state,
                    cb_optimize_size_state=cb_optimize_size_state,
                    sw_syn_this_active=sw_syn_this_active,
                    *args, **kwargs):
        # self.sync_data.put()

        self.sync_data.put('options',
                           cb_limit_state=cb_limit_state,
                           limit_num=limit_num,
                           cb_only_read_state=cb_only_read_state,
                           cb_keep_last_read_state=cb_keep_last_read_state,
                           cb_optimize_size_state=cb_optimize_size_state,
                           sw_syn_this_active=sw_syn_this_active
                           )
        self.cb_only_read_state = cb_only_read_state
        self.cb_keep_last_read_state = cb_keep_last_read_state
        self.cb_optimize_size_state = cb_optimize_size_state
        self.cb_limit_state = cb_limit_state
        self.limit_num = limit_num
        self.sync_range = int(self.last) + int(self.limit_num)
        self.sw_syn_this_active = sw_syn_this_active

    def download_file(self, comic):
        comic_index = 0
        self.file_download = False
        file_name = ntpath.basename(comic.FilePath)
        for i, j in enumerate(self.reading_list.comics):
            if j.Id == comic.Id:
                comic_index = i

        def got_file(results):
            self.num_file_done += 1
            toast(f'{file_name} Synced')
            self.file_download = True
            self.sync_comics.put(comic.Id,
                                 file=file_name,
                                 slug=comic.slug,
                                 data_type='ComicBook',
                                 Series=comic.Series,
                                 Number=comic.Number,
                                 Month=comic.Month,
                                 Year=comic.Year,
                                 UserCurrentPage=comic.UserCurrentPage,
                                 UserLastPageRead=comic.UserLastPageRead,
                                 PageCount=comic.PageCount,
                                 Summary=comic.Summary,
                                 Index=comic_index,
                                 FilePath=comic.FilePath,
                                 ReadlistID=self.reading_list.slug
                                 )

        def got_thumb(results):
            pass

        x = self.comic_thumb_width
        y = self.comic_thumb_height
        thumb_size = f'height={y}&width={x}'
        part_url = f'/Comics/{comic.Id}/Pages/0?'
        part_api = f'&apiKey={self.api_key}&height={round(dp(y))}'
        thumb_url = f"{self.api_url}{part_url}{part_api}"

        if self.cb_optimize_size_state == 'normal':
            sync_url = f'{self.api_url}/Comics/{comic.Id}/File/'
        elif self.cb_optimize_size_state == 'down':
            sync_url = f'{self.api_url}/Comics/{comic.Id}/Sync/'

        self.fetch_data.get_server_file_download(
            sync_url, callback=lambda req,
            results: got_file(results),
            file_path=os.path.join(self.my_comic_dir, file_name)

        )
        thumb_name = f'{comic.Id}.jpg'
        self.fetch_data.get_server_file_download(thumb_url,
                                                 callback=lambda req, results: got_thumb(
                                                     results),
                                                 file_path=os.path.join(
                                                     self.my_thumb_dir, thumb_name)

                                                 )

    def _finish_sync(self, dt):
        def _finish_toast(dt):
            toast('Reading List has been Synceddd')

        list_comics = self.reading_list.comics
        sync_num_comics = list_comics[self.last: self.sync_range]
        num_comic = len(sync_num_comics)
        if self.num_file_done == num_comic:
            Clock.schedule_once(_finish_toast, 3)
            self.event.cancel()

    def sync_readinglist(self):
        list_comics = self.reading_list.comics
        sync_num_comics = list_comics[self.last: self.sync_range]

        self.app.delayed_work(
            self.download_file, sync_num_comics, delay=.5)
        self.event = Clock.schedule_interval(self._finish_sync, 0.5)