From 7ec10f4d6dd31b82c37d528c77653f241668e413 Mon Sep 17 00:00:00 2001 From: Wardyn Date: Sat, 28 Jan 2023 01:10:07 -0800 Subject: [PATCH] add lolibot --- .gitignore | 163 +------------------------------------------ config_template.json | 6 ++ generate_config.py | 34 +++++++++ lolibot.py | 29 ++++++++ 4 files changed, 70 insertions(+), 162 deletions(-) create mode 100644 config_template.json create mode 100644 generate_config.py create mode 100644 lolibot.py diff --git a/.gitignore b/.gitignore index 5d381cc..0cffcb3 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/config_template.json b/config_template.json new file mode 100644 index 0000000..71552b7 --- /dev/null +++ b/config_template.json @@ -0,0 +1,6 @@ +{ + "instance":"", + "client_id":"", + "client_secret":"", + "user_token":"" +} diff --git a/generate_config.py b/generate_config.py new file mode 100644 index 0000000..208ad3b --- /dev/null +++ b/generate_config.py @@ -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") \ No newline at end of file diff --git a/lolibot.py b/lolibot.py new file mode 100644 index 0000000..748e74f --- /dev/null +++ b/lolibot.py @@ -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()) \ No newline at end of file