-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxspf-checker.py
executable file
·64 lines (53 loc) · 1.73 KB
/
xspf-checker.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
#
# Check xspf playlist for broken links
import os, sys, requests
from xml.dom import minidom
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
def getChildren(node, key):
for child in node.childNodes:
if child.localName == key:
yield child
def readPlaylist(path):
playlist = []
if os.path.isfile(path):
dom = minidom.parse(path)
tracks = dom.getElementsByTagName('track')
for node in tracks:
titles = []
locations = []
for t in getChildren(node, 'title'):
titles.append(t.childNodes[0].nodeValue)
for l in getChildren(node, 'location'):
locations.append(l.childNodes[0].nodeValue)
for i in range(len(titles)):
playlist.append([ titles[i], locations[i] ])
else:
print('file not found.')
sys.exit()
return playlist
def getURL(url):
HEADERS = {'User-Agent':'VLC/3.0.12 LibVLC/3.0.12'}
try:
r = requests.get(url, headers=HEADERS, timeout=20, allow_redirects=True, stream=True, verify=False)
if r.status_code == requests.codes.ok:
return True
else:
return False
except:
return False
def check(playlist):
for item in playlist:
result = getURL(item[1])
if result:
print('OK ::', item[0], '-', item[1][:50])
else:
print('ERR ::', item[0], '-', item[1][:50])
if __name__== "__main__":
if len(sys.argv) < 2:
print('xspf-checker.py [path-to-xspf]')
sys.exit()
path = sys.argv[1]
print('checking...')
check(readPlaylist(path))