| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 # | |
| 5 from twisted.web import client | |
| 6 from twisted.internet import reactor | |
| 7 import md5 | |
| 8 from zope.interface import implements | |
| 9 | |
| 10 class IChangeNotified: | |
| 11 pass | |
| 12 | |
| 13 class BaseChangeNotified: | |
| 14 | |
| 15 implements(IChangeNotified) | |
| 16 | |
| 17 def reportChange(self, old, new): | |
| 18 pass | |
| 19 | |
| 20 def reportNoChange(self): | |
| 21 pass | |
| 22 | |
| 23 class ChangeChecker: | |
| 24 | |
| 25 working = 0 | |
| 26 call = None | |
| 27 | |
| 28 def __init__(self, notified, url, delay=60): | |
| 29 self.notified = notified | |
| 30 self.url = url | |
| 31 self.md5 = None | |
| 32 self.delay = delay | |
| 33 | |
| 34 def start(self): | |
| 35 self.working = 1 | |
| 36 self._getPage() | |
| 37 | |
| 38 def stop(self): | |
| 39 if self.call: | |
| 40 self.call.cancel() | |
| 41 self.call = None | |
| 42 self.working = 0 | |
| 43 | |
| 44 def _getPage(self): | |
| 45 d = client.getPage(self.url) | |
| 46 d.addErrback(self.noPage) | |
| 47 d.addCallback(self.page) | |
| 48 self.call = None | |
| 49 | |
| 50 def noPage(self, e): | |
| 51 self.gotMD5(None) | |
| 52 | |
| 53 def page(self, p): | |
| 54 if p is None: | |
| 55 return self.gotMD5(None) | |
| 56 m = md5.new() | |
| 57 m.update(p) | |
| 58 self.gotMD5(m.digest()) | |
| 59 | |
| 60 def gotMD5(self, md5): | |
| 61 if not self.working: | |
| 62 return | |
| 63 if md5 != self.md5: | |
| 64 self.notified.reportChange(self.md5, md5) | |
| 65 self.md5 = md5 | |
| 66 else: | |
| 67 self.notified.reportNoChange() | |
| 68 if not self.call: | |
| 69 self.call = reactor.callLater(self.delay, self._getPage) | |
| 70 | |
| 71 | |
| 72 class ProxyChangeChecker(ChangeChecker): | |
| 73 | |
| 74 def __init__(self, proxyHost, proxyPort, notified, url, delay=60): | |
| 75 self.proxyHost = proxyHost | |
| 76 self.proxyPort = proxyPort | |
| 77 ChangeChecker.__init__(self, notified, url, delay) | |
| 78 | |
| 79 def _getPage(self): | |
| 80 factory = client.HTTPClientFactory(self.proxyHost, self.url) | |
| 81 factory.headers = {'pragma': 'no-cache'} | |
| 82 reactor.connectTCP(self.proxyHost, self.proxyPort, factory) | |
| 83 d = factory.deferred | |
| 84 d.addErrback(self.noPage) | |
| 85 d.addCallback(self.page) | |
| OLD | NEW |