1
0
Fork 0
fediverse-bots/vndb-bot/post.py

99 lines
3.8 KiB
Python
Executable File

#!/home/naiji/mastodon/vndb-bot/venv/bin/python
import re
import sys
import random
import requests
import vndb as v # author HarHar (https://github.com/HarHar)
from bs4 import BeautifulSoup
from mastodon import Mastodon
URL_HEAD = 'https://vndb.org/v/rand'
FORBIDDEN_TAGS = [2023, 156, 162, 897, 391, 98, 2047, 1341, 83]
def main():
#Logging into VNDB
vndb = v.VNDB('VNDBbot', '0.1', 'LOGIN', 'PASSWORD')
id = -1
safe = True
while True: # Searching for a good vn
# Taking a random visual novel
resp = requests.get(URL_HEAD)
soup = BeautifulSoup(resp.text, 'lxml')
# Extracting its ID
id = int(soup.find('base')['href'].split('v')[2])
# getting tags of a VN by given random ID
vndb_result = vndb.get('vn', 'tags', '(id=' + str(id) + ')', '') # getting all the VNs on VNDB
vn_tags = vndb_result['items'][0]['tags']
good_vn = True #supposing
for tag in vn_tags:
for forbidden_tag in FORBIDDEN_TAGS:
if int(tag[0]) == forbidden_tag:
good_vn = False # it contains a bad tag
if not good_vn:
continue
# getting stats of the VN
vndb_result = vndb.get('vn', 'stats', '(id=' + str(id) + ')', '') # getting all the VNs on VNDB
vn_stats = vndb_result['items'][0]
popularity = vn_stats['popularity'] if vn_stats['popularity'] else -1
rating = vn_stats['rating'] if vn_stats['rating'] else -1
votecount = vn_stats['votecount'] if vn_stats['votecount'] else -1
if votecount < 10 or rating < 5 or popularity < 0.8:
continue
# getting details of the VN
vndb_result = vndb.get('vn', 'details', '(id=' + str(id) + ')', '') # getting all the VNs on VNDB
vn_details = vndb_result['items'][0]
# even slightly suggestive or slightly violent go to Sensitive, so we skip it
if (vn_details['image_flagging']['sexual_avg'] > 1) or (vn_details['image_flagging']['violence_avg'] > 1):
safe = False
# getting basic information of the VN
vndb_result = vndb.get('vn', 'basic', '(id=' + str(id) + ')', '')
vn_basic = vndb_result['items'][0]
title = vn_basic['title'] if vn_basic['title'] else ''
description = vn_details['description'] if vn_details['description'] else ''
released = vn_basic['released'] if vn_basic['released'] else 'unknown'
link = 'https://vndb.org/v' + str(id)
languages = ''
for language in vn_basic['languages']:
languages += language + ' '
# logging in and posting
mastodon = Mastodon(
access_token = 'token.dat',
api_base_url = 'https://your.site/',
feature_set = 'pleroma'
)
text = title + '\n- - - - - - - -\n\n' + description + '\n\nReleased: ' + released + '\nPopularity: ' + (str(popularity) if popularity > -1 else 'unknown') + '\nRating:' + str(rating) + '\nLanguages: ' + languages + '\n\n' + link
# getting screenshots of the VN
vndb_result = vndb.get('vn', 'screens', '(id=' + str(id) + ')', '')
vn_screens = vndb_result['items'][0]['screens']
screens = [ ]
counter = 0
screens.append(mastodon.media_post(requests.get(vn_details['image']).content, 'image/jpeg'))
for screen in vn_screens:
if screen['flagging']['sexual_avg'] == 0 and screen['flagging']['violence_avg'] == 0:
screens.append(mastodon.media_post(requests.get(screen['image']).content, 'image/jpeg'))
counter += 1
if counter == 3:
break
mastodon.status_post(text, media_ids=screens, visibility='unlisted', sensitive=not safe, content_type='text/bbcode')
break
if __name__ == '__main__':
sys.exit(main())