西部世界 1080P高清下載和自動提醒后續(xù)新出的【Python】


title: 西部世界 1080P高清下載和自動提醒后續(xù)新出的【Python】
date: 2016-10-13 20:59:28
tags:


西部世界 1080P高清下載和自動提醒后續(xù)新出的#

1、主要思路是,通過高清源頭的網(wǎng)站提供的資源,爬取后,通過迅雷實現(xiàn)自動下載,
然后后續(xù)新出的,比如下周1更新后,腳本會自動捕捉后發(fā)郵件通知,并自動下載。

2、代碼:

# -*- coding: utf-8 -*-
# python 3.5.2
# 測試系統(tǒng),Win10
# Author:Van
# 實現(xiàn)《西部世界》有更新后自動下載,以及郵件通知
# V1.01
# 修改@href提取方法,因源頭網(wǎng)站變更
# 請把對應的帳號密碼修改成自己的


# from selenium import webdriver
import requests
from lxml import etree
import time
import os
from win32com.client import Dispatch
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import copy

# hints
print('請確保電腦安裝了迅雷')
print('如果你用的是破解版的迅雷,請先開啟再運行程序')
print()
# requests
url = 'http://www.btbtdy.com/btdy/dy7280.html'
html = requests.get(url).content.decode('utf-8')

# lxml
selector = etree.HTML(html)
real_link = []

# to be easy, try 'starts-with' , very useful in this case :)
HDTV = selector.xpath('//a[starts-with(@title, "HDTV-1080P")]/text()')
for each in HDTV:
    print(each)


# the site modified the magnet link position with adding the span
# we should use: following-sibling function to catch it :)
href = selector.xpath('//a[starts-with(@title, "HDTV-1080P")]/following-sibling::span/a/@href')
print()
print('目前有 %d 集西部世界' %len(href))
print()

for each in href:
    # split to get the right magnet link
    each = 'magnet' + each.split('magnet')[-1]
    # print(each)
    real_link.append(each)

print('他們的磁鏈接是 :\n', real_link)
# define a temp_link in deepcopy to compare for new series
temp_link = copy.deepcopy(real_link)
print('temp_link is :', temp_link)




def addTasktoXunlei(down_url,course_infos):
    flag = False
    o = Dispatch("ThunderAgent.Agent.1")
    if down_url:
        course_path = os.getcwd()
        try:
            #AddTask("下載地址", "另存文件名", "保存目錄","任務注釋","引用地址","開始模式", "只從原始地址下載","從原始地址下載線程數(shù)")
            o.AddTask(down_url, '', course_path, "", "", -1, 0, 5)
            o.CommitTasks()
            flag = True
        except Exception:

            print(Exception.message)
            print(" AddTask is fail!")
    return flag

def new_href():
    # to judge if there is a new series of WestWorld
    time.sleep(2)
    if len(real_link) > len(temp_link):
        print('西部世界1080P有更新!')
        print('現(xiàn)在一共有 %d 集了。' %len(real_link))
        return True
    else:
        return False

def send_email(htm):
    # send email to notice new WestWorld is coming
    sender = 'xxxxxxxx@163.com'
    receiver = 'xxxxxxxx@qq.com,xxxxxxxx@163.com'
    subject = '西部世界 1080P有更新!'
    smtpserver = 'smtp.163.com'
    username = 'xxxxxxxx@163.com'
    password = 'xxxxxxxx'
    msg = MIMEText(htm, 'html', 'utf-8')
    msg['Subject'] = Header(subject, 'UTF-8')
    msg['From'] = sender
    msg['To'] = ','.join(receiver)
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

def new_download():
    # only download the new WestWorld series
    if len(real_link) > len(temp_link):
        # 2個地址數(shù)據(jù)的差集
        new_link = list(set(real_link).difference(set(temp_link)))
        for i in new_link:
            addTasktoXunlei(i, course_infos=None)



if __name__ == '__main__':
    # download the exiting series of WestWorld
    # send_email('最新更新磁鏈接:'+ str(real_link))
    for i in real_link:
        addTasktoXunlei(i, course_infos=None)

    # to get the later WestWorld for each hour
    while 1:
        if new_href():
            send_email('所有的下載地址(磁鏈接):'+ str(real_link))
            new_download()
            time.sleep(15)
            # wait for an hour
            temp_link = real_link
            print(temp_link)
            print('神劇很好看吧,親,耐心等下一集!~!')

3、代碼分析,其中用到了deepcopy,這個功能很有用,并配合了2個數(shù)組的差集,使得可以規(guī)避定時器,而讓腳本直接比較temp_link的內(nèi)容,而撲捉到網(wǎng)站有新的更新了。另外,在地址識別的時候,一開始用.xpath 沒顯示內(nèi)容,有點奇怪,后來根據(jù)特性,使用了strats_with識別了內(nèi)容。另外,原始的郵件發(fā)送函數(shù),是一個接收人,如果要多發(fā),則receiver的格式為list,并修改 msg['To'] = ','.join(receiver)

4、郵件的作用是可以利用微信綁定來推送,相對短信,更覺方便。

5、感謝:
@陌 提供了163發(fā)送email的代碼
@何方 提供了高清網(wǎng)站源
@其他人,交流了細節(jié)

6、可改進點:
郵件的地址內(nèi)容顯示的是一個列表,有待改進。

7、github對應倉庫:

https://github.com/vansnowpea/WestWorld-auto-download-email-xunlei-

8、推薦 xpath學習教程:

http://zvon.org/xxl/XPathTutorial/General_chi/examples.html

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,581評論 25 708
  • 離開上海往南旅行。我知道我是上帝差派走天下的人。我在母腹中就來往于貴州上海之間了。以前飛機常來往于中國與歐洲之間時...
    撒拉JC閱讀 399評論 2 0
  • 二百多頁的小書,糖葫蘆故事串一樣,每章兩三個真實人物或案例娓娓道來,相互穿插印證、角度多樣辯證有趣,還挺好看 。 ...
    boks閱讀 395評論 0 1
  • summrr閱讀 198評論 0 1