Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pixiv Tagging #46

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config_sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ deepbooru_set_tag = true # Tag Deepbooru post with tag "deepbooru"
hide_progress = false # Set this to true to hide the progress bar
use_pixiv_artist = false # If the artist could only be found on pixiv, create and use the pixiv artist. Category has to be 'artist'.
update_relations = true # Set character <> parody relation if SauceNAO is disabled (or limit reached) and Deepbooru enabled
pixiv_tags = false # Tag posts with pixiv tags

# Leave those options as "None" if you have no accounts on these sites
[danbooru]
Expand Down
563 changes: 260 additions & 303 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ gallery-dl = "^1.25.4"
cunnypy = "^2.0.0"
tldextract = "^3.4.4"
flask = "^3.0.0"
pixivpy3 = "^3.7.4"

[tool.poetry.dev-dependencies]
black = "^22.1.0"
Expand Down
1 change: 1 addition & 0 deletions src/szurubooru_toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .config import Config
from .danbooru import Danbooru # noqa F401
from .gelbooru import Gelbooru # noqa F401
from .pixiv import Pixiv
from .szurubooru import Post # noqa F401
from .szurubooru import Szurubooru
from .twitter import Twitter # noqa F401
Expand Down
1 change: 1 addition & 0 deletions src/szurubooru_toolkit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def check_attr_set(self) -> None:
'hide_progress',
'use_pixiv_artist',
'update_relations',
'pixiv_tags',
],
'upload_media': [
'src_path',
Expand Down
48 changes: 48 additions & 0 deletions src/szurubooru_toolkit/pixiv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pixivpy3 import AppPixivAPI as Pixiv_Module
from aiohttp.client_exceptions import ClientConnectorError
from loguru import logger

class Pixiv:
def __init__(self, token):
self.client = Pixiv_Module()
self.client.auth(refresh_token=token)


def get_result(self, result_url):
temp = result_url.split('=')
post_id = int(result_url.split('=')[-1])
logger.debug(f'Getting result from id {post_id}')
for _ in range(1, 12):
try:
result = self.client.illust_detail(post_id)
logger.debug(f'Returning result: {result}')
break
except ClientConnectorError:
logger.debug('Could not establish connection to Pixiv, trying again in 5s...')
sleep(5)
except KeyError: # In case the post got deleted but is still indexed
result = None
logger.debug('Got no result')
break
else:
result = None

return result

def get_tags(self, result):
tags = []
if result.illust and result.illust.tags:
for tag in result.illust.tags:
temp = tag['name']
if not temp == None:
if not temp == "R-18":
tags.append(temp)
logger.debug(f'Returning tags {tags}')
return tags

def get_rating(self, result):
if result.illust and result.illust.tags:
for tag in result.illust.tags:
if tag['name'] == "R-18":
return 'unsafe'
return 'safe'
4 changes: 2 additions & 2 deletions src/szurubooru_toolkit/scripts/auto_tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def main(post_id: str = None, file_to_upload: bytes = None, limit_reached: bool
md5_results = asyncio.run(search_boorus('all', 'md5:' + post.md5, 1, 0))

if md5_results:
tags_by_md5, sources, post.rating = prepare_post(md5_results)
tags_by_md5, sources, post.rating = prepare_post(md5_results, config)
post.source = collect_sources(*sources, *post.source.splitlines())
else:
tags_by_md5 = []
Expand Down Expand Up @@ -257,7 +257,7 @@ def main(post_id: str = None, file_to_upload: bytes = None, limit_reached: bool
sauce_results, limit_reached = get_saucenao_results(sauce, post, image)

if sauce_results:
tags_by_sauce, sources, post.rating = prepare_post(sauce_results)
tags_by_sauce, sources, post.rating = prepare_post(sauce_results, config)
post.source = collect_sources(*sources, *post.source.splitlines())
else:
tags_by_sauce = []
Expand Down
26 changes: 19 additions & 7 deletions src/szurubooru_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from szurubooru_toolkit import Config
from szurubooru_toolkit import Danbooru
from szurubooru_toolkit import Gelbooru
from szurubooru_toolkit import Pixiv


# Keep track of total tagged posts
Expand Down Expand Up @@ -513,25 +514,36 @@ async def search_boorus(booru: str, query: str, limit: int, page: int = 1) -> di
return results


def prepare_post(results: dict):
def prepare_post(results: dict, config: Config):
tags = []
sources = []
rating = []

booru_found = False
for booru, result in results.items():
if booru != 'pixiv':
tags.append(result[0].tags.split())
sources.append(generate_src({'site': booru, 'id': result[0].id}))
rating = convert_rating(result[0].rating)
booru_found = True
else:
if config.auto_tagger['pixiv_tags']:
pixiv = Pixiv(config.pixiv['token'])
pixiv_result = pixiv.get_result(results['pixiv'].url)
pixiv_tags = pixiv.get_tags(pixiv_result)
pixiv_rating = pixiv.get_rating(pixiv_result)
pixiv_sources, pixiv_artist = extract_pixiv_artist(results['pixiv'])
sources.append(pixiv_sources)

final_tags = [item for sublist in tags for item in sublist]

if not final_tags and 'pixiv' in results and pixiv_artist:
final_tags = pixiv_artist

if config.auto_tagger['pixiv_tags']:
if 'pixiv' in results and pixiv_tags:
final_tags.extend(pixiv_tags)
if not booru_found and 'pixiv' in results and pixiv_rating:
rating = pixiv_rating
final_tags.append('check_safety')

if not booru_found and 'pixiv' in results and pixiv_artist:
final_tags.append(pixiv_artist)
return final_tags, sources, rating


Expand Down Expand Up @@ -565,7 +577,7 @@ def extract_pixiv_artist(result: Any) -> tuple[str, list]:
else:
artist = None

return source, [artist]
return source, artist


def extract_twitter_artist(metadata: dict) -> str:
Expand Down