Child: [77df3f] (diff)

Download this file

utils.py    56 lines (50 with data), 1.6 kB

 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
from xml.dom import minidom
import urllib2
import OpenSourceProject
class Configuration():
api_key = OpenSourceProject.ohloh_api_key
max_number_of_records = OpenSourceProject.local_record_limit
flossmole_locale = OpenSourceProject.flossmole_locale
class Forges(object):
ALL = 0
#SF = Sourceforge
SF = 1
#FC = Freecode was FM = Freshmeat
FC = 2
#FSF = Free Software Foundation
FSF = 5
#GH = Github
GH = 11
#GC = Google code
GC = 12
OHLOH = 100
CODEPLEX = 1000
APACHE = 1200
ECLIPSE = 1300
class xmlElements():
def __init__(self, xml):
self.xml = xml
def firstValue(self):
try:
return self.xml[0].firstChild.nodeValue
except:
return ""
class UrllibHelper():
@staticmethod
def urlopen(url_string, max_attempts = 3):
'''
Sometimes the download just fails for no apparent reason; retrying right after the
failure solves the issue; so this method retries max_attempts times with a default of 3
'''
success = False
n_attempts = 1
ret = ""
while not (success or n_attempts>max_attempts):
try:
response = urllib2.urlopen(url_string)
ret = response.read()
success = True
except Exception as ex:
print('Error downloading ' + url_string + " - Attempt n.:" + str(n_attempts) + " - " + str(ex))
n_attempts = n_attempts + 1
return ret