Changeset 221


Ignore:
Timestamp:
19-05-07 00:04:20 (6 years ago)
Author:
thomas
Message:
  • moap/command/doap.py (Search, Search.addOptions, Search.handleOptions, Search.do, Search.foundURL, Doap): Implement moap doap search using either google or yahoo to do a search for your project's page rank based on your keyword query.
  • moap/util/deps.py (genshi, genshi.fedora_install, google, google.fedora_install, yahoo, yahoo.fedora_install, handleImportError): Add deps for google and yahoo. Fix genshi dep.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ChangeLog

    r220 r221  
     12007-05-19  Thomas Vander Stichele  <thomas at apestaart dot org> 
     2 
     3        * moap/command/doap.py (Search, Search.addOptions, 
     4          Search.handleOptions, Search.do, Search.foundURL, Doap): 
     5          Implement moap doap search using either google or yahoo 
     6          to do a search for your project's page rank based on your 
     7          keyword query. 
     8        * moap/util/deps.py (genshi, genshi.fedora_install, google, 
     9          google.fedora_install, yahoo, yahoo.fedora_install, 
     10          handleImportError): 
     11          Add deps for google and yahoo.  Fix genshi dep. 
     12 
    1132007-05-16  Thomas Vander Stichele  <thomas at apestaart dot org> 
    214 
  • trunk/moap/command/doap.py

    r208 r221  
    125125                self.stderr.write("ERROR: %r\n" % e) 
    126126 
     127class Search(util.LogCommand): 
     128    description = "look up rank of project's home page based on keywords" 
     129 
     130    _engines = ["google", "yahoo"] 
     131    _default = "yahoo" 
     132 
     133    def addOptions(self): 
     134        self.parser.add_option('-e', '--engine', 
     135            action="store", dest="engine", default=self._default, 
     136            help="search engine to use (out of %s; defaults to %s)" % ( 
     137                ", ".join(self._engines), self._default)) 
     138        self.parser.add_option('-l', '--limit', 
     139            action="store", dest="limit", default="100", 
     140            help="maximum number of results to look at") 
     141 
     142    def handleOptions(self, options): 
     143        self._limit = int(options.limit) 
     144        self._engine = options.engine 
     145 
     146    def do(self, args): 
     147        if not args: 
     148            self.stderr.write('Please provide a search query.\n') 
     149            return 3 
     150 
     151        d = self.parentCommand.doap 
     152        project = d.getProject() 
     153 
     154        rank = 0 
     155        found = False 
     156        query = " ".join(args) 
     157 
     158        def foundURL(target, url): 
     159            # returns true if the url is close enough to the target 
     160            if target.endswith('/'): 
     161                target = target[:-1] 
     162            if url.endswith('/'): 
     163                url = url[:-1] 
     164            return url == target 
     165 
     166        if self._engine == 'google': 
     167            from pygoogle import google 
     168 
     169            while not found: 
     170                self.debug('Doing Google search for %s starting from %d' % ( 
     171                    " ".join(args), rank)) 
     172                value = google.doGoogleSearch(" ".join(args), start=rank) 
     173                for result in value.results: 
     174                    rank += 1 
     175                    self.debug('Hit %d: URL %s' % (rank, result.URL)) 
     176                    if foundURL(project.homepage, result.URL): 
     177                        found = True 
     178                        break 
     179 
     180                if rank >= self._limit: 
     181                    break 
     182 
     183        elif self._engine == 'yahoo': 
     184            from yahoo.search import web 
     185 
     186            # yahoo's start is 1-based 
     187            while not found: 
     188                search = web.WebSearch('moapmoap', query=query, start=rank+1) 
     189                info = search.parse_results() 
     190                for result in info.results: 
     191                    rank += 1 
     192                    self.debug('Hit %d: URL %s' % (rank, result['Url'])) 
     193                    if foundURL(project.homepage, result['Url']): 
     194                        found = True 
     195                        break 
     196 
     197                if rank >= self._limit: 
     198                    break 
     199 
     200        else: 
     201            self.stderr.write("Unknown search engine '%s'.\n" % self._engine) 
     202            self.stderr.write("Please choose from %s.\n" % 
     203                ", ".join(self._engines)) 
     204            return 3 
     205 
     206        if found: 
     207            self.stdout.write("Found homepage as hit %d\n." % rank) 
     208        else: 
     209            self.stdout.write("Did not find homepage in first %d hits.\n" % 
     210                self._limit) 
     211 
    127212class Ical(util.LogCommand): 
    128213    description = "Output iCal stream from project releases" 
     
    366451    usage = "doap [doap-options] %command" 
    367452    description = "read and act on DOAP file" 
    368     subCommandClasses = [Freshmeat, Ical, Mail, Rss, Show, bug.Bug] 
     453    subCommandClasses = [Freshmeat, Ical, Mail, Rss, Search, Show, bug.Bug] 
    369454 
    370455    doap = None 
  • trunk/moap/util/deps.py

    r204 r221  
    4040        return "Your version of Fedora does not have python-redland available." 
    4141 
    42  
    4342class genshi(Dependency): 
    44     module = 'genshii' 
     43    module = 'genshi' 
    4544    name = "Genshi templating language" 
    4645    homepage = "http://genshi.edgewall.com/" 
     
    4847    def fedora_install(self, distro): 
    4948        return "genshi is not yet available in Fedora Extras.\n" 
     49 
     50class google(Dependency): 
     51    module = 'google' 
     52    name = "A Python Interface to the Google API" 
     53    homepage = "http://pygoogle.sourceforge.net/" 
     54 
     55    def fedora_install(self, distro): 
     56        return "pygoogle is not yet available in Fedora Extras.\n" 
     57 
     58class yahoo(Dependency): 
     59    module = 'yahoo' 
     60    name = "A Python Interface to the Yahoo Web API" 
     61    homepage = "http://developer.yahoo.com/python/" 
     62 
     63    def fedora_install(self, distro): 
     64        return "python-yahoo is not yet available in Fedora Extras.\n" 
    5065 
    5166def handleImportError(exception): 
     
    6075    module = module.split('.')[0] 
    6176    deps = {} 
    62     for dep in [RDF(), genshi()]: 
     77    for dep in [RDF(), genshi(), google()]: 
    6378        deps[dep.module] = dep 
    6479 
     
    92107        sys.stderr.write('import %s\n' % module) 
    93108 
    94  
    95109        return 
    96110 
     111    # re-raise if we didn't have it 
    97112    raise 
Note: See TracChangeset for help on using the changeset viewer.