]> git.0d.be Git - pige-extractor.git/blob - download.cgi
Add support to extract second precise parts
[pige-extractor.git] / download.cgi
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import time
5 import cgi
6 import os
7 import sys
8 import datetime
9 import syslog
10
11 syslog.openlog('pigebox')
12
13 d = cgi.parse()
14
15 if os.environ.get('PATH_INFO'):
16     # filename has to be of the following form: %Y%m%d-%Hh%M-%Hh%M.%ext
17     filename = os.path.basename(os.environ.get('PATH_INFO'))
18     date = filename[:8]
19     start = '%s:%s' % (filename[9:11], filename[12:14])
20     seconds = 0
21     if filename[14] == 'm': # support for seconds
22         seconds += 3
23         start += ':' + filename[15:17]
24     else:
25         start += ':00'
26     end = '%s:%s' % (filename[15+seconds:17+seconds], filename[18+seconds:20+seconds])
27     if seconds:
28         end += ':' + filename[24:26]
29     else:
30         end += ':00'
31     ext = filename[-3:]
32
33     start = datetime.datetime.strptime('%s %s' % (date, start),
34            '%Y%m%d %H:%M:%S')
35     end = datetime.datetime.strptime('%s %s' % (date, end),
36            '%Y%m%d %H:%M:%S')
37 elif d:
38     end = '%s:%s' % (d.get('end_hour')[0], d.get('end_min')[0])
39
40     start = datetime.datetime.strptime('%s %s:%s' % (
41            d.get('date')[0], d.get('start_hour')[0], d.get('start_min')[0]),
42            '%d/%m/%Y %H:%M')
43     end = datetime.datetime.strptime('%s %s:%s' % (
44            d.get('date')[0], d.get('end_hour')[0], d.get('end_min')[0]),
45            '%d/%m/%Y %H:%M')
46     if d.get('wav'):
47         ext = 'wav'
48     else:
49         ext = 'ogg'
50 else:
51     print 'Location: .'
52     print ''
53     print 'Hop'
54     sys.exit(0)
55
56 if end < start:
57     end = end + datetime.timedelta(1)
58
59 floor_start = start
60 if floor_start.second:
61     floor_start = floor_start.replace(second=0)
62 floor_start = floor_start.replace(minute=floor_start.minute/15*15)
63
64 ceil_end = end
65 if ceil_end.second:
66     ceil_end = ceil_end.replace(minute=ceil_end.minute+1, second=0)
67 if ceil_end.minute%15:
68     if ceil_end.minute / 15 == 3:
69         ceil_end = ceil_end.replace(minute=0)
70         ceil_end += datetime.timedelta(seconds=3600)
71     else:
72         ceil_end = ceil_end.replace(minute=(1+(ceil_end.minute/15))*15)
73
74 os.chdir('/home/alsa-record/')
75
76 def get_filenames():
77     path = start.strftime('%Y/%m-%b/%d-%a/')
78     filenames = [os.path.join(path, x) for x in os.listdir(path)]
79     if end.hour < start.hour:
80         path = end.strftime('%Y/%m-%b/%d-%a/')
81         if os.path.exists(path):
82             filenames.extend([os.path.join(path, x) for x in os.listdir(path)])
83     filenames.sort()
84
85     filenames = [x for x in filenames if
86                 x >= floor_start.strftime('%Y/%m-%b/%d-%a/%Hh%M') and 
87                 x < ceil_end.strftime('%Y/%m-%b/%d-%a/%Hh%M')]
88     return filenames
89
90 while True:
91     filenames = get_filenames()
92     without_extension = [os.path.splitext(x)[0] for x in filenames]
93     for f in without_extension:
94         if without_extension.count(f) > 1:
95             # currently encoding, wait a moment and try again
96             print 'X-Currently-Encoding: please wait'
97             time.sleep(5)
98             break
99     else:
100         break
101
102 syslog.syslog(syslog.LOG_INFO, 'extraction (%s to %s)' % (
103                         start.strftime('%Y-%m-%d %Hh%M'),
104                         end.strftime('%Hh%M')))
105
106 if ext == 'wav':
107     print 'Content-Type: audio/x-wav'
108 else:
109     print 'Content-Type: audio/ogg'
110
111 print 'Content-Disposition: attachment; filename=%s.%s\n' % \
112                 (start.strftime('%Y-%m-%d-%Hh%M') + end.strftime('-%Hh%M'), ext)
113
114 sys.stdout.flush()
115
116 if ext == 'ogg':
117     command = ['sox'] + filenames + ['-t', 'ogg', '-C', '6', '-']
118 else:
119     command = ['sox'] + filenames + ['-t', 'wav', '-']
120
121 if start != floor_start or end != ceil_end:
122     trim_parts = ['trim']
123     if start != floor_start:
124         trim_start = (start - floor_start).seconds
125         trim_parts.append(str(trim_start))
126     else:
127         trim_start = 0
128     if end != ceil_end:
129         if trim_start:
130             duration = (end - start).seconds
131             trim_parts.append(str(duration))
132         else:
133             trim_end = (ceil_end-end).seconds
134             trim_parts.append(str(trim_end))
135     command.extend(trim_parts)
136
137 os.system(' '.join(command))
138