def all_topics(): cur = pg.cursor() cur.execute("SELECT sid, name, read_increase, pages_increase, num_pages, count_read FROM alerts") rows = cur.fetchall() data = map(lambda x: row_to_topic(x), rows) print json.dumps(data) return json.dumps(data)
def remember(): """Remember what's already in the database to avoid re-scraping.""" global memo cursor = pg.cursor() for key in memo.keys(): cursor.execute("SELECT sid FROM {0}".format(pg.tables[key[:-1]])) rows = cursor.fetchall() for row in rows: memo[key].add(row[0]) return True
def latest(bot, update): cur = pg.cursor() cur.execute("SELECT sid, name FROM topic ORDER BY sid DESC LIMIT 10") def format(row): return """{0} https://bitcointalk.org/index.php?topic={1}""".format(row[1], row[0]) messages = map(lambda x: format(x), cur.fetchall()) message = "\r\n".join(messages) updater.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode=telegram.ParseMode.MARKDOWN)
def tearDown(self): """Teardown tables for test and restore memo.""" # Drop test tables cur = pg.cursor() for table in pg.tables.values(): cur.execute("""DROP TABLE IF EXISTS {0}""".format(table)) cur.execute("""COMMIT""") # Undo swap / sub of tables pg.tables = self.tablesOriginal # Undo swap / sub of memo global memo memo = self.memoOriginal
def setUp(self): """Setup tables and memo for test.""" # Swap and sub tables self.tablesOriginal = pg.tables pg.tables = {} for key, table in self.tablesOriginal.iteritems(): pg.tables[key] = "{0}_test".format(table) # Create test tables cur = pg.cursor() for key, table in pg.tables.iteritems(): cur.execute("""CREATE TABLE IF NOT EXISTS {0} (LIKE {1} INCLUDING ALL)""".format( table, self.tablesOriginal[key])) cur.execute("""COMMIT""") # Reset memo global memo self.memoOriginal = memo memo = {'boards': set(), 'members': set(), 'topics': set()}
def setUp(self): """Setup tables and memo for test.""" # Swap and sub tables self.tablesOriginal = pg.tables pg.tables = {} for key, table in self.tablesOriginal.iteritems(): pg.tables[key] = "{0}_test".format(table) # Create test tables cur = pg.cursor() for key, table in pg.tables.iteritems(): cur.execute("""CREATE TABLE IF NOT EXISTS {0} (LIKE {1} INCLUDING ALL)""".format( table, self.tablesOriginal[key])) cur.execute("""COMMIT""") # Reset memo global memo self.memoOriginal = memo memo = { 'boards': set(), 'members': set(), 'topics': set() }
dict["pages_increase"] = row[4] dict["read_increase"] = row[5] dict["last_day_pages"] = row[6] dict["last_day_reads"] = row[7] return dict def get_alarms(query, cur, topics): cur.execute(query) rows = cur.fetchall() print "{0} alarms found.".format(len(rows)) return map(lambda x: create_dict(x, topics), rows) cur = pg.cursor() topics = get_topics(cur) alarms = get_alarms(query, cur, topics) def chunkify(l, n): """Yield successive n-sized chunks from l.""" for i in xrange(0, len(l), n): yield l[i:i + n] if len(alarms) > 0: counter = 0 messages = [ u"🔔🔔 Announcements com aumento de relevância: 🔔🔔", "\r\n" ]