#!/usr/bin/python3
# -*- coding: utf-8 -*-
import csv,time
from datetime import datetime
import ftplib
import pymysql
def open_db():
try:
conn = pymysql.connect(host='', port=, user='', passwd='', db='cache_xxz', charset='utf8')
return conn,conn.cursor(cursor=pymysql.cursors.DictCursor)
except Exception as ex:
print("err mysql:数据库连接失败,请检查配置!", ex)
def records():
cnn, cur = open_db()
cur.execute("SELECT IF(LOCATE('https://', url), SUBSTRING(url, 9, LOCATE('/', url, 9) - 9), SUBSTRING(url, 8, LOCATE('/', url, 8) - 8)) AS domain,title,publishtime,content,url from other_media WHERE inserttime>NOW()-INTERVAL 30 MINUTE")
result = cur.fetchall()
return result
def save_file():
try:
print('save_file')
rs = records()
out = open('{}.csv'.format(datetime.strftime(datetime.now(), "%Y-%m-%d %H%M")), 'a', encoding='utf8', newline='')
csv_write = csv.writer(out, dialect='excel')
for r in rs:
row = [r['domain'], r['title'], r['publishtime'], r['content'], r['url']]
csv_write.writerow(row)
out.close()
ftp_upload()
except Exception as ex:
print(ex)
def ftp_upload():
print('ftp_upload')
host = ''
username = ''
password = ''
f = ftplib.FTP(host)
f.login(username, password)
'''以二进制形式上传文件'''
r = '{}.csv'.format(datetime.strftime(datetime.now(), "%Y-%m-%d %H%M"))
l = '{}.csv'.format(datetime.strftime(datetime.now(), "%Y-%m-%d %H%M"))
buf = 2048
fp = open(l, 'rb')
f.storbinary('STOR ' + r, fp, buf)
fp.close()
f.quit()
print('ftp_upload done.')
if __name__ == '__main__':
while True:
if datetime.now().minute == 30:
save_file()
elif datetime.now().minute == 59:
save_file()
time.sleep(60)