Last active
December 3, 2020 14:21
-
-
Save medmin/54cd48aab25a520fc0dc9ba4a4489609 to your computer and use it in GitHub Desktop.
SaveAllWeixinMessages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from wxpy import * | |
import pymysql.cursors | |
import datetime | |
bot = Bot(console_qr=True) | |
@bot.register(None, TEXT, False) | |
def saveMsg(msg): | |
# Manipulate msg string | |
msgID = msg.id | |
msgRaw = str(msg) | |
msgSrcName = msg.chat.name | |
msgSrcID = msg.chat.wxid | |
msgTextContent = msg.text | |
msgCreatedDate = msg.create_time.date() | |
msgCreatedDatetime = msg.create_time | |
# Connect to the database | |
connection = pymysql.connect(host='localhost', | |
user='myDBUser', | |
password='myDBpw', | |
db='myDB', | |
charset='utf8mb4', | |
cursorclass=pymysql.cursors.DictCursor) | |
try: | |
with connection.cursor() as cursor: | |
# Create a new record | |
sql = "INSERT INTO `myTable` (`msgID`, `msgRaw`, `msgSrcName`, `msgSrcID`, `msgTextContent`, `msgCreatedDate` ,`msgCreatedDatetime`) VALUES (%s, %s, %s, %s, %s, %s, %s)" | |
cursor.execute(sql, (msgID, msgRaw, msgSrcName, msgSrcID, msgTextContent, msgCreatedDate, msgCreatedDatetime)) | |
# connection is not autocommit by default. So you must commit to save your changes. | |
connection.commit() | |
finally: | |
connection.close() | |
bot.join() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `myTable` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`msgID` BIGINT UNSIGNED NOT NULL, | |
`msgRaw` TEXT NOT NULL, | |
`msgSrcName` varchar(255) NOT NULL, | |
`msgSrcID` varchar(255) NOT NULL, | |
`msgTextContent` TEXT NOT NULL, | |
`msgCreatedDate` DATE NOT NULL, | |
`msgCreatedDatetime` DATETIME NOT NULL, | |
`DaysToExpirationDate` int(11) NOT NULL DEFAULT 90, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_general_ci AUTO_INCREMENT=1; | |
//注意上面的: | |
//1。msgID是64位整型,正整数 | |
//2。DATE和DATETIME类型 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment