Last active
November 23, 2023 19:24
-
-
Save harrynull/0194a5c1119a9c1a3020f3a551559262 to your computer and use it in GitHub Desktop.
Random Anime Wallpaper API using Reddit /r/Animewallpaper/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MIT License | |
# | |
# Copyright (c) 2019 Null | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# Note that if you use this with `download=true` parameter, you might want to | |
# clean the /static folder periodically. | |
import requests | |
import xml.etree.ElementTree | |
import re | |
import random | |
import time | |
import hashlib | |
import os.path | |
import copy | |
from flask import Flask | |
from flask import request | |
from flask import jsonify | |
app = Flask(__name__) | |
URL_BASE_PATH = "http://127.0.0.1:5000/" | |
# See https://github.com/reddit-archive/reddit/wiki/API for UserAgent format | |
USER_AGENT = "python-backend:tech.harrynull.os_random_anime_wallpaper:1.0.0 (by /u/Harry_Null)" | |
CACHE_VALID_TIME = 60 * 10 # 10mins | |
RE_EXTRACT_IMG = re.compile(r'<span><a href="(.*?)">\[link\]</a>') | |
FEED_URL = "https://www.reddit.com/r/Animewallpaper/search.rss?q=flair_name%3A%22Desktop%22&restrict_sr=1" | |
def entry_to_dictionary(entry): | |
return { | |
"source": entry.find("{http://www.w3.org/2005/Atom}link").attrib["href"], | |
"title":entry.find("{http://www.w3.org/2005/Atom}title").text, | |
"img": RE_EXTRACT_IMG.search(entry.find("{http://www.w3.org/2005/Atom}content").text).groups(0)[0] | |
} | |
last_fetch_time = 0 | |
cached_results = None | |
def fetch_feed(): | |
global last_fetch_time | |
global cached_results | |
if time.time() - last_fetch_time <= CACHE_VALID_TIME and cached_results: | |
return cached_results | |
results = [] | |
e = xml.etree.ElementTree.fromstring(requests.get(FEED_URL, headers={"User-Agent":USER_AGENT}).text.strip()) | |
for entry in e.findall("{http://www.w3.org/2005/Atom}entry"): | |
try: | |
results.append(entry_to_dictionary(entry)) | |
except: | |
pass | |
cached_results = results | |
last_fetch_time = time.time() | |
return results | |
def download_img(src): | |
ext = src.split(".")[-1] | |
name = "static/" + hashlib.sha1(src.encode()).hexdigest() + "." + ext | |
if os.path.isfile(name): | |
return name | |
print("Downloading ",src, " to ",name) | |
with open(name, 'wb') as f: | |
f.write(requests.get(src).content) | |
return name | |
@app.route("/random_anime_wallpaper") | |
def random_anime_wallpaper(): | |
res = fetch_feed() | |
selected = copy.deepcopy(res[random.randint(0, len(res))]) | |
if request.args.get('download', False): | |
selected["img_src"] = selected["img"] | |
selected["img"] = URL_BASE_PATH + download_img(selected["img"]) | |
return jsonify(selected) | |
@app.route("/anime_wallpapers") | |
def all_anime_wallpapers(): | |
return jsonify(fetch_feed()) |
@Infinibyte-Github Please refer to https://github.com/harrynull/random_anime_wallpaper
A demo can be found at https://harrynull.tech
Thanks man!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I use it??