诱因
豆腐一不抽烟二不喝酒,唯一可以算得上嗜好的就是 Anime 了。而且喜欢追新番,每周一集从不落下。
坐在电脑前第一首要的的事情就是翻翻各大BT页,看关注的动画是否出新,时间久了便成了习惯虽然繁琐却也不觉着有什么不便。
但最近即将因为工作的原因不得不到外地去一个月,而且多半驻地没有网络,难道只能等回来用 eMule 慢慢拖了么?
如果可以做到自动下载BT种子然后进而下载相应的文件就好了,上网搜刮一番却没有发现合用的现成软件,看来又只能 DIY 了。
思路
欲善其功,先利其器
最近CMD脚本用得顺手、Curl也正合我用,老搭档一出手BT种子应该是手到擒来才是。
接着是BT下载的问题,BitComet是豆腐常用的,翻翻说明有支持命令行。嗯,就是它了。
最后是自动定时运行,不需其它软件捉刀,Windows自带的任务计划足以。
知己知彼,百战不殆
要想从别人那里拿东西,当然就得熟悉别人的规矩。对于提供BT种子的网页要分别分析其结构才行,选择那些结构明晰的可以省不少功夫。
以豆腐常常出没的漫游BT页为例:
全部包含种子的条目都是在一个<table>里面每个<td>元素里面的结构都是有序的分为置顶和一般两种情况。
如此一来就便于用 FOR 来取得相应的 URL。再看 href 里面的 HASH 值正是实际种子文件的文件名,这样用 CURL 来取是再方便不过了。
而且只要把每次已经下载的种子的文件名保存到一个外部文件,在反复执行的时候就可以通过比较来排除这些已经下载过的文件。
实践
为了顺藤摸瓜,先用 Curl 取得BT页的首页:
curl -o tmp.txt http://bt.popgo.net/
要顺利找到欲下载的符合只定条件的项目,FINDSTR 是不错的选择,这里豆腐以‘漫游字幕组’为关键字的即可收录所有该组的作品:
'findstr "漫游字幕组" tmp.txt^|findstr "href"'
分析 tmp.txt 文件之后决定用‘=’和‘>’作分隔符,取出分割后的第5、6、9段字符串存入环境变量 i、j、k,
利用变量 i 来判断是否置顶的条目,如果是则 k 为种子文件名,否则 j 为种子文件名。
最后再对字符串作相应的截取处理,在代入 Curl 参数取得种子文件之前还要检查是否该文件已经被下载过了:
for /f "tokens=5,6,9 delims==>" %%i in ('findstr "漫游字幕组" tmp.txt^|findstr "href"') do (
set flag=1
set tmp=%%i
if "!tmp:~1,2%!"=="置顶" (
set temp=%%k
set posthash=!temp:~0,-1%!
) else (
set temp=%%j
set posthash=!temp:~0,-1%!
)
for /f "delims=" %%j in (download.txt) do if !posthash!==%%j set flag=0
if !flag!==1 (
curl -O http://bt.popgo.net/allowed/!posthash!.torrent
)
)
得到种子文件之后,用 START 命令来给 BitComet.exe 传送命令行参数启动BT下载。
‘/s’代表不需要确认立即开始下载的安静模式;‘/tray’代表启动时最小化到系统托盘;‘/o’代表储存文件的路径:
start bitcomet.exe /s /tray !posthash!.torrent /o e:\downloads\
最后把已经下载过的文件名存入 download.txt 文件,以便下次使用:
@echo !posthash!>>download.txt
完成
最终完整代码为:
@echo off
setlocal ENABLEDELAYEDEXPANSION
curl -o tmp.txt http://bt.popgo.net/
for /f "tokens=5,6,9 delims==>" %%i in ('findstr "漫游字幕组" tmp.txt^|findstr "href"') do (
set flag=1
set tmp=%%i
if "!tmp:~1,2%!"=="置顶" (
set temp=%%k
set posthash=!temp:~0,-1%!
) else (
set temp=%%j
set posthash=!temp:~0,-1%!
)
for /f "delims=" %%j in (download.txt) do if !posthash!==%%j set flag=0
if !flag!==1 (
curl -O http://bt.popgo.net/allowed/!posthash!.torrent
start bitcomet.exe /s /tray !posthash!.torrent /o e:\downloads\
@echo !posthash!>>download.txt
)
)
endlocal
把上述代码保存为test.cmd,确认Task Scheduler为‘自动’,启动任务计划新增一个任务执行test.cmd。
设定开始时间为每天 0:00 开始、每隔 30 分钟、为时 24 小时就行了:
该脚本将每隔 30 分钟检查一次指定网站,下载最新的漫游字幕组的翻译作品。至此大功告成,豆腐终于可以安心外出了。
本文为chenke_ikari原创,首发于
豆腐的简陋小屋
本文采用
Creative Commons 署名-非商业性使用-相同方式共享 2.5 China 许可协议 进行许可
Last edited by ikari on 2006-8-2 at 13:41 ]
**Inducement**
Doufu neither smokes nor drinks. The only hobby he can be considered to have is Anime. And he likes to follow new episodes, never missing a single one every week.
The first thing to do when sitting in front of the computer is to flip through various BT pages to see if the animated series he cares about has been updated. Over time, this has become a habit. Although it is tedious, he doesn't feel any inconvenience.
But recently, due to work reasons, he has to go to another place for a month, and most of the stations have no network. Does he have to wait until he comes back to drag it slowly with eMule?
If he can automatically download the BT torrent and then download the corresponding files, that would be great. He searched the Internet but didn't find a suitable ready-made software. It seems he has to DIY again.
**Idea**
To do a good job, one must first sharpen one's tools
Recently, CMD scripts are easy to use, and Curl is just right for him. The old partner should be able to get the BT torrent easily.
Then there is the problem of BT downloading. BitComet is what Doufu often uses. Looking through the instructions, it supports the command line. Well, that's it.
Finally, for automatic timing operation, no other software is needed. The task scheduler built into Windows is enough.
Know yourself and know your enemy, and you can win a hundred battles
To get things from others, of course, one must be familiar with their rules. One should analyze the structure of the web pages that provide BT torrents respectively. Choosing those with clear structures can save a lot of work.
Take the BT page where Doufu often hangs out as an example:
All the entries containing torrents are in a <table>. The structure in each <td> element is ordered and divided into two situations: top-sticked and general.
In this way, it is convenient to use FOR to get the corresponding URL. Then look at the HASH value in href, which is exactly the file name of the actual torrent file. So it is very convenient to get it with CURL.
Moreover, as long as the file names of the torrents that have been downloaded each time are saved to an external file, when executing repeatedly, these downloaded files can be excluded by comparison.
**Practice**
To follow the clues, first use Curl to get the homepage of the BT page:
curl -o tmp.txt http://bt.popgo.net/
To successfully find the items that meet the specified conditions to be downloaded, FINDSTR is a good choice. Here, Doufu takes "Man漫游Subtitle Group" as the key word, and all the works of this group can be included:
'findstr "漫游字幕组" tmp.txt^|findstr "href"'
After analyzing the tmp.txt file, decide to use '=' and '>' as delimiters, take the 5th, 6th, and 9th segments of the divided strings and store them in environment variables i, j, k.
Use variable i to judge whether it is a top-sticked entry. If it is, k is the torrent file name; otherwise, j is the torrent file name.
Finally, make corresponding interception processing of the string. Before substituting into the Curl parameter to get the torrent file, it is also necessary to check whether the file has been downloaded:
for /f "tokens=5,6,9 delims==>" %%i in ('findstr "漫游字幕组" tmp.txt^|findstr "href"') do (
set flag=1
set tmp=%%i
if "!tmp:~1,2%!"=="置顶" (
set temp=%%k
set posthash=!temp:~0,-1%!
) else (
set temp=%%j
set posthash=!temp:~0,-1%!
)
for /f "delims=" %%j in (download.txt) do if !posthash!==%%j set flag=0
if !flag!==1 (
curl -O http://bt.popgo.net/allowed/!posthash!.torrent
)
)
After getting the torrent file, use the START command to pass command line parameters to BitComet.exe to start BT downloading.
'/s' means quiet mode without confirmation to start downloading immediately; '/tray' means minimize to the system tray when starting; '/o' means the path to store files:
start bitcomet.exe /s /tray !posthash!.torrent /o e:\downloads\
Finally, store the file name that has been downloaded into the download.txt file for next use:
@echo !posthash!>>download.txt
**Completion**
The final complete code is:
@echo off
setlocal ENABLEDELAYEDEXPANSION
curl -o tmp.txt http://bt.popgo.net/
for /f "tokens=5,6,9 delims==>" %%i in ('findstr "漫游字幕组" tmp.txt^|findstr "href"') do (
set flag=1
set tmp=%%i
if "!tmp:~1,2%!"=="置顶" (
set temp=%%k
set posthash=!temp:~0,-1%!
) else (
set temp=%%j
set posthash=!temp:~0,-1%!
)
for /f "delims=" %%j in (download.txt) do if !posthash!==%%j set flag=0
if !flag!==1 (
curl -O http://bt.popgo.net/allowed/!posthash!.torrent
start bitcomet.exe /s /tray !posthash!.torrent /o e:\downloads\
@echo !posthash!>>download.txt
)
)
endlocal
Save the above code as test.cmd. Confirm that Task Scheduler is 'Automatic'. Add a new task in Task Scheduler to execute test.cmd.
Set the start time to start at 0:00 every day, every 30 minutes, for 24 hours:
This script will check the specified website every 30 minutes and download the latest translated works of Man漫游Subtitle Group. So far, it's done. Doufu can finally go out with peace of mind.
This article is original by chenke_ikari, first published on
Doufu's Simple Hut
This article is licensed under the
Creative Commons Attribution-NonCommercial-ShareAlike 2.5 China License
Last edited by ikari on 2006-8-2 at 13:41 ]