-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo.py
50 lines (38 loc) · 1.65 KB
/
info.py
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
39
40
41
42
43
44
45
46
47
48
# coding:utf-8
from twisted.internet import reactor
from twisted.internet import protocol
from twisted.internet.defer import Deferred
import json
class InfoProcessProtocol(protocol.ProcessProtocol):
data = ""
def __init__(self, deferred):
self.deferred = deferred
def outReceived(self, data):
self.data += data
def processEnded(self, reason):
try:
info = json.loads(self.data)
item = []
for v in info.get("formats"):
#剔除一些不需要的项目
if v.get("format_note") and "x" not in v.get("format") and not v.get("filesize"):
item.append({"format": v.get("format").split(' - ')[-1].lstrip(),
"ext": v.get("ext")})
result = json.dumps({
"url": info.get("webpage_url"),
"title": info.get("title"),
"items": item
})
self.deferred.callback(result)
except:
self.deferred.errback(Exception("error"))
def getinfo(url):
# http://www.youtube.com/watch?v=-wNyEUrxzFU
d = Deferred()
d.addCallback(lambda x:x)
reactor.spawnProcess(InfoProcessProtocol(d), '/usr/local/bin/youtube-dl', ["youtube-dl",
"--socket-timeout", "5",
"--skip-download",
"--print-json", "--newline",
url])
return d