Example #1
0
def unsubscribe(id, verbose=False):
    try:
        db = Pshb.objects.get(id=id)
    except Pshb.DoesNotExist:
        return {'rc': 1}

    topic = __get_absolute_url(urlresolvers.reverse('index')) + '?format=atom'
    callback = __get_absolute_url(urlresolvers.reverse('pshb', args=[db.hash]))

    if settings.PSHB_HTTPS_CALLBACK:
        callback = callback.replace('http://', 'https://')

    data = {
        'hub.mode': 'unsubscribe',
        'hub.topic': topic,
        'hub.callback': callback,
        'hub.verify': 'sync'
    }

    try:
        r = httpclient.get(db.hub, data=data)
        if verbose:
            print('Response code: %d' % r.status_code)
        return {'hub': db.hub, 'rc': r.status_code}
    except (IOError, httpclient.HTTPError) as e:
        error = ''
        if hasattr(e, 'message'):
            error = e.message
        elif hasattr(e, 'read'):
            error = e.read()
        if verbose:
            print('%s, Response: "%s"' % (e, error))
        return {'hub': db.hub, 'rc': error}
Example #2
0
def publish(hubs=None, verbose=False):
    hubs = hubs or settings.PSHB_HUBS
    url = __get_absolute_url(urlresolvers.reverse('index')) + '?format=atom'
    if 'localhost' in url:
        return
    for hub in hubs:
        hub = hub.replace('https://', 'http://')  # it's just a ping.
        data = {'hub.mode': 'publish', 'hub.url': url}
        try:
            r = httpclient.get(hub, data=data, timeout=7)
            if verbose:
                if r.status_code == 204:
                    print('%s: Successfully pinged.' % hub)
                else:
                    print('%s: Pinged and got %d.' % (hub, r.status_code))
        except (IOError, httpclient.HTTPError) as e:
            if hasattr(e, 'status_code') and e.status_code == 204:
                continue
            if verbose:
                error = ''
                if hasattr(e, 'message'):
                    error = e.message
                elif hasattr(e, 'read'):
                    error = e.read()
                print('%s, Response: "%s"' % (e, error))
Example #3
0
def publish(hubs=None, verbose=False):
    hubs = hubs or settings.PSHB_HUBS
    url = __get_absolute_url(urlresolvers.reverse('index')) + '?format=atom'
    if 'localhost' in url:
        return
    for hub in hubs:
        hub = hub.replace('https://', 'http://')  # it's just a ping.
        data = {'hub.mode': 'publish', 'hub.url': url}
        try:
            r = httpclient.get(hub, data=data, timeout=7)
            if verbose:
                if r.status_code == 204:
                    print('%s: Successfully pinged.' % hub)
                else:
                    print('%s: Pinged and got %d.' % (hub, r.status_code))
        except (IOError, httpclient.HTTPError) as e:
            if hasattr(e, 'status_code') and e.status_code == 204:
                continue
            if verbose:
                error = ''
                if hasattr(e, 'message'):
                    error = e.message
                elif hasattr(e, 'read'):
                    error = e.read()
                print('%s, Response: "%s"' % (e, error))
Example #4
0
def unsubscribe(id, verbose=False):
    try:
        db = Pshb.objects.get(id=id)
    except Pshb.DoesNotExist:
        return {'rc': 1}

    topic = __get_absolute_url(
        urlresolvers.reverse('index')) + '?format=atom'
    callback = __get_absolute_url(
        urlresolvers.reverse('pshb', args=[db.hash]))

    if settings.PSHB_HTTPS_CALLBACK:
        callback = callback.replace('http://', 'https://')

    data = {'hub.mode': 'unsubscribe',
            'hub.topic': topic,
            'hub.callback': callback,
            'hub.verify': 'sync'}

    try:
        r = httpclient.get(db.hub, data=data)
        if verbose:
            print('Response code: %d' % r.status_code)
        return {'hub': db.hub, 'rc': r.status_code}
    except (IOError, httpclient.HTTPError) as e:
        error = ''
        if hasattr(e, 'message'):
            error = e.message
        elif hasattr(e, 'read'):
            error = e.read()
        if verbose:
            print('%s, Response: "%s"' % (e, error))
        return {'hub': db.hub, 'rc': error}
Example #5
0
    def fetch(self, url):
        self.fp_error = False
        if not self.payload:
            try:
                hs = httpclient.gen_auth(self.service, url)
                r = httpclient.get(url, auth=hs)
                alturl = httpclient.get_alturl_if_html(r)
                if alturl:
                    r = httpclient.get(alturl, auth=hs)
                self.fp = feedparser.parse(r.text)
                self.fp.etag = r.headers.get('etag')
                self.fp.modified = r.headers.get('last-modified')
            except (IOError, httpclient.HTTPError) as e:
                self.fp_error = True
                if self.verbose:
                    error = e.message if hasattr(e, 'message') else ''
                    print('%s (%d) HTTPError: %s' % (self.service.api,
                                                     self.service.id,
                                                     error))
                return
        else:
            self.fp = feedparser.parse(self.payload)

        if hasattr(self.fp, 'bozo') and self.fp.bozo:
            self.fp_error = True
            if isinstance(self.fp.bozo_exception,
                          feedparser.CharacterEncodingOverride):
                self.fp_error = False
            if self.verbose:
                print('%s (%d) Bozo: %s' % (self.service.api,
                                            self.service.id, self.fp))

        if not self.fp_error:
            self.service.etag = self.fp.get('etag', '')
            if self.service.etag is None:
                self.service.etag = ''
            try:
                self.service.last_modified = mtime(self.fp.modified)
            except:
                pass
            self.service.last_checked = now()
            if not self.service.link:
                self.service.link = self.fp.feed.get('link', '')
            self.service.save()
            if not self.fetch_only:
                self.process()
Example #6
0
    def fetch(self, url):
        self.fp_error = False
        if not self.payload:
            try:
                hs = httpclient.gen_auth(self.service, url)
                r = httpclient.get(url, auth=hs)
                alturl = httpclient.get_alturl_if_html(r)
                if alturl:
                    r = httpclient.get(alturl, auth=hs)
                self.fp = feedparser.parse(r.text)
                self.fp.etag = r.etag
                self.fp.modified = r.modified
            except (IOError, httpclient.HTTPError) as e:
                self.fp_error = True
                if self.verbose:
                    error = e.message if hasattr(e, 'message') else ''
                    print('%s (%d) HTTPError: %s' %
                          (self.service.api, self.service.id, error))
                return
        else:
            self.fp = feedparser.parse(self.payload)

        if hasattr(self.fp, 'bozo') and self.fp.bozo:
            self.fp_error = True
            if isinstance(self.fp.bozo_exception,
                          feedparser.CharacterEncodingOverride):
                self.fp_error = False
            if self.verbose:
                print('%s (%d) Bozo: %s' %
                      (self.service.api, self.service.id, self.fp))

        if not self.fp_error:
            self.service.etag = self.fp.get('etag', '')
            if self.service.etag is None:
                self.service.etag = ''
            try:
                self.service.last_modified = mtime(self.fp.modified)
            except:
                pass
            self.service.last_checked = now()
            if not self.service.link:
                self.service.link = self.fp.feed.get('link', '')
            self.service.save()
            if not self.fetch_only:
                self.process()
Example #7
0
def get_thumbnail_url(id):
    try:
        r = httpclient.get('https://vimeo.com/api/v2/video/%s.json' % id)
        if r.status_code == 200:
            jsn = r.json()
            if 'thumbnail_medium' in jsn[0]:
                return jsn[0]['thumbnail_medium']
    except:
        pass
    return None
Example #8
0
def get_thumbnail_url(id):
    try:
        r = httpclient.get('https://vimeo.com/api/v2/video/%s.json' % id)
        if r.status_code == 200:
            jsn = r.json()
            if 'thumbnail_medium' in jsn[0]:
                return jsn[0]['thumbnail_medium']
    except:
        pass
    return None
Example #9
0
def discover(url, provider, maxwidth=None, maxheight=None):
    pro = providers.get(provider, None)
    if not pro:
        return None
    q = "?url=%s&format=json" % urllib.quote(url)
    if maxwidth:
        q += "&maxwidth=%d" % maxwidth
    if maxheight:
        q += "&maxheight=%d" % maxheight
    try:
        r = httpclient.get(pro + q, timeout=15)
        if r.status_code == 200:
            return r.json()
    except:
        pass
    return None
Example #10
0
def discover(url, provider, maxwidth=None, maxheight=None):
    pro = providers.get(provider, None)
    if not pro:
        return None
    q = '?url=%s&format=json' % urllib.quote(url)
    if maxwidth:
        q += '&maxwidth=%d' % maxwidth
    if maxheight:
        q += '&maxheight=%d' % maxheight
    try:
        r = httpclient.get(pro + q, timeout=15)
        if r.status_code == 200:
            return r.json()
    except:
        pass
    return None
Example #11
0
 def fetch(self, url):
     try:
         r = httpclient.get('https://vimeo.com' + url)
         if r.status_code == 200:
             self.json = r.json()
             self.service.last_checked = now()
             self.service.save()
             self.process()
         elif self.verbose:
             print('%s (%d) HTTP: %s' % (self.service.api,
                                         self.service.id, r.reason))
     except Exception as e:
         if self.verbose:
             import sys
             import traceback
             print('%s (%d) Exception: %s' % (self.service.api,
                                              self.service.id, e))
             traceback.print_exc(file=sys.stdout)
Example #12
0
 def fetch(self, url):
     try:
         r = httpclient.get('https://vimeo.com' + url)
         if r.status_code == 200:
             self.json = r.json()
             self.service.last_checked = now()
             self.service.save()
             self.process()
         elif self.verbose:
             print('%s (%d) HTTP: %s' %
                   (self.service.api, self.service.id, r.reason))
     except Exception as e:
         if self.verbose:
             import sys
             import traceback
             print('%s (%d) Exception: %s' %
                   (self.service.api, self.service.id, e))
             traceback.print_exc(file=sys.stdout)
Example #13
0
def subscribe(service, verbose=False):
    try:
        webfeed = __import__('glifestream.apis.webfeed', {}, {}, ['API'])
    except ImportError:
        return {'rc': 1, 'error': 'ImportError apis.webfeed'}
    webfeed_api = getattr(webfeed, 'API')

    try:
        mod = __import__('glifestream.apis.%s' % service.api, {}, {}, ['API'])
    except ImportError:
        return {'rc': 1, 'error': 'ImportError apis.%s' % service.api}
    mod_api = getattr(mod, 'API')
    api = mod_api(service, False, False)

    if not isinstance(api, webfeed_api):
        return {'rc': 1, 'error': 'PSHB is not supported by this API.'}

    api.fetch_only = True
    api.run()
    if api.fp_error:
        return {'rc': 1, 'error': api.fp.bozo_exception}

    hub = None
    for link in api.fp.feed.links:
        if link.rel == 'hub':
            hub = link.href
            break
    if not hub:
        return {'rc': 2}

    secret = hashlib.md5(
        '%s:%d/%s/%s' %
        (hub, service.id, api.url, settings.SECRET_KEY)).hexdigest()
    hash = hashlib.sha1(secret).hexdigest()[0:20]
    secret = secret[0:8] if 'https://' in hub else None

    save_db = False
    try:
        db = Pshb.objects.get(hash=hash, service=service)
    except Pshb.DoesNotExist:
        db = Pshb(hash=hash, service=service, hub=hub, secret=secret)
        save_db = True

    topic = __get_absolute_url(urlresolvers.reverse('index')) + '?format=atom'
    callback = __get_absolute_url(urlresolvers.reverse('pshb', args=[hash]))

    if settings.PSHB_HTTPS_CALLBACK:
        callback = callback.replace('http://', 'https://')

    data = {
        'hub.mode': 'subscribe',
        'hub.topic': topic,
        'hub.callback': callback,
        'hub.verify': 'async'
    }
    if secret:
        data['hub.secret'] = secret

    try:
        r = httpclient.get(hub, data=data)
        if verbose:
            print('Response code: %d' % r.status_code)
        if save_db:
            db.save()
        return {'hub': hub, 'rc': r.status_code}
    except (IOError, httpclient.HTTPError) as e:
        error = ''
        if hasattr(e, 'message'):
            error = e.message
        elif hasattr(e, 'read'):
            error = e.read()
        if verbose:
            print('%s, Response: "%s"' % (e, error))
        return {'hub': hub, 'rc': error}
Example #14
0
def subscribe(service, verbose=False):
    try:
        webfeed = __import__('glifestream.apis.webfeed', {}, {}, ['API'])
    except ImportError:
        return {'rc': 1, 'error': 'ImportError apis.webfeed'}
    webfeed_api = getattr(webfeed, 'API')

    try:
        mod = __import__('glifestream.apis.%s' % service.api, {}, {}, ['API'])
    except ImportError:
        return {'rc': 1, 'error': 'ImportError apis.%s' % service.api}
    mod_api = getattr(mod, 'API')
    api = mod_api(service, False, False)

    if not isinstance(api, webfeed_api):
        return {'rc': 1, 'error': 'PSHB is not supported by this API.'}

    api.fetch_only = True
    api.run()
    if api.fp_error:
        return {'rc': 1, 'error': api.fp.bozo_exception}

    hub = None
    for link in api.fp.feed.links:
        if link.rel == 'hub':
            hub = link.href
            break
    if not hub:
        return {'rc': 2}

    secret = hashlib.md5('%s:%d/%s/%s' % (hub, service.id, api.url,
                                          settings.SECRET_KEY)).hexdigest()
    hash = hashlib.sha1(secret).hexdigest()[0:20]
    secret = secret[0:8] if 'https://' in hub else None

    save_db = False
    try:
        db = Pshb.objects.get(hash=hash, service=service)
    except Pshb.DoesNotExist:
        db = Pshb(hash=hash, service=service, hub=hub, secret=secret)
        save_db = True

    topic = __get_absolute_url(
        urlresolvers.reverse('index')) + '?format=atom'
    callback = __get_absolute_url(urlresolvers.reverse('pshb', args=[hash]))

    if settings.PSHB_HTTPS_CALLBACK:
        callback = callback.replace('http://', 'https://')

    data = {'hub.mode': 'subscribe',
            'hub.topic': topic,
            'hub.callback': callback,
            'hub.verify': 'async'}
    if secret:
        data['hub.secret'] = secret

    try:
        r = httpclient.get(hub, data=data)
        if verbose:
            print('Response code: %d' % r.status_code)
        if save_db:
            db.save()
        return {'hub': hub, 'rc': r.status_code}
    except (IOError, httpclient.HTTPError) as e:
        error = ''
        if hasattr(e, 'message'):
            error = e.message
        elif hasattr(e, 'read'):
            error = e.read()
        if verbose:
            print('%s, Response: "%s"' % (e, error))
        return {'hub': hub, 'rc': error}