Пример #1
0
 def __init__(self, dbPath='/work.sqlite3.db'):
   pathPrefix = os.path.dirname(os.path.realpath( __file__ ))
   self.db = DBOper(pathPrefix + dbPath)
   self.db.connect()
Пример #2
0
class ResourceOperator:
  def __init__(self, dbPath='/work.sqlite3.db'):
    pathPrefix = os.path.dirname(os.path.realpath( __file__ ))
    self.db = DBOper(pathPrefix + dbPath)
    self.db.connect()

  def addFilterPattern(self, reason, tag):
    sql = 'insert into reason_of_filtered(content, type_id, create_time) values({!r},{!r},{!r})'
    curTime = time2Str(datetime.datetime.now())
    self.db.executeDML(sql.format(reason, tag, curTime))

  def addFeeds(self, feeds):
    for feed in feeds:
      self.addFeed(feed)

  def addFeed(self, feed):
    self.db.executeDML(feed.insertStmt())

  def writeValid(self, feeds):
    self._writeFeeds(feeds, 'feeds')

  def writeFiltered(self, feeds):
    self._writeFeeds(feeds, 'feeds_filtered')

  def _writeFeeds(self, feeds, table):
    sql = 'insert into {} (from_feed_id, title, link, time) values ({!r},{!r},{!r},{!r})'
    for feed in feeds:
      self.db.executeDML(sql.format(table, feed.fromFeedId, feed.title, feed.link, feed.pubDate))
      Cache.update(feed) #update cache when fetched new feed
      #TODO create a notify to make cache update

  def getFilterPattern(self):
    rs = self.db.select(queryStmt('from reason_of_filtered'))
    return [ FilterReason(e[1], e[2], e[3], e[0]) for e in rs ] 

  def getFeedResource(self):
#    feedResUpdateTime = self.getFeedResUpdateTime()
    rs = self.db.select(queryStmt('from feed_info'))
    return [ FeedRes(e[1], e[2], e[3], e[0], FeedResUpdateTime.get(e[0]) ) for e in rs ] 

  def addFeedResUpdateTime(self, feedRes):
    sql = 'insert into feed_update_time (feed_id, update_time) values ({!r}, {!r})'
    self.db.executeDML(sql.format(feedRes.id, time2Str(feedRes.pubDate)))

  def getFeedResUpdateTime(self):
    rs = self.db.select('select feed_id, update_time from feed_update_time group by feed_id')
    for r in rs:
      FeedResUpdateTime(r[0], r[1])
#    return { r[0]: r[1] for r in rs }

  def getFeeds(self, size=1000):
    rs = self.db.select(queryStmt('from feeds order by id desc limit '+str(size)))
    return [ FeedItem(r[2], r[4], r[3], r[1]) for r in rs ]

  def fillCache(self):
    self.getFeedResUpdateTime()
    for e in self.getFeeds(100):
      Cache.update(e)