Exemplo n.º 1
0
def get_feed(req, code):
    if req.method != 'GET':
        return HttpResponseNotAllowed(['GET'])

    if has_no_subscribers(req.META.get('HTTP_USER_AGENT', '')):
        logging.info('Feedfetcher and feed with no subscribers. Redirecting to empty feed.')
        return HttpResponseRedirect(req.build_absolute_uri('/feeds/feedfetcher-without-subscribers.xml'))

    try:
        q = decrypt(code)
        feed = q['feed']
        user = q['user']
        password = q['password']
        ljcut = True if q.get('ljcut') else False
    except (TypeError, CryptError, KeyError):
        # TypeError on b64decode failure
        return HttpResponseBadRequest()

    if is_livejournal(feed):
        return HttpResponseGone()

    headers = {}
    for header, value in req.META.items():
        if header.startswith('HTTP_'):
            header = header.replace('_', '-').split('-', 1)[1]
            if header.lower() not in BLOCKED_REQEST_HEADERS:
                headers[header.title()] = value

    try:
        fd = _urlopen_digested(feed, user, password, headers)
        headers = fd.headers
        content = fd.read()
        if ljcut:
            content = _ljcut_nonpublic(content, feed, user)
    except urllib2.HTTPError, e:
        # FIXME: what should happen if remote side returns permanent redirect?
        if e.code >= 400:
            logging.warning('HTTPError %i while fetching feed <%s> for %s' %
                            (e.code, feed, user))
        fd = e
        headers = fd.hdrs
        content = fd.read() if hasattr(fd, 'read') else ''

        if e.code == httplib.FORBIDDEN and 'livejournal.com/bots' in content:
            return HttpResponseRedirect(req.build_absolute_uri('/feeds/livejournal-403-bots.xml'))
Exemplo n.º 2
0
def _lj_gen_opml(req):
    credentials = decrypt(req.POST['credentials'])
    friends = req.POST['friends'].split()
    communities = req.POST['communities'].split()

    ljuser = credentials['ljuser']
    password = credentials['password']

    outlines = lj.get_opml(ljuser)
    outlines['users'].update(outlines['communities'])
    outlines = outlines['users']

    ctx = {'ljuser': ljuser}
    feeds = []
    for who in friends + communities:
        o = outlines[who]
        if req.POST.get('read_' + who):
            param = {'feed': o.xmlURL + '?auth=digest',
                     'user': ljuser,
                     'password': password}
            if req.POST.get('cut_' + who):
                param['ljcut'] = 'body'
            xmlURL = req.build_absolute_uri('/feed/' + encrypt(param))
        else:
            xmlURL = outlines[who].xmlURL

        if o.text.lower() != who.lower():
            text = "%s (%s)" % (o.text, who)
        else:
            text = o.text

        feeds.append({'xmlURL':  xmlURL,
                      'htmlURL': o.htmlURL,
                      'text':    text})
    ctx['feeds'] = feeds
    content = render_to_string('lj_opml.xml', ctx)
    resp = HttpResponse(content_type='text/xml', content=content)
    resp['Content-Disposition'] = 'attachment; filename="livejournal.opml"'
    return resp