def handle_check(): conn, count = handle_open() if not conn: return '啊嘞嘞出错了,一定是数据库炸了不是咱!' if count == 0: return '库里啥也没有' nid = random.randint(1, count) cur = conn.cursor() cur.execute('SELECT mksvy FROM marx WHERE id=?;', (nid, )) msg = cur.fetchall()[0][0] method.sqlite_db_close(conn) return msg
def handle_add(msg): conn, count = handle_open() if not conn: return '添加失败' try: count += 1 cur = conn.cursor() cur.execute('INSERT INTO marx(id, mksvy) VALUES(?, ?);', (count, msg)) cur.execute(f'UPDATE marx SET mksvy="{count}" WHERE id=0;') method.sqlite_db_close(conn) except Exception as e: e.with_traceback() method.sqlite_db_close(conn) return f'添加失败: {e}' return '添加成功'
def handle_open(): conn = method.sqlite_default_db_open('message', 'marx') cur = conn.cursor() cur.execute( 'CREATE TABLE IF NOT EXISTS marx(id BIGINT, mksvy VARCHAR(2048));') cur.execute('SELECT mksvy FROM marx WHERE id=0;') count = cur.fetchall() try: if count: count = int(count[0][0]) else: count = 0 cur.execute('INSERT INTO marx (id, mksvy) VALUES(0, "0");') except Exception as e: e.with_traceback() method.sqlite_db_close(conn) conn = None count = 0 return conn, count
def main(msgdict): global subset, lastday comm = msgdict['raw_message'].split() if msgdict['message_type'] == 'private': uid = msgdict['user_id'] elif msgdict['message_type'] == 'group': uid = -msgdict['group_id'] else: timenow = time.gmtime(time.time() + 8 * 3600) if timenow.tm_year != lastday[0] or timenow.tm_mon != lastday[1] or timenow.tm_mday != lastday[2]: db = method.sqlite_db_open(database) cursor = db.cursor() cursor.execute('DELETE FROM news;') cursor.close() method.sqlite_db_close(db) lastday = [timenow.tm_year, timenow.tm_mon, timenow.tm_mday] newss = loongnix_cn_news() ans = '龙芯开源社区新闻:' postnews = [] db = method.sqlite_db_open(database) cursor = db.cursor() for s in newss: cursor.execute('SELECT content FROM news WHERE content=?;', (s, )) if len(cursor.fetchall()) == 0: postnews.append(s) cursor.execute('INSERT INTO news(content) values(?)', (s, )) cursor.close() method.sqlite_db_close(db) # print(postnews) if len(postnews) > 0: for i in range(len(postnews)): ans += f'\n{i}. {postnews[i]}' # print(ans) for id in subset: if id > 0: api.send_private_msg(id, ans) else: api.send_group_msg(-id, ans) return status = uid in subset ans = """龙芯开源社区新闻: sub 订阅 unsub 取消订阅""" if status: ans += '\n汝已订阅' else: ans += '\n汝未订阅' if len(comm) > 1 and comm[1] in ['sub', 'unsub']: db = method.sqlite_db_open(database) cursor = db.cursor() if comm[1] == 'sub' and not status: subset.add(uid) cursor.execute('INSERT INTO ids(id) values(?)', (uid, )) elif comm[1] == 'unsub' and status: subset.remove(uid) cursor.execute('DELETE FROM ids WHERE id=?', (uid, )) cursor.close() method.sqlite_db_close(db) ans = '汝的操作成功执行' return ans
starttime = time.gmtime(time.time() + 8 * 3600) lastday = [starttime.tm_year, starttime.tm_mon, starttime.tm_mday] subset = set() database = method.sqlite_get_name('message', 'loongnews') mydb = method.sqlite_db_open(database) cursor = mydb.cursor() cursor.execute('CREATE TABLE IF NOT EXISTS ids(id Long);') cursor.execute('CREATE TABLE IF NOT EXISTS news(content Text);') ids = cursor.execute('SELECT id FROM ids;') ids = ids.fetchall() for id in ids: subset.add(id[0]) cursor.close() method.sqlite_db_close(mydb) def get_page(url): try: res = requests.get(url=url, timeout=5) except Exception as e: pass else: if res.status_code == 200: return res.text return '' def loongnix_cn_news(): """