123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import configparser
- import os,sys,re,shutil
- import subprocess
- import time
- import datetime
- import argparse
- import logging
- from selenium import webdriver
- from selenium.webdriver import ActionChains
- from taobao.mail.mail_sender import MailSender
- from util.qiniuUtil import *
- from __init__ import *
- from taobao.csdn.download import CsdnDownloader
- from mail.mail_message import *
- from mail.mail_sender import *
- from mail.mail_sender_browser import MailSenderBrowser
- from taobao.order_bot import TaobaoClimber
- from util.str_util import print_msg, send_mail
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.DEBUG)
- # 再创建一个handler,用于输出到控制台
- ch = logging.StreamHandler()
- ch.setLevel(logging.DEBUG)
- # 定义handler的输出格式
- formatter = logging.Formatter(
- '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
- ch.setFormatter(formatter)
- # 给logger添加handler
- logger.addHandler(ch)
- def set_args():
- '''
- write config to config/conf.ini
- '''
- cf = configparser.ConfigParser()
- cf.read('config/conf.ini')
- if not cf.has_section("taobao"):
- cf.add_section("taobao")
- cf.set("taobao","username","zhangsan")
- cf.set("taobao","password","1346")
- cf.set("taobao","shelve_url","https://taobao.com/xx")
- # save
- with open('config/conf.ini', 'w') as f:
- cf.write(f)
- def get_args()->configparser.ConfigParser:
- parser = argparse.ArgumentParser()
- parser.add_argument('-c', '--conf', help='config file path', default='config/conf.ini')
- args = parser.parse_args()
- conf_file = args.conf
- if not os.path.exists(conf_file):
- print("配置文件不存在!")
- sys.exit(1)
- cf = configparser.ConfigParser.ConfigParser()
- cf.read(conf_file)
- return cf
- if __name__ == '__main__':
- cf=get_args()
- climber = TaobaoClimber(cf.get("taobao","username"), cf.get("taobao","password"))
- downloader = CsdnDownloader(cf.get("csdn","usernmae"), cf.get("csdn","password"))
- sender = MailSender(cf.get("mail","username"), cf.get("mail","authorcode"))
- sender_browser = MailSenderBrowser(cf.get("mail","username"), cf.get("mail","password"), cf.get("mail","password2"))
- # 2.实例化driver
- driver = webdriver.Firefox() # 应将浏览器驱动放于python根目录下,且python已配置path环境变量
- action = ActionChains(driver)
- driver.maximize_window() # 浏览器最大化
- driver.set_page_load_timeout(cf.get("other","delay_wait")) # 设定页面加载限制时间
- TaobaoClimber.driver = CsdnDownloader.driver = MailSenderBrowser.driver = driver
- TaobaoClimber.action = CsdnDownloader.action = MailSenderBrowser.action = action
- # 3.建立邮箱标签页
- if mail_send_type == 2:
- driver.execute_script("window.open('')")
- # 正则:解析留言内容
- re_note = re.compile(
- ur"留言:\s*([\w.-]+@[\w.-]+\.\w+)\s+((?:https?://)?[-A-Za-z0-9+&@#/%?=~_|!,.;]+)\s*")
- # 休眠总时间
- sleep_total_time = 0
- # 存在未留言订单
- exists_no_note_order = False
- # 2.1上架宝贝
- climber.shelve()
- is_running = True
- while is_running:
- # 2.2爬取订单
- orders = climber.climb()
- orders_len = len(orders)
- for order in orders:
- if downloader.download_count >= download_total:
- send_mail(sender, message_over_download_total, orders_len)
- is_running = False
- break
- note_array = re.findall(re_note, order[3])
- if len(note_array) != 1:
- exists_no_note_order = True
- continue
- order_info = "【淘宝】已产生可操作订单:订单号:%s\t订单日期:%s \t买家:%s\t备注:%s" % order
- print_msg(order_info)
- user_to = note_array[0][0]
- remote_url = note_array[0][1]
- # 2.3下载资源
- local_path = downloader.download(remote_url, local_dir)
- if local_path is None:
- send_mail(sender, message_download_false, order[0])
- continue
- else:
- print_msg("【CSDN】" + user_to + "的文件下载成功,本地路径:" + local_path)
- orders_len -= 1
- # 2.4进行下架判断
- if downloader.download_count == download_total - 1:
- if climber.unshelve() is False:
- send_mail(sender, message_unshelve_false, downloader.download_count)
- # 2.5 发送邮件
- if mail_send_type == 0:
- download_url_text = server_file_url + os.path.basename(local_path)
- if sender.send(Mail(user_to, download_url_text)):
- print_msg("【邮件】" + user_to + "的邮件发送成功")
- else:
- send_mail(sender, message_send_false, order[0])
- continue
- elif mail_send_type == 1:
- if sender.send(Mail(user_to, local_path, 2)):
- print_msg("【邮件】" + user_to + "的邮件发送成功")
- else:
- send_mail(sender, message_send_false, order[0])
- continue
- elif mail_send_type == 2:
- ret = sender_browser.send(user_to, local_path)
- if ret is None:
- print_msg("【邮件-浏览器】" + user_to + "的邮件发送成功")
- else: # 发送失败
- send_mail(sender, message_send_mail_error, order[0], ret)
- continue
- else:
- download_url_text = upload_file(local_path)
- if sender.send(Mail(user_to, download_url_text)):
- print_msg("【七牛】" + user_to + "的邮件发送成功")
- else:
- send_mail(sender, message_send_false, order[0])
- continue
- # 2.6 订单改为已发货
- if climber.delivered(order[0]) is False:
- send_mail(sender, message_delivered_false, order[0])
- time.sleep(check_order_period) # 每指定时间抓一次
- sleep_total_time += check_order_period
- if sleep_total_time >= check_refunding_period: # 每指定时间检查一次退款和未留言订单
- if climber.exists_refunding():
- send_mail(sender, message_exists_refunding)
- if exists_no_note_order:
- send_mail(sender, message_notice_for_no_note)
- exists_no_note_order = False
- sleep_total_time = 0
|