-
Notifications
You must be signed in to change notification settings - Fork 1
/
rss.py
executable file
·92 lines (78 loc) · 2.65 KB
/
rss.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# marek kuczynski
# www.marek.asia
# sudo apt-get install python-eyed3
from time import sleep
import eyeD3, sys, os, hashlib, time, shutil
from SimpleXMLWriter import XMLWriter
# import configuration file
from cfg import *
###
# define the output of the program (either stdout or file)
xml = XMLWriter('rss.xml')
# head of the file
output = xml.start('rss', version='2.0')
# channel part of the file
xml.start('channel')
xml.element('title', title)
xml.element('link', url)
xml.element('language', 'en-us')
xml.element('description', description)
xml.element('author', author)
xml.element('category', category)
xml.start('image')
xml.element('url', image)
xml.end('image')
# channel owner information
xml.start('owner')
xml.element('name', author)
xml.element('email', email)
xml.end('owner')
# check of the dirpath for music has been defined, else set to current dir
if musicpath == "":
musicpath = '.'
# calculate the amount of chars in the defined webroot
rootchars = int(musicpath.count('')) - 1
# crawl the current directory recursively
for dirname, dirnames, filenames in os.walk(musicpath):
dirnames.sort()
filenames.sort()
for filename in filenames:
if 'mp3' in filename or 'm4a' in filename:
# define the path of the fime
path = os.path.join(dirname, filename)
print path
# get the dir structure from the webroot
webpath = path[rootchars:]
# create a new item for every file
xml.start('item')
tag = eyeD3.Tag()
tag.link(path)
fileinfo = eyeD3.Mp3AudioFile(path)
# merge artist + trackname
mtrack = filename.split('.')[0]
martist = webpath.split('/'[0])[0]
#print mtrack+' - '+martist
# retrieve several required fields
xml.element('title', martist+' - '+mtrack)
xml.element('enclosure', url=str(url + '/' + webpath), length=str(fileinfo.getSize()), type='audio/mpeg')
#xml.element('pubDate', time.ctime(os.path.getctime(path)))
#xml.element('category', webpath.split('/')[0])
#xml.element('description', str(webpath.split('/')[0]))
# the following attributes are optional, uncomment if needed
xml.element('duration', fileinfo.getPlayTimeString())
#xml.element('keywords', tag.getComment())
#xml.element('album', tag.getAlbum())
#xml.element('bpm', str(tag.getBPM()))
#xml.element('author', tag.getArtist())
xml.element('link', link)
# calculate the guid by making an md5 hash
guid = hashlib.md5()
guid.update(tag.getTitle() + tag.getArtist() + path)
xml.element('guid', guid.hexdigest())
sleep(0.2)
xml.end('item')
# close tags and write file
#xml.end('channel')
xml.close(output)
shutil.copyfile('/var/www/music/rss/rss.xml','/var/www/rss.xml')