Switch to unified view

a b/OSSEval/OSProject/ohloh.py
1
# This Source Code Form is subject to the terms of the Mozilla Public
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5
# Copyright 2014 Bitergium SLL
6
7
import urllib2
8
from xml.dom import minidom
9
from utils import Configuration, xmlElements, Forges
10
from OSSEval.utils import TrivialJSONEncoder 
11
12
class OhlohProxy():
13
    
14
    def search_json(self, name):
15
        a = TrivialJSONEncoder().encode(OhlohProxy.search(name))
16
        return a
17
18
    @staticmethod
19
    def search(name):
20
        page = 1
21
        print "Ohloh search - " + name + " - page " + str(page)
22
        url_ohloh = "https://www.ohloh.net/p.xml?api_key=" + Configuration.api_key + "&query=" + name + "&page="
23
        response = urllib2.urlopen(url_ohloh + str(page))
24
        xml = response.read()
25
        xmldoc = minidom.parseString(xml)
26
27
        items_available = xmlElements(xmldoc.getElementsByTagName('items_available')).firstValue()
28
        status = xmlElements(xmldoc.getElementsByTagName('status')).firstValue()
29
        project_list = []
30
        while (status == 'success' and len(project_list) < min(Configuration.max_number_of_records, items_available)):
31
            page = page + 1
32
            print "Ohloh search - " + name + " - page " + str(page)
33
            item_list = xmldoc.getElementsByTagName('project')
34
            for s in item_list :
35
                try:
36
                    op = OhlohProject()
37
                    op.id = xmlElements(s.getElementsByTagName('url_name')).firstValue()
38
                    op.name = xmlElements(s.getElementsByTagName('name')).firstValue()
39
                    op.description = xmlElements(s.getElementsByTagName('description')).firstValue()
40
                    op.small_logo_url = xmlElements(s.getElementsByTagName('small_logo_url')).firstValue()
41
                    op.homepage_url = xmlElements(s.getElementsByTagName('homepage_url')).firstValue()
42
                    op.id_forge = Forges.OHLOH
43
                    op.datasource_id = 0
44
                    project_list.append(op)
45
                    # s.getElementsByTagName('id')[0].localName
46
                    # id
47
                except Exception as ex:
48
                    print str(ex)
49
            response = urllib2.urlopen(url_ohloh + str(page))
50
            xml = response.read()
51
            xmldoc = minidom.parseString(xml)
52
            status = xmldoc.getElementsByTagName('status')[0].firstChild.nodeValue
53
        
54
        return project_list
55
56
    @staticmethod
57
    def getProjectInfo(project_identifier):
58
        url_ohloh = "http://www.ohloh.net/projects/" + project_identifier + ".xml?api_key=" + Configuration.api_key
59
        response = urllib2.urlopen(url_ohloh)
60
        xml = response.read()
61
        xmldoc = minidom.parseString(xml)
62
        info = {}
63
        
64
        status = xmlElements(xmldoc.getElementsByTagName('status')).firstValue()
65
        if status == 'success':
66
            info['id'] = project_identifier
67
            info['project_identifier'] = project_identifier
68
            info['widget_languages'] = '<script type="text/javascript" src="http://www.ohloh.net/p/' + project_identifier + '/widgets/project_languages.js"></script>'
69
            info['ohloh_url'] = "http://www.ohloh.net/p/" + project_identifier
70
            info['commits_spark'] = "http://www.ohloh.net/p/" + project_identifier + "/analyses/latest/commits_spark.png";
71
            info['created_at'] = xmlElements(xmldoc.getElementsByTagName('created_at')).firstValue()
72
            info['updated_at'] = xmlElements(xmldoc.getElementsByTagName('updated_at')).firstValue()
73
            info['description'] = xmlElements(xmldoc.getElementsByTagName('description')).firstValue()
74
            info['homepage_url'] = xmlElements(xmldoc.getElementsByTagName('homepage_url')).firstValue()
75
            info['download_url'] = xmlElements(xmldoc.getElementsByTagName('download_url')).firstValue()
76
            info['medium_logo_url'] = xmlElements(xmldoc.getElementsByTagName('medium_logo_url')).firstValue()
77
            info['small_logo_url'] = xmlElements(xmldoc.getElementsByTagName('small_logo_url')).firstValue()
78
            info['user_count'] = xmlElements(xmldoc.getElementsByTagName('user_count')).firstValue()
79
            info['average_rating'] = xmlElements(xmldoc.getElementsByTagName('average_rating')).firstValue()
80
            info['rating_count'] = xmlElements(xmldoc.getElementsByTagName('rating_count')).firstValue()
81
            info['review_count'] = xmlElements(xmldoc.getElementsByTagName('review_count')).firstValue()
82
            info['twelve_month_contributor_count'] = xmlElements(xmldoc.getElementsByTagName('twelve_month_contributor_count')).firstValue()
83
            info['total_code_lines'] = xmlElements(xmldoc.getElementsByTagName('total_code_lines')).firstValue()
84
            info['main_language_name'] = xmlElements(xmldoc.getElementsByTagName('main_language_name')).firstValue()
85
            info['licenses'] = []
86
            license_list = xmldoc.getElementsByTagName('license')
87
            for s in license_list :
88
                info['licenses'].append(xmlElements(s.getElementsByTagName('nice_name')).firstValue())
89
            info['languages'] = []
90
            language_list = xmldoc.getElementsByTagName('language')
91
            for l in language_list :
92
                language = {}
93
                language['project_identifier'] = l.firstChild.nodeValue
94
                language['percentage'] = l.attributes['percentage'].value
95
                language['color'] = l.attributes['color'].value
96
                language['id'] = l.attributes['id'].value
97
                info['languages'].append(language)
98
                
99
        url_ohloh = "http://www.ohloh.net/projects/" + project_identifier + "/factoids.xml?api_key=" + Configuration.api_key
100
        response = urllib2.urlopen(url_ohloh)
101
        xml = response.read()
102
        xmldoc = minidom.parseString(xml)
103
        
104
        status = xmlElements(xmldoc.getElementsByTagName('status')).firstValue()
105
        info['factoids'] = []
106
        if status == 'success':
107
            factoid_list = xmldoc.getElementsByTagName('factoid')
108
            for f in factoid_list :
109
                factoid = {}
110
                factoid['type'] = xmlElements(f.getElementsByTagName('type')).firstValue()
111
                factoid['description'] = xmlElements(f.getElementsByTagName('description')).firstValue()
112
                factoid['severity'] = xmlElements(f.getElementsByTagName('severity')).firstValue()
113
                info['factoids'].append(factoid)
114
115
116
        url_ohloh = "http://www.ohloh.net/projects/" + project_identifier + "/analyses/latest/size_facts.xml?api_key=" + Configuration.api_key
117
        response = urllib2.urlopen(url_ohloh)
118
        xml = response.read()
119
        xmldoc = minidom.parseString(xml)
120
        
121
        info['size_fact'] = None
122
        status = xmlElements(xmldoc.getElementsByTagName('status')).firstValue()
123
        if status == 'success':
124
            size_fact_list = xmldoc.getElementsByTagName('size_fact')
125
            if len(size_fact_list) > 0:
126
                sf = size_fact_list[len(size_fact_list) - 1]
127
                size_fact = {}
128
                size_fact['month'] = xmlElements(sf.getElementsByTagName('month')).firstValue()
129
                size_fact['code'] = xmlElements(sf.getElementsByTagName('code')).firstValue()
130
                size_fact['comments'] = xmlElements(sf.getElementsByTagName('comments')).firstValue()
131
                size_fact['blanks'] = xmlElements(sf.getElementsByTagName('comment_ratio')).firstValue()
132
                size_fact['commits'] = xmlElements(sf.getElementsByTagName('commits')).firstValue()
133
                size_fact['comment_ratio'] = xmlElements(sf.getElementsByTagName('comment_ratio')).firstValue()
134
                size_fact['man_months'] = xmlElements(sf.getElementsByTagName('man_months')).firstValue()
135
                # I just need the last as it is the most recent
136
                info['size_fact'] = size_fact 
137
138
        return info
139
140
        
141
class OhlohProject():
142
    id = 0
143
    name = ""
144
    description = ""
145
    small_logo_url = ""
146
    homepage_url = ""
147
    id_forge = 0
148
    datasource_id = 0
149
    def load_information(self):
150
        self.info = OhlohProxy.getProjectInfo(self.name)
151
152
153