Compare commits
3 commits
9f4e06d7f9
...
9456806e7c
Author | SHA1 | Date | |
---|---|---|---|
9456806e7c | |||
ac24cad7fc | |||
e154cf7fe9 |
5 changed files with 68 additions and 35 deletions
1
app.py
1
app.py
|
@ -25,5 +25,6 @@ def all_images():
|
|||
# TODO Implement
|
||||
return NotImplementedError
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0')
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
import os
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
user = os.environ['DB_USER']
|
||||
password = os.environ['DB_PASSWORD']
|
||||
host = os.environ['DB_HOST']
|
||||
port = os.environ['DB_PORT']
|
||||
database = os.environ['DB_DATABASE']
|
||||
|
||||
|
||||
def get_connection():
|
||||
return create_engine(
|
||||
url="mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format(
|
||||
user, password, host, port, database
|
||||
)
|
||||
)
|
41
db/database.py
Normal file
41
db/database.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import create_engine, Column, Integer, String, DateTime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
user = os.environ['DB_USER']
|
||||
password = os.environ['DB_PASSWORD']
|
||||
host = os.environ['DB_HOST']
|
||||
port = os.environ['DB_PORT']
|
||||
database = os.environ['DB_DATABASE']
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class Image(Base):
|
||||
__tablename__ = "images"
|
||||
|
||||
id = Column('id', Integer(), primary_key=True, autoincrement=True)
|
||||
location = Column('location', String(25), nullable=False)
|
||||
description = Column('copyright', String(), nullable=False)
|
||||
timestamp = Column('date_added', DateTime, default=datetime.now())
|
||||
|
||||
def __init__(self, location, description):
|
||||
self.location = location
|
||||
self.description = description
|
||||
|
||||
|
||||
def get_connection():
|
||||
return create_engine(
|
||||
url="mysql+pymysql://{0}:{1}@{2}:{3}/{4}".format(
|
||||
user, password, host, port, database
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
engine = create_engine
|
||||
Base.metadata.create_all(engine)
|
||||
Session = scoped_session(sessionmaker(bind=engine,
|
||||
autocommit=False, autoflush=False))
|
|
@ -1,18 +0,0 @@
|
|||
import sqlalchemy as db
|
||||
|
||||
from db import connector
|
||||
|
||||
engine = connector.create_engine
|
||||
|
||||
metadata_obj = db.MetaData()
|
||||
|
||||
profile = db.Table(
|
||||
'images',
|
||||
metadata_obj,
|
||||
db.Column('id', db.Integer, primary_key=True),
|
||||
db.Column('location', db.String),
|
||||
db.Column('copyright', db.String),
|
||||
db.Column('date_added', db.TIMESTAMP),
|
||||
)
|
||||
|
||||
metadata_obj.create_all(engine)
|
26
service/bing_downloader.py
Normal file
26
service/bing_downloader.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
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()
|
Loading…
Reference in a new issue