Initial commit
This commit is contained in:
0
providers/__init__.py
Normal file
0
providers/__init__.py
Normal file
BIN
providers/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
providers/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
providers/__pycache__/bing.cpython-311.pyc
Normal file
BIN
providers/__pycache__/bing.cpython-311.pyc
Normal file
Binary file not shown.
BIN
providers/__pycache__/provider.cpython-311.pyc
Normal file
BIN
providers/__pycache__/provider.cpython-311.pyc
Normal file
Binary file not shown.
BIN
providers/__pycache__/unsplash.cpython-311.pyc
Normal file
BIN
providers/__pycache__/unsplash.cpython-311.pyc
Normal file
Binary file not shown.
BIN
providers/__pycache__/wikimedia.cpython-311.pyc
Normal file
BIN
providers/__pycache__/wikimedia.cpython-311.pyc
Normal file
Binary file not shown.
31
providers/bing.py
Normal file
31
providers/bing.py
Normal file
@ -0,0 +1,31 @@
|
||||
import logging
|
||||
import requests
|
||||
from requests import session
|
||||
|
||||
from providers.provider import Provider
|
||||
|
||||
class bing(Provider):
|
||||
name = "Bing"
|
||||
url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"
|
||||
idx = 0
|
||||
number = 2**31-1
|
||||
|
||||
def __init__(self, settings, session):
|
||||
super().__init__(settings, session)
|
||||
self.size = settings.get("size")
|
||||
self.country = settings.get("country")
|
||||
self.market = settings.get("market")
|
||||
|
||||
def get_image_url(self):
|
||||
query = f"https://www.bing.com/HPImageArchive.aspx?format=js&idx={self.idx}&n={self.number}&mkt={self.market}"
|
||||
logging.debug(f"Query: {query}")
|
||||
|
||||
response = self.session.get(query).json()
|
||||
logging.debug(f"Response: {response}")
|
||||
|
||||
image_url = response["images"][0]["urlbase"]
|
||||
logging.debug(f"Image URL: {image_url}")
|
||||
|
||||
image_url = f"https://www.bing.com{image_url}_{self.size}.jpg"
|
||||
return image_url
|
||||
|
14
providers/provider.py
Normal file
14
providers/provider.py
Normal file
@ -0,0 +1,14 @@
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
|
||||
class Provider(ABC):
|
||||
name = None
|
||||
url = None
|
||||
|
||||
def __init__(self, settings, session):
|
||||
self.settings = settings
|
||||
self.session = session
|
||||
|
||||
@abstractmethod
|
||||
def get_image_url(self):
|
||||
pass
|
36
providers/unsplash.py
Normal file
36
providers/unsplash.py
Normal file
@ -0,0 +1,36 @@
|
||||
# Using official Unsplash API to get images
|
||||
# curl https://api.unsplash.com/collections/1459961/photos
|
||||
# But we will scrape >:)
|
||||
|
||||
import logging
|
||||
import requests
|
||||
import re
|
||||
from providers.provider import Provider
|
||||
|
||||
class unsplash(Provider):
|
||||
name = "Unsplash"
|
||||
url = "https://unsplash.com/collections/1459961/photo-of-the-day-(archive)"
|
||||
|
||||
def __init__(self, settings, session):
|
||||
super().__init__(settings, session)
|
||||
|
||||
def get_image_url(self):
|
||||
query = "https://unsplash.com/collections/1459961/photo-of-the-day-(archive)"
|
||||
logging.debug(f"Query: {query}")
|
||||
|
||||
response = self.session.get(query).text
|
||||
# logging.debug(f"Response: {response}")
|
||||
|
||||
regex = r"href=\"\/photos\/(.*?)\""
|
||||
matches = re.search(regex, response)
|
||||
logging.debug(f"Matches: {matches}")
|
||||
image_slug = matches.group(1)
|
||||
logging.debug(f"Image slug: {image_slug}")
|
||||
|
||||
image_id = image_slug.split("-")[-1]
|
||||
logging.debug(f"Image ID: {image_id}")
|
||||
|
||||
image_url = f"https://unsplash.com/photos/{image_id}/download"
|
||||
logging.debug(f"Image URL: {image_url}")
|
||||
return image_url
|
||||
|
32
providers/wikimedia.py
Normal file
32
providers/wikimedia.py
Normal file
@ -0,0 +1,32 @@
|
||||
import datetime
|
||||
import logging
|
||||
import requests
|
||||
import re
|
||||
from providers.provider import Provider
|
||||
|
||||
# https://api.wikimedia.org/wiki/Feed_API/Reference/Featured_content
|
||||
|
||||
class wikimedia(Provider):
|
||||
name = "Wikimedia"
|
||||
url = "https://api.wikimedia.org/feed/v1/wikipedia/en/featured/"
|
||||
|
||||
|
||||
def __init__(self, settings, session):
|
||||
super().__init__(settings, session)
|
||||
|
||||
def get_image_url(self):
|
||||
today = datetime.datetime.now()
|
||||
date = today.strftime('%Y/%m/%d')
|
||||
logging.debug(f"Date: {date}")
|
||||
url = 'https://api.wikimedia.org/feed/v1/wikipedia/en/featured/' + date
|
||||
logging.debug(f"URL: {url}")
|
||||
|
||||
response = self.session.get(url).json()
|
||||
logging.debug(f"Response: {response}")
|
||||
|
||||
image = response['image']['image']['source']
|
||||
logging.debug(f"Image: {image}")
|
||||
image_url = image
|
||||
|
||||
return image_url
|
||||
|
Reference in New Issue
Block a user