From ac24cad7fc68270c7a3ba175a8135a3c45e3c5b6 Mon Sep 17 00:00:00 2001 From: "Simon V. Lejel" Date: Fri, 11 Mar 2022 20:13:32 +0100 Subject: [PATCH] Add bing downloader logic --- service/bing_downloader.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 service/bing_downloader.py diff --git a/service/bing_downloader.py b/service/bing_downloader.py new file mode 100644 index 0000000..50ad993 --- /dev/null +++ b/service/bing_downloader.py @@ -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()