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, 1640, 2600, 84, 156, 162, 897, 214, 391, 98, 2047, 1341, 83] def main(): #Logging into VNDB vndb = v.VNDB('VNDBbot', '0.1', 'LOGIN', 'PASSWORD') id = -1 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 < 2: 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'] != 0) or (vn_details['image_flagging']['violence_avg'] != 0): continue # 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 '' raw_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) # processing description and removing markdown description = re.sub('^\[.rom.*\[\/url\]\].*', '', raw_description) # logging in and posting mastodon = Mastodon( access_token = 'token.dat', api_base_url = 'https://udongein.xyz/' ) text = title + '\n- - - - - - - -\n\n' + description + '\n\nReleased: ' + released + '\nPopularity: ' + (str(popularity) if popularity > -1 else 'unknown') + '\nRating:' + str(rating) + '\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 = [ ] 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')) mastodon.status_post(text, media_ids=screens, visibility='unlisted', sensitive=False) break if __name__ == '__main__': sys.exit(main())