-
Notifications
You must be signed in to change notification settings - Fork 5
/
SourceForgeURLProvider.py
76 lines (55 loc) · 1.92 KB
/
SourceForgeURLProvider.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
#!/usr/bin/env python
import datetime
import re
from xml.dom.minidom import parse, parseString
import urllib2
from autopkglib import Processor, ProcessorError
__all__ = ["SourceForgeURLProvider"]
FILE_INDEX_URL = 'http://sourceforge.net/api/file/index/project-id/%s/rss'
class SourceForgeURLProvider(Processor):
'''Provides URL to the latest file that matches a pattern for a particular SourceForge project.'''
input_variables = {
'SOURCEFORGE_PROJECT_ID': {
'required': True,
'description': 'Numerical ID of SourceForge project',
},
'SOURCEFORGE_FILE_PATTERN': {
'required': True,
'description': 'Pattern to match SourceFile files on',
},
}
output_variables = {
'url': {
'description': 'URL to the latest SourceForge project download'
}
}
description = __doc__
def get_sf_file_url(self, proj_id, pattern):
flisturl = FILE_INDEX_URL % proj_id
try:
f = urllib2.urlopen(flisturl)
rss = f.read()
f.close()
except BaseException as e:
raise ProcessorError('Could not retrieve RSS feed %s' % flisturl)
re_file = re.compile(self.env.get('SOURCEFORGE_FILE_PATTERN'), re.I)
rss_parse = parseString(rss)
items = []
for i in rss_parse.getElementsByTagName('item'):
pubDate = i.getElementsByTagName('pubDate')[0].firstChild.nodeValue
link = i.getElementsByTagName('link')[0].firstChild.nodeValue
pubDatetime = datetime.datetime.strptime(pubDate, '%a, %d %b %Y %H:%M:%S +0000')
if re_file.search(link):
items.append((pubDatetime, link),)
items.sort(key=lambda r: r[0])
if len(items) < 1:
raise ProcessorError('No matched files')
return items[-1][1]
def main(self):
proj_id = self.env.get('SOURCEFORGE_PROJECT_ID')
file_pat = self.env.get('SOURCEFORGE_FILE_PATTERN')
self.env['url'] = self.get_sf_file_url(proj_id, file_pat)
self.output('File URL %s' % self.env['url'])
if __name__ == '__main__':
processor = SourceForgeURLProvider()
processor.execute_shell()