commit bf6684cc922419aa75ac6cf9c9ce146fc04a4424 Author: Julimiro Date: Wed Jul 10 08:34:43 2024 -0400 Upload files to "/" diff --git a/get_apps.py b/get_apps.py new file mode 100644 index 0000000..0f1215d --- /dev/null +++ b/get_apps.py @@ -0,0 +1,31 @@ +import os +import requests +from dotenv import load_dotenv, dotenv_values + +load_dotenv() + + +baseurl = '' # replace with your actual URL +path = "/app/create" +url = os.getenv("baseurl")+path + +data = { + "name": "test", + "description": "test", + "permission": + ["read:drive","write:drive","write:messaging","write:notes"], +"callbackUrl": "http://example.com" +} + +# Define the headers, including the bearer token for authorization +headers = { + 'Authorization': f'Bearer {os.getenv("api_key")}', # replace YOUR_ACCESS_TOKEN with your actual token + 'Content-Type': 'application/json' +} + +# Send the POST request +response = requests.post(url, json=data, headers=headers) + +# Print the response (optional) +print(response.status_code) +print(response.json()) \ No newline at end of file diff --git a/image_source.py b/image_source.py new file mode 100644 index 0000000..258d653 --- /dev/null +++ b/image_source.py @@ -0,0 +1,84 @@ +import os +import random +import requests +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +# Danbooru API base URL +base_url = 'https://danbooru.donmai.us/posts.json' + +# Parameters from .env file +search_query = os.getenv('searchquery') +banned_tags = os.getenv('bannedtags').split(',') +danbooru_user = os.getenv('danbooru_user') +danbooru_apikey = os.getenv('danbooru_apikey') + +# Function to fetch a random post +def fetch_random_post(): + try: + tags = search_query.split() + + + tags = tags[:2] + + params = { + 'tags': ' '.join(tags), + 'random': 'true', + 'login': danbooru_user, + 'api_key': danbooru_apikey + } + + response = requests.get(base_url, params=params) + response.raise_for_status() + + posts = response.json() + + filtered_posts = [post for post in posts if not any(tag in post['tag_string'] for tag in banned_tags)] + + if filtered_posts: + random_post = random.choice(filtered_posts) + return random_post + else: + print("No posts found matching the criteria.") + return None + + except requests.exceptions.RequestException as e: + print(f"Error fetching data: {e}") + return None + +def get_image_url(post): + if 'large_file_url' in post: + return post['large_file_url'] + else: + print("Image URL not found in the post data.") + return None +def get_artist_names(post): + return post['tag_string_artist'].split(',') if 'tag_string_artist' in post else [] + +# Main function +def main(): + random_post = fetch_random_post() + if random_post: + print(f"URL: https://danbooru.donmai.us/posts/{random_post['id']}") + image_url = get_image_url(random_post) + # if image_url: + # r = requests.get(image_url) + # with open("out.jpg","wb") as f: + # f.write(r.content) + # print(f"Image URL: {image_url}") + # else: + # print("Failed to fetch a random post.") + + + artist_names = get_artist_names(random_post) + if artist_names: + artists = f"{', '.join(artist_names)}" + else: + artists = "Unknown" + + return f"https://danbooru.donmai.us/posts/{random_post['id']}", image_url, artists + +if __name__ == "__main__": + main() diff --git a/main.py b/main.py new file mode 100644 index 0000000..cce74dd --- /dev/null +++ b/main.py @@ -0,0 +1,90 @@ +import os +import json +import requests +import image_source ## image source needs to have a `main` function that returns +# a string which is a link to the source of the image. this function also needs to +# refresh the `out.jpg` to the new image. +from dotenv import load_dotenv, dotenv_values + +load_dotenv() +baseurl = os.getenv("baseurl") + +def post(text,id): + path = "/notes/create" + url = baseurl+path + + data = { + # "visibility":"specified", + "text": text, + "mediaIds":[id], + "appSecret": os.getenv("secret") + } + + + headers = { + 'Authorization': f'Bearer {os.getenv("api_key")}', + 'Content-Type': 'application/json' + } + + + response = requests.post(url, json=data, headers=headers) + return response + + + + +def upload_url(fileurl): + path = "/drive/files/upload-from-url" + url = baseurl+path + + data = { + "url": fileurl, + "appSecret": os.getenv("secret") + } + + + headers = { + 'Authorization': f'Bearer {os.getenv("api_key")}', + 'Content-Type': 'application/json' + } + + + response = requests.post(url, json=data, headers=headers) + return response + +def get_id(filename): + path = "/drive/files/find" + url = baseurl+path + + data = { + "name":filename, + "appSecret": os.getenv("secret") + } + + + headers = { + 'Authorization': f'Bearer {os.getenv("api_key")}', + 'Content-Type': 'application/json' + } + + + response = requests.post(url, json=data, headers=headers) + + json_data = str(response.text) + try: + return json.loads(json_data)[0]["id"] + except IndexError: + return get_id(filename) + +def main(): + posturl, url, artists = image_source.main() + filename = url.split("/")[len(url.split("/"))-1] + upload_url(url) + post(f"Artist(s): {artists}\nSource: {posturl}",get_id(filename)) + + + + +if __name__ == "__main__": + main() +