-
Notifications
You must be signed in to change notification settings - Fork 8
/
podcast.py
executable file
·210 lines (173 loc) · 6.72 KB
/
podcast.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# -*- coding: utf8 -*-
import MySQLdb
from contextlib import closing
import socket
from email.utils import formatdate
import datetime
import time
import sys
import os
import login
HOST_NAME = 'http://' + socket.gethostname()
PODCAST_IMG_PATH = '/var/www/img/podcast/'
PODCAST_PATH = '/var/www/podcast/'
def get_station_name(connection, station_alias):
with closing(connection.cursor()) as cursor:
cursor.execute(
'SELECT name FROM sender WHERE alias=%s', (
station_alias,)
)
row = cursor.fetchone()
return row[0]
def get_podcast_img(station_alias):
podcast_img = PODCAST_IMG_PATH + station_alias + '.jpg'
if os.path.isfile(podcast_img) is False:
podcast_img = HOST_NAME + '/img/podcast/' + 'default.jpg'
else:
podcast_img = HOST_NAME + '/img/podcast/' + station_alias + '.jpg'
return podcast_img
def create_podcast_header(station, last_build_date, podcast_img):
podcast_header = [
'<?xml version=\"1.0\" encoding=\"UTF-8\"?>',
'<rss xmlns:itunes=\"http://www.itunes.com/'
+ 'dtds/podcast-1.0.dtd\" version=\"2.0\">',
'<channel>',
'<title>RadioBeere - '
+ station
+ '</title>',
'<link>'
+ HOST_NAME
+ '</link>',
'<description>Hier hörst du alle Sendungen, die du mit'
+ ' der RadioBeere aufgenommen hast.</description>',
'<language>de-de</language>',
'<pubDate>'
+ last_build_date
+ '</pubDate>',
'<lastBuildDate>'
+ last_build_date
+ '</lastBuildDate>',
'<generator>RadioBeere</generator>',
'<category>Society & Culture</category>',
'<image>',
'<url>'
+ podcast_img
+ '</url>',
'<title>RadioBeere - '
+ station
+ '</title>',
'<link>'
+ HOST_NAME
+ '</link>',
'</image>',
'<itunes:subtitle>Hören, wann du willst!'
+ '</itunes:subtitle>',
'<itunes:author>'
+ station
+ '</itunes:author>',
'<itunes:summary>Hier hörst du alle Sendungen, die du '
+ 'mit der RadioBeere aufgenommen hast.</itunes:summary>',
'<itunes:image href=\"'
+ podcast_img
+ '\" />',
'<itunes:category text=\"Society & Culture\" />',
'\n'
]
podcast_header = '\n'.join(podcast_header)
return podcast_header
def create_podcast_item(title, audio_file, length_bytes, guid,
pub_date, podcast_img, station, length):
podcast_item = [
'<item>',
'<title>'
+ title
+ '</title>',
'<link>'
+ HOST_NAME
+ '/Aufnahmen/'
+ audio_file
+ '</link>',
'<description>Aufgenommen mit der RadioBeere.'
+ '</description>',
'<enclosure url=\"'
+ HOST_NAME
+ '/Aufnahmen/'
+ audio_file
+ '\" length=\"'
+ str(length_bytes)
+ '\" type=\"audio/mpeg\" />',
'<guid isPermaLink=\"false\">'
+ guid
+ '</guid>',
'<pubDate>'
+ pub_date
+ '</pubDate>',
'<image>',
'<url>'
+ podcast_img
+ '</url>',
'<title>RadioBeere - '
+ station
+ '</title>',
'<link>'
+ HOST_NAME
+ '</link>',
'</image>',
'<itunes:author>'
+ station
+ '</itunes:author>',
'<itunes:subtitle>Hören, wann du willst!'
+ '</itunes:subtitle>',
'<itunes:image href=\"'
+ podcast_img
+ '\" />',
'<itunes:duration>'
+ length
+ '</itunes:duration>',
'</item>',
'\n'
]
podcast_item = '\n'.join(podcast_item)
return podcast_item
def main():
with closing(MySQLdb.connect(
login.DB_HOST, login.DB_USER,
login.DB_PASSWORD, login.DB_DATABASE)) as connection:
station_alias = sys.argv[1]
station = get_station_name(connection, station_alias)
last_build_date = formatdate(time.time(), True)
podcast_img = get_podcast_img(station_alias)
xml_file = PODCAST_PATH + station_alias + '.xml'
feed = open(xml_file, 'w')
podcast_header = create_podcast_header(
station, last_build_date, podcast_img)
feed.write(podcast_header)
feed.close
feed = open(xml_file, 'a')
with closing(connection.cursor()) as cursor:
cursor.execute(
'SELECT * FROM aufnahmen WHERE sender=%s', station,)
result = cursor.fetchall()
for db_record in result:
recording_time = datetime.datetime.fromtimestamp(db_record[5])
podcast_item = create_podcast_item(
'{0}, {1:%d.%m.%Y, %H:%M} Uhr'.format(
station, recording_time),
db_record[4],
db_record[7],
HOST_NAME
+ '/podcast/'
+ station_alias
+ '_'
+ str(db_record[0]),
db_record[8],
podcast_img,
station,
db_record[6]
)
feed.write(podcast_item)
feed.write('</channel>\n</rss>')
feed.close
if __name__ == '__main__':
main()