Add bing downloader logic

This commit is contained in:
Simon V. Lejel 2022-03-11 20:13:32 +01:00
parent e154cf7fe9
commit ac24cad7fc

View file

@ -0,0 +1,25 @@
import xml.etree.ElementTree as ElementTree
import requests
import datetime
from db.database import Session, Image
def get_bing_xml():
root_node = ElementTree.parse(
requests.get('https://www.bing.com/HPImageArchive.aspx', params={'n': 1}).text
).getroot()
image_node = root_node.find('image')
image_url = image_node.find('urlBase').text + '_1920x1080.jpg'
r = requests.get(image_url, stream=True)
image_location = '/data/bing_images/' + datetime.datetime.now().strftime("%d%m%Y") + '.jpg'
with open(image_location, 'wb') as f:
for chunk in r:
f.write(chunk)
image = Image(location=image_location, description=image_node.find('copyright').text)
session = Session()
session.add(image)
session.commit()