2020-06-25 23:41:15 +00:00
|
|
|
import json
|
|
|
|
|
2020-06-26 00:21:03 +00:00
|
|
|
from sqlalchemy import create_engine, Column, String, ForeignKey, Integer
|
2020-06-25 23:41:15 +00:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
2020-06-26 21:28:39 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2020-11-09 02:45:10 +00:00
|
|
|
from sqlalchemy.types import JSON
|
2020-06-25 23:41:15 +00:00
|
|
|
|
|
|
|
engine = create_engine('sqlite:///data/qotnews.sqlite')
|
|
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
class Story(Base):
|
|
|
|
__tablename__ = 'stories'
|
|
|
|
|
2020-06-26 00:21:03 +00:00
|
|
|
sid = Column(String(16), primary_key=True)
|
2020-06-27 22:53:39 +00:00
|
|
|
ref = Column(String(16), unique=True)
|
2020-11-09 02:45:10 +00:00
|
|
|
meta = Column(JSON)
|
|
|
|
data = Column(JSON)
|
2020-06-26 21:28:39 +00:00
|
|
|
title = Column(String)
|
2020-06-25 23:41:15 +00:00
|
|
|
|
2020-06-26 00:21:03 +00:00
|
|
|
class Reflist(Base):
|
|
|
|
__tablename__ = 'reflist'
|
|
|
|
|
|
|
|
rid = Column(Integer, primary_key=True)
|
|
|
|
ref = Column(String(16), unique=True)
|
|
|
|
sid = Column(String, ForeignKey('stories.sid'), unique=True)
|
2020-06-27 22:53:39 +00:00
|
|
|
source = Column(String(16))
|
2020-06-26 00:21:03 +00:00
|
|
|
|
2020-06-25 23:41:15 +00:00
|
|
|
def init():
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
def get_story(sid):
|
2020-06-25 23:41:15 +00:00
|
|
|
session = Session()
|
2020-06-26 21:28:39 +00:00
|
|
|
return session.query(Story).get(sid)
|
2020-06-25 23:41:15 +00:00
|
|
|
|
2020-06-26 00:21:03 +00:00
|
|
|
def put_story(story):
|
2020-06-27 22:53:39 +00:00
|
|
|
story = story.copy()
|
2020-11-09 02:45:10 +00:00
|
|
|
data = {}
|
|
|
|
data.update(story)
|
2020-06-25 23:41:15 +00:00
|
|
|
|
2020-11-09 02:45:10 +00:00
|
|
|
meta = {}
|
|
|
|
meta.update(story)
|
|
|
|
meta.pop('text', None)
|
|
|
|
meta.pop('comments', None)
|
2020-06-25 23:41:15 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
session = Session()
|
2020-06-26 21:28:39 +00:00
|
|
|
s = Story(
|
|
|
|
sid=story['id'],
|
2020-06-27 22:53:39 +00:00
|
|
|
ref=story['ref'],
|
2020-11-09 02:45:10 +00:00
|
|
|
data=data,
|
|
|
|
meta=meta,
|
2020-06-26 21:28:39 +00:00
|
|
|
title=story.get('title', None),
|
|
|
|
)
|
2020-06-25 23:41:15 +00:00
|
|
|
session.merge(s)
|
|
|
|
session.commit()
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
session.close()
|
2020-06-26 00:21:03 +00:00
|
|
|
|
2020-06-27 22:53:39 +00:00
|
|
|
def get_story_by_ref(ref):
|
2020-06-26 21:28:39 +00:00
|
|
|
session = Session()
|
2020-06-27 22:53:39 +00:00
|
|
|
return session.query(Story).filter(Story.ref==ref).first()
|
2020-06-26 21:28:39 +00:00
|
|
|
|
2020-06-26 00:21:03 +00:00
|
|
|
def get_reflist(amount):
|
|
|
|
session = Session()
|
|
|
|
q = session.query(Reflist).order_by(Reflist.rid.desc()).limit(amount)
|
2020-06-27 22:53:39 +00:00
|
|
|
return [dict(ref=x.ref, sid=x.sid, source=x.source) for x in q.all()]
|
2020-06-26 00:21:03 +00:00
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
def get_stories(amount):
|
|
|
|
session = Session()
|
2020-11-09 02:45:10 +00:00
|
|
|
q = session.query(Reflist, Story.meta).\
|
2020-06-26 21:28:39 +00:00
|
|
|
join(Story).\
|
|
|
|
filter(Story.title != None).\
|
2020-11-09 02:45:10 +00:00
|
|
|
order_by(Story.meta['date'].desc()).\
|
2020-06-26 21:28:39 +00:00
|
|
|
limit(amount)
|
|
|
|
return [x[1] for x in q]
|
|
|
|
|
2020-06-27 22:53:39 +00:00
|
|
|
def put_ref(ref, sid, source):
|
2020-06-26 00:21:03 +00:00
|
|
|
try:
|
|
|
|
session = Session()
|
2020-06-27 22:53:39 +00:00
|
|
|
r = Reflist(ref=ref, sid=sid, source=source)
|
2020-06-26 00:21:03 +00:00
|
|
|
session.add(r)
|
|
|
|
session.commit()
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
def del_ref(ref):
|
|
|
|
try:
|
|
|
|
session = Session()
|
|
|
|
session.query(Reflist).filter(Reflist.ref==ref).delete()
|
|
|
|
session.commit()
|
|
|
|
except:
|
|
|
|
session.rollback()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
session.close()
|
|
|
|
|
2020-06-26 00:21:03 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
init()
|
2020-06-26 21:28:39 +00:00
|
|
|
|
2020-06-27 22:53:39 +00:00
|
|
|
print(get_story_by_ref('hgi3sy'))
|