add lolibot

This commit is contained in:
Wardyn 2023-01-28 01:10:07 -08:00
parent 8e881e48f1
commit 7ec10f4d6d
4 changed files with 70 additions and 162 deletions

163
.gitignore vendored
View File

@ -1,162 +1 @@
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
config.json

6
config_template.json Normal file
View File

@ -0,0 +1,6 @@
{
"instance":"",
"client_id":"",
"client_secret":"",
"user_token":""
}

34
generate_config.py Normal file
View File

@ -0,0 +1,34 @@
import os
import requests
import json
from urllib.parse import urlencode
def generate_config(app_name, scopes):
#Ensure Credentials
parent = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(parent, 'config_template.json'), 'r') as template:
config = json.load(template)
#Create app
instance = input('Please enter your instance: ')
if not instance[:4] == 'http':
instance = 'https://' + instance
response = requests.post(instance + '/api/v1/apps', data={'client_name':app_name,'scopes':scopes, 'redirect_uris':'urn:ietf:wg:oauth:2.0:oob'}).json()
client_id = config['client_id'] = response['client_id']
client_secret = config['client_secret'] = response['client_secret']
config['instance'] = instance
#Log in to user account
print(instance + '/oauth/authorize?', urlencode({'response_type':'code', 'client_id':client_id, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':scopes}))
code = input("To generate a token to access your account, " + app_name + " needs an authorization code. Please authorize using the link above and enter the code it provides you \nCode: ")
response = requests.post(instance + '/oauth/token', data={'grant_type':'authorization_code', 'code':code, 'client_id':client_id, 'client_secret':client_secret, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':scopes})
config['user_token'] = response.json()['access_token']
with open(os.path.join(parent, 'config.json'), 'w') as config_file:
config_file.write(json.dumps(config))
if __name__ == "__main__":
generate_config("test_app", "read")

29
lolibot.py Normal file
View File

@ -0,0 +1,29 @@
import os
from generate_config import generate_config
import json
import requests
import tempfile
parent = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(os.path.join(parent, 'config.json')):
generate_config("lolibot", "write:statuses write:media")
with open(os.path.join(parent, 'config.json'), 'r') as config_file:
config = json.load(config_file)
session = requests.Session()
session.headers.update({"Authorization" : "Bearer " + config['user_token']})
post = requests.get('https://gelbooru.com/index.php', params={'page':'dapi','s':'post', 'q':'index', 'limit':1, 'tags':'loli sort:random score:>=10', 'json':1}).json()['post'][0]
print(post['id'])
with tempfile.TemporaryFile('w+b', suffix='.' + post['file_url'].split('.')[-1]) as image:
#with open('image.' + post['file_url'].split('.')[-1], 'w+b') as image:
image.write(requests.get(post['file_url']).content)
image.seek(0)
attachment = session.post(config['instance'] + '/api/v2/media', files={'file':image}, data={'description': 'gelbooru ID: ' + str(post['id'])}).json()
print(attachment)
content = 'source: https://gelbooru.com/index.php?page=post&s=view&id=' + str(post['id'])
if not 'source_request' in post['tags'].split(' '):
content = 'source: ' + post['source']
sensitive = True
print(session.post(config['instance'] + '/api/v1/statuses', json={'status':content, 'media_ids':[attachment['id']], 'visibility':'direct', 'sensitive': True}).json())