Compare commits
No commits in common. "9456806e7c2373bc867c2262f78737b6378e41b2" and "9f4e06d7f9240b437a4eff403a78ee984c0bb9d9" have entirely different histories.
9456806e7c
...
9f4e06d7f9
5 changed files with 35 additions and 68 deletions
1
app.py
1
app.py
|
@ -25,6 +25,5 @@ def all_images():
|
|||
# TODO Implement
|
||||
return NotImplementedError
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0')
|
||||
|
|
17
db/connector.py
Normal file
17
db/connector.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
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
|
||||
)
|
||||
)
|
|
@ -1,41 +0,0 @@
|
|||
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))
|
18
db/model/image_table.py
Normal file
18
db/model/image_table.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
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)
|
|
@ -1,26 +0,0 @@
|
|||
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