Exemple #1
0
    def __init__(self, rids, db, rewrite=False):
        self.__rids = rids
        self.__iter = 0
        self.__now = int(time.time())
        self.__templ = app.jinja_env.get_template('feed.html')

        if db == None:
            self.__db = RSS_Resource_db()
        else:
            self.__db = db

        self.__rewriter = get_rewriter(rewrite)
Exemple #2
0
class ResourceIterator:
    def __init__(self, rids, db):
        self.__rids = rids
        self.__iter = 0
        self.__now = int(time.time())
        self.__templ = app.jinja_env.get_template('feed.html')

        if db == None:
            self.__db = RSS_Resource_db()
        else:
            self.__db = db

    def __iter__(self):
        return self

    def next(self):
        while True:
            if self.__iter >= len(self.__rids):
                raise StopIteration()
            else:
                rid = self.__rids[self.__iter]
                self.__iter += 1

                try:
                    url = RSS_Resource_id2url(rid, self.__db.cursor())
                    rid, response = feed(url, self.__db, self.__templ)
                    self.__rids[self.__iter - 1] = rid
                    return '<span style="display: block; overflow: hidden;" class="rssfeed" id="feed-%s">%s</span>' % (format_rid(rid), response)
                except KeyError:
                    self.__rids[self.__iter - 1] = None
Exemple #3
0
class ResourceIterator:
    def __init__(self, rids, db, rewrite=False):
        self.__rids = rids
        self.__iter = 0
        self.__now = int(time.time())
        self.__templ = app.jinja_env.get_template('feed.html')

        if db == None:
            self.__db = RSS_Resource_db()
        else:
            self.__db = db

        self.__rewriter = get_rewriter(rewrite)

    def __iter__(self):
        return self

    def next(self):
        while True:
            if self.__iter >= len(self.__rids):
                raise StopIteration()
            else:
                rid = self.__rids[self.__iter]
                self.__iter += 1

                try:
                    url = RSS_Resource_id2url(rid, self.__db.cursor())
                    rid, response = feed(url, self.__rewriter, self.__db,
                                         self.__templ)
                    self.__rids[self.__iter - 1] = rid
                    return '<span style="display: block; overflow: hidden;" class="rssfeed" id="feed-%s">%s</span>' % (format_rid(rid), response)
                except KeyError:
                    self.__rids[self.__iter - 1] = None
Exemple #4
0
    def __init__(self, rids, db):
        self.__rids = rids
        self.__iter = 0
        self.__now = int(time.time())
        self.__templ = app.jinja_env.get_template('feed.html')

        if db == None:
            self.__db = RSS_Resource_db()
        else:
            self.__db = db
Exemple #5
0
def opml(ids=''):
    db = RSS_Resource_db()
    if ids:
        rids = map(parse_rid, ids.split(','))
    else:
        rids = []
    items = []

    for rid in rids:
        try:
            url = RSS_Resource_id2url(rid, db.cursor())
            while url != None:
                resource = RSS_Resource(url, db, generate_id=generate_id)
                url, seq = resource.redirect_info(db)

            items.append(resource)
        except KeyError:
            pass

    response = current_app.response_class(render_template('subscriptions.xml', items=items), mimetype='application/xml')
    return response
Exemple #6
0
def opml(ids=''):
    db = RSS_Resource_db()
    if ids:
        rids = map(parse_rid, ids.split(','))
    else:
        rids = []
    items = []

    for rid in rids:
        try:
            url = RSS_Resource_id2url(rid, db.cursor())
            while url != None:
                resource = RSS_Resource(url, db, generate_id=generate_id)
                url, seq = resource.redirect_info(db)

            items.append(resource)
        except KeyError:
            pass

    response = current_app.response_class(render_template('subscriptions.xml', items=items), mimetype='application/xml')
    return response
Exemple #7
0
def feed(url, rewriter, db=None, templ=app.jinja_env.get_template('feed.html')):
    now = int(time.time())
    if db == None:
        db = RSS_Resource_db()

    while url != None:
        resource = RSS_Resource(url, db, generate_id=generate_id)
        url, seq = resource.redirect_info(db)

    next_update = resource.next_update(False)

    if next_update <= now:
        new_items, next_item_id, redirect_resource, redirect_seq, redirects = resource.update()

        if redirect_resource != None:
            resource = redirect_resource

    channel_info = resource.channel_info()
    channel_title = channel_info.title
    if not channel_title.strip():
        channel_title = channel_info.link
    if not channel_title.strip():
        channel_title = resource.url()

    last_updated, last_modified, invalid_since = resource.times()
    last_updated = time.asctime(time.gmtime(last_updated))
    if last_modified:
        last_modified = time.asctime(time.gmtime(last_modified))

    if invalid_since:
        error_info = resource.error_info()
    else:
        error_info = ''

    items, last_id = resource.get_headlines(None)
    items = items[-15:]
    items.reverse()

    for item in items:
        item.published = format_timestamp(item.published)
        item.link = rewriter.rewrite(item.link)

    return (resource.id(), templ.render(rid=format_rid(resource.id()),
                                        url=resource.url(),
                                        link=channel_info.link,
                                        title=channel_title,
                                        penalty=100*resource.penalty() / 1024,
                                        updated=last_modified,
                                        polled=last_updated,
                                        error_info=error_info,
                                        items=items))
Exemple #8
0
def page(ids='', rewrite=False):
    db = RSS_Resource_db()
    if ids:
        rids = map(parse_rid, ids.split(','))
    else:
        rids = []
    resources = []

    content_top = render_template('top.html')
    content_iter = itertools.chain(iter((content_top,)),
                                   ResourceIterator(rids, db, rewrite),
                                   BottomIterator(rids))

    response = current_app.response_class(content_iter)
    return response
Exemple #9
0
def addurl(ids=''):
    db = RSS_Resource_db()
    if ids:
        rids = map(parse_rid, ids.split(','))
    else:
        rids = []

    url = request.form['url']
    if not url:
        raise NotFound()

    while url != None:
        resource = RSS_Resource(url, db, generate_id)
        url, seq = resource.redirect_info(db)

    if resource.id() not in rids:
        rids.append(resource.id())

    ridlist = map(format_rid, rids)
    raise RequestRedirect(url_for('page', ids=','.join(ridlist)))
Exemple #10
0
    elif optname in ('-o', '--output'):
        epubname = optval
    elif optname in ('-r', '--rewrite-db'):
        rewrite_db = optval
    elif optname in ('-u', '--user-agent'):
        http_headers['User-Agent'] = optval


def get_http_session():
    sess = requests.Session()
    sess.headers.update(http_headers)
    return sess


init_parserss()
db = RSS_Resource_db()
now = int(time.time())
rewriter = UrlRewriter(rewrite_db)

epub = ZipFile(epubname, 'w', ZIP_DEFLATED)
epub.writestr('mimetype', 'application/epub+zip', ZIP_STORED)
epub.writestr('META-INF/container.xml', CONTAINER_XML)

sess = get_http_session()
rss_titles, pageinfo, visited, resources = [], [], {}, {}

for rss in args:
    while rss != None:
        resource = RSS_Resource(rss, db)
        rss, seqnr = resource.redirect_info(db)