Add bing downloader logic
This commit is contained in:
parent
e154cf7fe9
commit
ac24cad7fc
1 changed files with 25 additions and 0 deletions
25
service/bing_downloader.py
Normal file
25
service/bing_downloader.py
Normal 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()
|
Loading…
Reference in a new issue