Switch to unified view

a/OSSEval/OSSEval/utils.py b/OSSEval/OSSEval/utils.py
1
from json import JSONEncoder
1
from json import JSONEncoder
2
from lxml import html
3
import urllib2
4
from apiclient.discovery import build
2
5
3
class TrivialJSONEncoder(JSONEncoder):
6
class TrivialJSONEncoder(JSONEncoder):
4
    def default(self, o):
7
    def default(self, o):
5
        return o.__dict__
8
        return o.__dict__
6
9
...
...
27
        try:
30
        try:
28
            return int(xmldoc.attributes[tag].firstChild.data)
31
            return int(xmldoc.attributes[tag].firstChild.data)
29
        except:
32
        except:
30
            return -1
33
            return -1
31
        
34
        
35
class SearchEngine():
36
    @staticmethod
37
    def search__engine_name():
38
        return "Google"
39
    
40
    @staticmethod
41
    def search_url(search_text, site_search=""):
42
        if site_search == "":
43
            return "https://www.google.com/?#q=" + search_text
44
        else:
45
            return "https://www.google.com/?#q=" + search_text + "+site%3A" + site_search
46
47
    @staticmethod
48
    def readable_query(search_text, site_search=""):
49
        return search_text.replace("%3A", ":").replace("%20", " ").replace("%2B", "+") + ("" if site_search=="" else " site:" + site_search)
50
51
    @staticmethod
52
    def search_count(search_text, site_search=""):
53
        '''
54
        https://developers.google.com/custom-search/json-api/v1/reference/cse/list
55
        exactTerms        string     Identifies a phrase that all documents in the search results must contain.
56
        excludeTerms      string     Identifies a word or phrase that should not appear in any documents in the search results.
57
        siteSearch        string     Specifies all search results should be pages from a given site.
58
        siteSearchFilter  string     Controls whether to include or exclude results from the site named in the siteSearch parameter.
59
        Acceptable values are:            "e": exclude            "i": include
60
        '''
61
        service = build("customsearch", "v1", developerKey="AIzaSyCAova46cAkHga_SZWTBqROdjoz1KcTlw8")
62
        print search_text + " " + site_search
63
        if site_search == "":
64
            res = service.cse().list(q=search_text, cx='017576662512468239146:omuauf_lfve',).execute()
65
        else:
66
            res = service.cse().list(q=search_text, siteSearch=site_search, cx='017576662512468239146:omuauf_lfve',).execute()
67
        return int(res['searchInformation']['totalResults'])