Ticket #5768: google-5678.patch

File google-5678.patch, 5.5 KB (added by thijs, 4 years ago)
  • twisted/web/test/test_web.py

     
    587587
    588588
    589589
    590 
    591 from twisted.web import google
    592 class GoogleTestCase(unittest.TestCase):
    593     def testCheckGoogle(self):
    594         raise unittest.SkipTest("no violation of google ToS")
    595         d = google.checkGoogle('site:www.twistedmatrix.com twisted')
    596         d.addCallback(self.assertEqual, 'http://twistedmatrix.com/')
    597         return d
    598 
    599 
    600     def test_deprecated(self):
    601         """
    602         Google module is deprecated since Twisted 11.1.0
    603         """
    604         from twisted.web import google
    605         warnings = self.flushWarnings(offendingFunctions=[self.test_deprecated])
    606         self.assertEqual(len(warnings), 1)
    607         self.assertEqual(warnings[0]['category'], DeprecationWarning)
    608 
    609 
    610 
    611590class RequestTests(unittest.TestCase):
    612591    """
    613592    Tests for the HTTP request class, L{server.Request}.
  • twisted/web/topfiles/5678.removal

     
     1twisted.web.google, deprecated since Twisted 11.1, is removed now.
  • twisted/web/__init__.py

     
    1010"""
    1111
    1212from twisted.web._version import version
    13 from twisted.python.versions import Version
    14 from twisted.python.deprecate import deprecatedModuleAttribute
    1513
    1614__version__ = version.short()
    1715
    18 deprecatedModuleAttribute(
    19     Version('Twisted', 11, 1, 0),
    20     "Google module is deprecated. Use Google's API instead",
    21     __name__, "google")
  • twisted/web/google.py

     
    1 # Copyright (c) Twisted Matrix Laboratories.
    2 # See LICENSE for details.
    3 
    4 #
    5 """\"I'm Feeling Lucky\" with U{Google<http://google.com>}.
    6 """
    7 import urllib
    8 from twisted.internet import protocol, reactor, defer
    9 from twisted.web import http
    10 
    11 class GoogleChecker(http.HTTPClient):
    12 
    13     def connectionMade(self):
    14         self.sendCommand('GET', self.factory.url)
    15         self.sendHeader('Host', self.factory.host)
    16         self.sendHeader('User-Agent', self.factory.agent)
    17         self.endHeaders()
    18 
    19     def handleHeader(self, key, value):
    20         key = key.lower()
    21         if key == 'location':
    22             self.factory.gotLocation(value)
    23 
    24     def handleStatus(self, version, status, message):
    25         if status != '302':
    26             self.factory.noLocation(ValueError("bad status"))
    27 
    28     def handleEndHeaders(self):
    29         self.factory.noLocation(ValueError("no location"))
    30 
    31     def handleResponsePart(self, part):
    32         pass
    33 
    34     def handleResponseEnd(self):
    35         pass
    36 
    37     def connectionLost(self, reason):
    38         self.factory.noLocation(reason)
    39 
    40 
    41 class GoogleCheckerFactory(protocol.ClientFactory):
    42 
    43     protocol = GoogleChecker
    44 
    45     def __init__(self, words):
    46         self.url = ('/search?q=%s&btnI=%s' %
    47                     (urllib.quote_plus(' '.join(words)),
    48                      urllib.quote_plus("I'm Feeling Lucky")))
    49         self.agent="Twisted/GoogleChecker"
    50         self.host = "www.google.com"
    51         self.deferred = defer.Deferred()
    52 
    53     def clientConnectionFailed(self, _, reason):
    54         self.noLocation(reason)
    55 
    56     def gotLocation(self, location):
    57         if self.deferred:
    58             self.deferred.callback(location)
    59             self.deferred = None
    60 
    61     def noLocation(self, error):
    62         if self.deferred:
    63             self.deferred.errback(error)
    64             self.deferred = None
    65 
    66 
    67 def checkGoogle(words):
    68     """Check google for a match.
    69 
    70     @returns: a Deferred which will callback with a URL or errback with a
    71         Failure.
    72     """
    73     factory = GoogleCheckerFactory(words)
    74     reactor.connectTCP('www.google.com', 80, factory)
    75     return factory.deferred
  • doc/web/examples/index.xhtml

     
    8585        distributed web setup with a master and slave using
    8686        <code>twisted.web.distrib</code> and
    8787        <code>twisted.spread.pb</code></li>
    88         <li><a href="google.py">google.py</a> - use
    89         <code>twisted.web.google</code> to get the I'm Feeling Lucky
    90         page for a search term</li>
    9188        <li><a href="soap.py">soap.py</a> - use
    9289        <code>twisted.web.soap</code> to publish SOAP methods</li>
    9390    </ul>
  • doc/web/examples/google.py

     
    1 # Copyright (c) Twisted Matrix Laboratories
    2 # See LICENSE for details.
    3 
    4 """
    5 This program will print out the URL corresponding to the first webpage given by
    6 a Google search.
    7 
    8 Usage:
    9     $ python google.py <keyword(s)>
    10 """
    11 
    12 import sys
    13 
    14 from twisted.web.google import checkGoogle
    15 from twisted.python.util import println
    16 from twisted.internet import reactor
    17 
    18 checkGoogle(sys.argv[1:]).addCallbacks(
    19  lambda l:(println(l),reactor.stop()),
    20  lambda e:(println('error:',e),reactor.stop()))
    21 reactor.run()