def export_image_table(caller, filename, pixiv, fanbox, sketch): export_list = list() table = list() try: if pixiv == 'o': table.append("Pixiv") elif fanbox == 'o': table.append("Fanbox") elif sketch == 'o': table.append("Sketch") else: if pixiv == 'y': table.append("Pixiv") if fanbox == 'y': table.append("Fanbox") if sketch == 'y': table.append("Sketch") for t in table: export_list = caller.__dbManager__.exportImageTable(t) PixivBookmark.export_image_list(export_list, f"{filename}-{t}") except KeyboardInterrupt: raise except BaseException: PixivHelper.print_and_log( 'error', 'Error at export_image_table(): {0}'.format(sys.exc_info())) raise
def export_bookmark(caller, config, filename, hide='n', start_page=1, end_page=0, member_id=None): try: total_list = list() if hide != 'o': print("Importing Bookmarks...") total_list.extend( get_bookmarks(caller, config, False, start_page, end_page, member_id)) if hide != 'n': print("Importing Private Bookmarks...") total_list.extend( get_bookmarks(caller, config, True, start_page, end_page, member_id)) print("Result: ", str(len(total_list)), "items.") PixivBookmark.exportList(total_list, filename) except KeyboardInterrupt: raise except BaseException: PixivHelper.print_and_log( 'error', 'Error at export_bookmark(): {0}'.format(sys.exc_info())) raise
def export_image_bookmark(caller, config, hide='n', start_page=1, end_page=0, tag=None, use_image_tag=False, filename='exported_images.txt'): try: print("Getting image bookmarks...") total_list = list() private_list = list() public_list = list() total_bookmark_count = 0 if hide == 'n': (public_list, total_bookmark_count) = get_image_bookmark( caller, config, False, start_page, end_page, tag, use_image_tag) elif hide == 'y': # public and private image bookmarks (public_list, total_bookmark_count_pub) = get_image_bookmark( caller, config, False, start_page, end_page, tag, use_image_tag) (private_list, total_bookmark_count_priv) = get_image_bookmark( caller, config, True, start_page, end_page, tag, use_image_tag) total_bookmark_count = total_bookmark_count_pub + total_bookmark_count_priv else: (private_list, total_bookmark_count) = get_image_bookmark( caller, config, True, start_page, end_page, tag, use_image_tag) total_list.extend(private_list) total_list.extend(public_list) PixivBookmark.export_image_list(total_list, filename) PixivHelper.print_and_log( 'info', f"Found {len(total_list)} of {total_bookmark_count} possible image(s) ." ) print("Done.\n") except KeyboardInterrupt: raise except BaseException: PixivHelper.print_and_log( 'error', 'Error at export_image_bookmark(): {0}'.format(sys.exc_info())) raise
def get_bookmarks(caller, config, hide, start_page=1, end_page=0, member_id=None): br: PixivBrowser = caller.__br__ """Get User's bookmarked artists """ total_list = list() i = start_page limit = 48 offset = 0 is_json = False locale = "&lang=en" if br._locale is not None and len(br._locale) > 0: locale = f"&lang={br._locale}" while True: if end_page != 0 and i > end_page: print('Limit reached') break PixivHelper.print_and_log('info', f'Exporting page {i}') if member_id: is_json = True offset = limit * (i - 1) url = f'https://www.pixiv.net/ajax/user/{member_id}/following?offset={offset}&limit={limit}' else: # Issue #942 member_id = br._myId is_json = True url = f'https://www.pixiv.net/ajax/user/{member_id}/following?offset={offset}&limit={limit}' if hide: url = url + "&rest=hide" else: url = url + "&rest=show" url = url + locale PixivHelper.print_and_log('info', f"Source URL: {url}") page = br.open_with_retry(url) page_str = page.read().decode('utf8') page.close() bookmarks = PixivBookmark.parseBookmark( page_str, root_directory=config.rootDirectory, db_path=config.dbPath, locale=br._locale, is_json=is_json) if len(bookmarks) == 0: print('No more data') break total_list.extend(bookmarks) i = i + 1 print(str(len(bookmarks)), 'items') PixivHelper.wait(config=config) return total_list
def get_image_bookmark(caller, config, hide, start_page=1, end_page=0, tag=None, sorting=None): """Get user's image bookmark""" br = caller.__br__ total_list = list() i = start_page offset = 0 limit = 48 member_id = br._myId while True: if end_page != 0 and i > end_page: print("Page Limit reached: " + str(end_page)) break # https://www.pixiv.net/ajax/user/189816/illusts/bookmarks?tag=&offset=0&limit=48&rest=show show = "show" if hide: show = "hide" # # Implement #468 default is desc, only for your own bookmark. # not available in current api # if sorting in ('asc', 'date_d', 'date'): # url = url + "&order=" + sorting if tag is not None and len(tag) > 0: tag = PixivHelper.encode_tags(tag) offset = limit * (i - 1) url = f"https://www.pixiv.net/ajax/user/{member_id}/illusts/bookmarks?tag={tag}&offset={offset}&limit={limit}&rest={show}" PixivHelper.print_and_log( 'info', f"Importing user's bookmarked image from page {i}") PixivHelper.print_and_log('info', f"Source URL: {url}") page = br.open(url) page_str = page.read().decode('utf8') page.close() bookmarks = PixivBookmark.parseImageBookmark(page_str) total_list.extend(bookmarks) if len(bookmarks) == 0: print("No more images.") break else: print(" found " + str(len(bookmarks)) + " images.") i = i + 1 # Issue#569 PixivHelper.wait(config=config) return total_list
def testPixivImageBookmark(self): # print('\nTesting PixivImageBookmark') p = open('./test/test-image-bookmark.htm', 'r', encoding="utf-8") page = BeautifulSoup(p.read(), features="html5lib") result = PixivBookmark.parseImageBookmark(page) self.assertEqual(len(result), 20) self.assertTrue(35303260 in result) self.assertTrue(28629066 in result) self.assertTrue(27249307 in result) self.assertTrue(30119925 in result)
def testPixivImageBookmark(self): # print('\nTesting PixivImageBookmark') p = open('./test/bookmarks.json', 'r', encoding="utf-8") page = p.read() result = PixivBookmark.parseImageBookmark(page) self.assertEqual(len(result), 18) self.assertTrue(35303260 in result) self.assertTrue(28629066 in result) self.assertTrue(27249307 in result) self.assertTrue(30119925 in result)
def get_image_bookmark(caller, config, hide, start_page=1, end_page=0, tag=None, use_image_tag=False): """Get user's image bookmark""" br = caller.__br__ total_list = list() i = start_page offset = 0 limit = 48 member_id = br._myId total_bookmark_count = 0 encoded_tag = '' while True: if end_page != 0 and i > end_page: print("Page Limit reached: " + str(end_page)) break # https://www.pixiv.net/ajax/user/189816/illusts/bookmarks?tag=&offset=0&limit=48&rest=show show = "show" if hide: show = "hide" if tag is not None and len(tag) > 0: encoded_tag = PixivHelper.encode_tags(tag) offset = limit * (i - 1) PixivHelper.print_and_log( 'info', f"Importing user's bookmarked image from page {i}") url = f"https://www.pixiv.net/ajax/user/{member_id}/illusts/bookmarks?tag={encoded_tag}&offset={offset}&limit={limit}&rest={show}" if use_image_tag: # don't filter based on user's bookmark tag url = f"https://www.pixiv.net/ajax/user/{member_id}/illusts/bookmarks?tag=&offset={offset}&limit={limit}&rest={show}" PixivHelper.print_and_log('info', f"Using image tag: {tag}") PixivHelper.print_and_log('info', f"Source URL: {url}") page = br.open(url) page_str = page.read().decode('utf8') page.close() if use_image_tag: (bookmarks, total_bookmark_count) = PixivBookmark.parseImageBookmark( page_str, image_tags_filter=tag) else: (bookmarks, total_bookmark_count) = PixivBookmark.parseImageBookmark(page_str) total_list.extend(bookmarks) if len(bookmarks) == 0 and not use_image_tag: print("No more images.") break elif use_image_tag and total_bookmark_count / limit < i: print("Last page reached.") break else: print(f" found {len(bookmarks)} images.") i = i + 1 # Issue#569 PixivHelper.wait(config=config) return (total_list, total_bookmark_count)