Skip to content

Commit

Permalink
Merge pull request Python-World#544 from LeiyuanBlog/ly
Browse files Browse the repository at this point in the history
export_mysql_data_to_csv
  • Loading branch information
chavarera authored Nov 24, 2021
2 parents 69ea279 + daacb14 commit 2dbd273
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
25 changes: 25 additions & 0 deletions projects/export_mysql_to_csv_send_to_wocom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# export_mysql_to_csv_send_to_wocom

Export the data in mysql into CSV files and send them to enterprise wechat group chat.

### Prerequisites

- PyMySQL==1.0.2
- requests==2.26.0

### How to run the script

```shell
# 1. edit config.ini
$ vim config.ini
# 2. run script
$ python export_mysql_data_to_csv.py
```

### Screenshot/GIF showing the sample use of the script

![pic](./pic.png)

### Author Name

[Yuan Lei(雷园)](https://github.com/LeiyuanBlog)
10 changes: 10 additions & 0 deletions projects/export_mysql_to_csv_send_to_wocom/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[db]
host = 127.0.0.1
username = root
password = 123456
database = user
[wecom]
key = *
[message]
sql = select * from user
title = 测试文件
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
import codecs
import configparser
import csv
import time

import pymysql
import requests as requests

ini = configparser.ConfigParser()
ini.read('config.ini')
wecom_key = ini.get('wecom', 'key')


def sql(sqlstr): # 定义一个执行SQL的函数
conn = pymysql.connect(host=ini.get('db', 'host'), user=ini.get('db', 'username'),
password=ini.get('db', 'password'), database=ini.get('db', 'database'))
cursor = conn.cursor()
cursor.execute(sqlstr)
results = cursor.fetchall() # 获取查询的所有记录
cursor.close()
conn.close()
return results


def read_mysql_to_csv(filename):
with codecs.open(filename=filename, mode='w', encoding='utf-8') as f:
write = csv.writer(f, dialect='excel')
results = sql(
ini.get('message', 'sql')
)
for result in results:
write.writerow(result)


def upload_file_robots(filename):
url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=%(key)s&type=file" % {"key": wecom_key}
data = {'file': open(filename, 'rb')} # post jason
response = requests.post(url=url, files=data) # post 请求上传文件
json_res = response.json() # 返回转为json
media_id = json_res['media_id'] # 提取返回ID
return media_id # 返回请求状态


def send_file_robots(media_id):
wx_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%(key)s' % {"key": wecom_key}
data = {"msgtype": "file",
"file": {"media_id": media_id}} # post json
r = requests.post(url=wx_url, json=data)
return r


if __name__ == '__main__':
filename = ini.get('message', 'title') + time.strftime('%y%m%d') + '.csv'
read_mysql_to_csv(filename)
print(send_file_robots(upload_file_robots(filename)))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions projects/export_mysql_to_csv_send_to_wocom/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
certifi==2021.10.8
charset-normalizer==2.0.7
configparser==5.0.2
idna==3.3
importlib-metadata==4.8.1
Jinja2==3.0.2
MarkupSafe==2.0.1
prettytable==2.4.0
pyecharts==1.9.0
PyMySQL==1.0.2
requests==2.26.0
simplejson==3.17.5
typing-extensions==3.10.0.2
urllib3==1.26.7
wcwidth==0.2.5
xlwt==1.3.0
zipp==3.6.0

0 comments on commit 2dbd273

Please sign in to comment.