예제 #1
0
def test_split_backslash_last():
    assert (
        pure.split_backslash_last("https://www.pixiv.net/en/users/2232374") == "2232374"
    )
    assert (
        pure.split_backslash_last("https://www.pixiv.net/en/artworks/78823485")
        == "78823485"
    )
예제 #2
0
 def show_full_res(self) -> 'IO':
     # FIXME: some images that need to be downloaded in png won't work
     # Can use verified function above
     large_url = pure.change_url_to_full(self.current_url)
     filename = pure.split_backslash_last(large_url)
     download.download_url(self.download_path, large_url, filename)
     lscat.icat(self.download_path / filename)
예제 #3
0
    def _go_next_image(self):
        """
        Downloads next image if not downloaded, open it, download the next image
        in the background
        """
        # IDEAL: image prompt should not be blocked while downloading
        # But I think delaying the prompt is better than waiting for an image
        # to download when you load it

        # First time from gallery; download next image
        if self.data.img_post_page_num == 1:
            download.async_download_spinner(self.data.large_dir,
                                            [self.data.current_url()])

        utils.display_image_vp(self.data.filepath())

        # Downloads the next image
        try:
            next_img_url = self.data.next_img_url()
        except IndexError:  # Last page
            pass
        else:  # No error
            self.data.downloaded_images.append(
                pure.split_backslash_last(next_img_url))
            download.async_download_spinner(self.data.large_dir,
                                            [next_img_url])

        print(
            f'Page {self.data.img_post_page_num+1}/{self.data.number_of_pages}'
        )
예제 #4
0
def display_image(post_json, artist_user_id, number_prefix, current_page_num):
    """
    Opens image given by the number (medium-res), downloads large-res and
    then display that.

    Parameters
    ----------
    number_prefix : int
        The number prefixed in each image
    post_json : JsonDict
    artist_user_id : int
    current_page_num : int
    """
    search_string = f"{str(number_prefix).rjust(3, '0')}_"

    # LSCAT
    os.system('clear')
    arg = f'{KONEKODIR}/{artist_user_id}/{current_page_num}/{search_string}*'
    os.system(f'kitty +kitten icat --silent {arg}')

    url = pure.url_given_size(post_json, 'large')
    filename = pure.split_backslash_last(url)
    large_dir = f'{KONEKODIR}/{artist_user_id}/{current_page_num}/large/'
    download.download_core(large_dir, url, filename)

    # BLOCKING: imput is blocking, will not display large image until input
    # received

    # LSCAT
    os.system('clear')
    arg = f'{KONEKODIR}/{artist_user_id}/{current_page_num}/large/{filename}'
    os.system(f'kitty +kitten icat --silent {arg}')
예제 #5
0
    def preview(self) -> 'IO':
        """Download the next four images in the background and/or display them
        one at a time, so if user interrupts, it won't hang.
        """
        tracker = lscat.TrackDownloadsImage(self)
        i = 1
        while (not self.event.is_set() and i <= 4
               and self.page_num + i < self.number_of_pages):

            url = self.page_urls[self.page_num + i]
            name = pure.split_backslash_last(url)
            path = self.download_path / name

            if path.is_file():
                tracker.update(name)
            else:
                download.async_download_no_rename(self.download_path, [url],
                                                  tracker=tracker)

            if i == 4:  # Last pic
                printer.move_cursor_xy(self.loc[0], self.loc[1])

            self.preview_images = tracker.images

            i += 1
예제 #6
0
def download_image_verified(image_id=None,
                            post_json=None,
                            png=False,
                            **kwargs):
    """
    This downloads an image, checks if it's valid. If not, retry with png.
    Used for downloading full-res, single only; on-user-demand
    """
    if png and 'url' in kwargs:  # Called from recursion
        # IMPROVEMENT This is copied from full_img_details()...
        url = pure.change_url_to_full(url=kwargs['url'], png=True)
        filename = pure.split_backslash_last(url)
        filepath = pure.generate_filepath(filename)

    elif not kwargs:
        url, filename, filepath = full_img_details(image_id=image_id,
                                                   post_json=post_json,
                                                   png=png)
    else:
        url = kwargs['url']
        filename = kwargs['filename']
        filepath = kwargs['filepath']

    download_path = Path('~/Downloads').expanduser()
    download_core(download_path, url, filename, try_make_dir=False)

    verified = utils.verify_full_download(filepath)
    if not verified:
        download_image_verified(url=url, png=True)
    else:
        print(f'Image downloaded at {filepath}\n')
예제 #7
0
 def download_image(self):
     # Need to work on multi-image posts
     # Doing the same job as full_img_details
     large_url = pure.change_url_to_full(url=self.data.current_url())
     filename = pure.split_backslash_last(large_url)
     filepath = pure.generate_filepath(filename)
     download.download_image_verified(url=large_url,
                                      filename=filename,
                                      filepath=filepath)
예제 #8
0
    def _process_url_or_input(self):
        """Overriding base class to account for 'illust_id' cases"""
        if 'illust_id' in self._url_or_id:
            reg = re.findall(r'&illust_id.*', self._url_or_id)
            self._user_input = reg[0].split('=')[-1]

        elif 'pixiv' in self._url_or_id:
            self._user_input = pure.split_backslash_last(self._url_or_id)
        else:
            self._user_input = self._url_or_id
예제 #9
0
def full_img_details(png=False, post_json=None, image_id=None):
    """
    All in one function that gets the full-resolution url, filename, and
    filepath of given image id. Or it can get the id given the post json
    """
    if image_id and not post_json:
        current_image = api.myapi.protected_illust_detail(image_id)

        post_json = current_image.illust

    url = pure.change_url_to_full(post_json=post_json, png=png)
    filename = pure.split_backslash_last(url)
    filepath = pure.generate_filepath(filename)
    return url, filename, filepath
예제 #10
0
    def __init__(self, raw, image_id):
        self._raw = raw
        self.url = pure.url_given_size(self._raw, 'large')
        self.filename = pure.split_backslash_last(self.url)
        self.artist_user_id = self._raw['user']['id']
        self.img_post_page_num = 0

        self.number_of_pages, self.page_urls = pure.page_urls_in_post(
            self._raw, 'large')
        if self.number_of_pages == 1:
            self.downloaded_images = None
            self.large_dir = f'{KONEKODIR}/{self.artist_user_id}/individual/'
        else:
            self.downloaded_images = list(
                map(pure.split_backslash_last, self.page_urls[:2]))
            # So it won't be duplicated later
            self.large_dir = f'{KONEKODIR}/{self.artist_user_id}/individual/{image_id}/'
예제 #11
0
 def urls_as_names(self) -> 'list[str]':
     return [pure.split_backslash_last(url) for url in self.all_urls]
예제 #12
0
 def large_filename(self) -> str:
     return pure.split_backslash_last(self.page_urls[0])
예제 #13
0
 def image_filename(self) -> str:
     return pure.split_backslash_last(self.current_url)
예제 #14
0
 def _process_url_or_input(self):
     if 'pixiv' in self._url_or_id:
         self._user_input = pure.split_backslash_last(self._url_or_id)
     else:
         self._user_input = self._url_or_id
예제 #15
0
 def show_full_res(self) -> 'IO':
     filename = pure.split_backslash_last(self.current_original_url)
     download.download_url(self.download_path, self.current_original_url,
                           filename)
     lscat.api.show_center(self.download_path / filename)
예제 #16
0
def test_split_backslash_last():
    assert (pure.split_backslash_last('https://www.pixiv.net/en/users/2232374')
            == '2232374')
    assert (pure.split_backslash_last(
        'https://www.pixiv.net/en/artworks/78823485') == '78823485')