'Fetches all new messages from a subscribed RSS Feed'

@staticmethod
def poll_rss(channel_id):

    db = current.db
    s3db = current.s3db
    table = s3db.msg_rss_channel
    query = (table.channel_id == channel_id)
    channel = db(query).select(table.date, table.etag, table.url, table.content_type, table.username, table.password, limitby=(0, 1)).first()
    if (not channel):
        return ('No    Such    RSS    Channel:    %s' % channel_id)
    import feedparser
    username = channel.username
    password = channel.password
    if (username and password):
        base64string = base64.encodestring(('%s:%s' % (username, password))).replace('\n', '')
        request_headers = {'Authorization': ('Basic    %s' % base64string)}
    else:
        request_headers = None
    if channel.content_type:
        response_headers = {'content-type': 'application/xml'}
    else:
        response_headers = None
    if channel.etag:
        d = feedparser.parse(channel.url, etag=channel.etag, request_headers=request_headers, response_headers=response_headers)
    elif channel.date:
        d = feedparser.parse(channel.url, modified=channel.date.utctimetuple(), request_headers=request_headers, response_headers=response_headers)
    else:
        d = feedparser.parse(channel.url, request_headers=request_headers, response_headers=response_headers)
    if d.bozo:
        S3Msg.update_channel_status(channel_id, status=d.bozo_exception.message, period=(300, 3600))
        return
    now = current.request.utcnow
    data = dict(date=now)
    etag = d.get('etag', None)
    if etag:
        data['etag'] = etag
    db(query).update(**data)
    from time import mktime, struct_time
    gis = current.gis
    geocode_r = gis.geocode_r
    hierarchy_level_keys = gis.hierarchy_level_keys
    utcfromtimestamp = datetime.datetime.utcfromtimestamp
    gtable = db.gis_location
    ginsert = gtable.insert
    mtable = db.msg_rss
    minsert = mtable.insert
    ltable = db.msg_rss_link
    linsert = ltable.insert
    update_super = s3db.update_super
    parser = s3db.msg_parser_enabled(channel_id)
    if parser:
        ptable = db.msg_parsing_status
        pinsert = ptable.insert
    entries = d.entries
    if entries:
        count_old = db((mtable.id > 0)).count()
    for entry in entries:
        link = entry.get('link', None)
        exists = db((mtable.from_address == link)).select(mtable.id, mtable.location_id, mtable.message_id, limitby=(0, 1)).first()
        if exists:
            location_id = exists.location_id
        else:
            location_id = None
        title = entry.get('title')
        content = entry.get('content', None)
        if content:
            content = content[0].value
        else:
            content = entry.get('description', None)
        date_published = entry.get('published_parsed', entry.get('updated_parsed'))
        if isinstance(date_published, struct_time):
            date_published = utcfromtimestamp(mktime(date_published))
        else:
            date_published = now
        tags = entry.get('tags', None)
        if tags:
            tags = [t.term.encode('utf-8') for t in tags]
        location = False
        lat = entry.get('geo_lat', None)
        lon = entry.get('geo_long', None)
        if ((lat is None) or (lon is None)):
            georss = entry.get('georss_point', None)
            if georss:
                location = True
                (lat, lon) = georss.split('    ')
        else:
            location = True
        if location:
            try:
                query = ((gtable.lat == lat) & (gtable.lon == lon))
                lexists = db(query).select(gtable.id, limitby=(0, 1), orderby=gtable.level).first()
                if lexists:
                    location_id = lexists.id
                else:
                    data = dict(lat=lat, lon=lon)
                    results = geocode_r(lat, lon)
                    if isinstance(results, dict):
                        for key in hierarchy_level_keys:
                            v = results.get(key, None)
                            if v:
                                data[key] = v
                    location_id = ginsert(**data)
                    data['id'] = location_id
                    gis.update_location_tree(data)
            except:
                pass
        links = entry.get('links', [])
        if exists:
            db((mtable.id == exists.id)).update(channel_id=channel_id, title=title, from_address=link, body=content, author=entry.get('author', None), date=date_published, location_id=location_id, tags=tags)
            if links:
                query_ = ((ltable.rss_id == exists.id) & (ltable.deleted != True))
                for link_ in links:
                    url_ = link_['url']
                    type_ = link_['type']
                    query = ((query_ & (ltable.url == url_)) & (ltable.type == type_))
                    dbset = db(query)
                    row = dbset.select(ltable.id, limitby=(0, 1)).first()
                    if row:
                        dbset.update(rss_id=exists.id, url=url_, type=type_)
                    else:
                        linsert(rss_id=exists.id, url=url_, type=type_)
            if parser:
                pinsert(message_id=exists.message_id, channel_id=channel_id)
        else:
            _id = minsert(channel_id=channel_id, title=title, from_address=link, body=content, author=entry.get('author', None), date=date_published, location_id=location_id, tags=tags)
            record = dict(id=_id)
            update_super(mtable, record)
            for link_ in links:
                linsert(rss_id=_id, url=link_['url'], type=link_['type'])
            if parser:
                pinsert(message_id=record['message_id'], channel_id=channel_id)
    if entries:
        count_new = db((mtable.id > 0)).count()
        if (count_new == count_old):
            S3Msg.update_channel_status(channel_id, status='+1', period=(300, 3600))
    return 'OK'
