記一個 CSDM 下載 Script


早兩天在網上看見的一篇文章.
原理方面直接看代碼應該就了解了.
直接用 Python 實現了一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import urllib2
import argparse
import webbrowser
import json

class Downloader(object):
def __init__(self, id):
self.id = id

def download(self):
request = urllib2.Request(
"http://download.csdn.net/index.php/rest/source/getsourceinfo/{0}".format(self.id),
headers={
'User-Agent' : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
}
)
connection = urllib2.urlopen(request)
response = connection.read()
connection.close()

try:
source = json.loads(response)
webbrowser.open_new_tab(source['url'])
print("Your download is: {0}".format(source['title'].encode("UTF8")))
except:
print("Can not found the source info")

if __name__ == "__main__":
if sys.version_info < (2.7):
print("Require python version > 2.7")
else:
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("id", metavar='ID', type=int, help="The source like: http://download.csdn.net/detail/king1884/5286748\nThe ID shoud be: 5286748")

arguments = parser.parse_args()

Downloader(arguments.id).download()