image-host-server/service/bing_downloader.py
2022-03-11 20:15:34 +01:00

26 lines
780 B
Python

import datetime
import xml.etree.ElementTree as ElementTree
import requests
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()