forked from Python-World/python-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Python-World#544 from LeiyuanBlog/ly
export_mysql_data_to_csv
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |
This file contains 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
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 = 测试文件 |
56 changes: 56 additions & 0 deletions
56
projects/export_mysql_to_csv_send_to_wocom/export_mysql_data_to_csv.py
This file contains 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
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
17
projects/export_mysql_to_csv_send_to_wocom/requirements.txt
This file contains 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
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 |