'Parse RSS Feeds into the CAP Module'

@staticmethod
def parse_rss_2_cap(message):

    db = current.db
    s3db = current.s3db
    table = s3db.msg_rss
    message_id = message.message_id
    record = db((table.message_id == message_id)).select(table.id, table.channel_id, table.title, table.from_address, table.body, table.date, table.location_id, table.author, limitby=(0, 1)).first()
    if (not record):
        return
    pstable = s3db.msg_parsing_status
    pquery = (pstable.message_id == message_id)
    prows = db(pquery).select(pstable.id, pstable.is_parsed)
    for prow in prows:
        if prow.is_parsed:
            return
    alert_table = s3db.cap_alert
    info_table = s3db.cap_info
    body = (record.body or record.title)
    query = (info_table.description == body)
    exists = db(query).select(info_table.id, limitby=(0, 1)).first()
    author = record.author
    if author:
        ptable = s3db.pr_person
        from nameparser import HumanName
        name = HumanName(author)
        first_name = name.first
        middle_name = name.middle
        last_name = name.last
        query = (((ptable.first_name == first_name) & (ptable.middle_name == middle_name)) & (ptable.last_name == last_name))
        pexists = db(query).select(ptable.id, limitby=(0, 1)).first()
        if pexists:
            person_id = pexists.id
        else:
            person_id = ptable.insert(first_name=first_name, middle_name=middle_name, last_name=last_name)
            s3db.update_super(ptable, dict(id=person_id))
    else:
        person_id = None
    if exists:
        info_id = exists.id
        db((info_table.id == info_id)).update(headline=record.title, description=body, created_on=record.date)
    else:
        url = record.from_address
        import_xml = s3db.resource('cap_alert').import_xml
        stylesheet = os.path.join(current.request.folder, 'static', 'formats', 'cap', 'import.xsl')
        try:
            file = fetch(url)
        except urllib2.HTTPError as e:
            import base64
            rss_table = s3db.msg_rss_channel
            query = (rss_table.channel_id == record.channel_id)
            channel = db(query).select(rss_table.date, rss_table.etag, rss_table.url, rss_table.username, rss_table.password, limitby=(0, 1)).first()
            username = channel.username
            password = channel.password
            if ((e.code == 401) and username and password):
                request = urllib2.Request(url)
                base64string = base64.encodestring(('%s:%s' % (username, password)))
                request.add_header('Authorization', ('Basic    %s' % base64string))
            else:
                request = None
            try:
                file = (urllib2.urlopen(request).read() if request else fetch(url))
            except urllib2.HTTPError as e:
                from urlparse import urlparse
                ltable = s3db.msg_rss_link
                query_ = ((ltable.rss_id == record.id) & (ltable.deleted != True))
                rows_ = db(query_).select(ltable.type, ltable.url)
                url_format = '{uri.scheme}://{uri.netloc}/'.format
                url_domain = url_format(uri=urlparse(url))
                for row_ in rows_:
                    url = row_.url
                    if (url and (row_.type == 'application/cap+xml') and (url_domain == url_format(uri=urlparse(url)))):
                        if ((e.code == 401) and username and password):
                            request = urllib2.Request(url)
                            request.add_header('Authorization', ('Basic    %s' % base64string))
                        else:
                            request = None
                        try:
                            file = (urllib2.urlopen(request).read() if request else fetch(url))
                        except urllib2.HTTPError as e:
                            current.log.error(('Getting    content    from    link    failed:    %s' % e))
                        else:
                            import_xml(StringIO(file), stylesheet=stylesheet, ignore_errors=True)
            else:
                import_xml(StringIO(file), stylesheet=stylesheet, ignore_errors=True)
        else:
            import_xml(StringIO(file), stylesheet=stylesheet, ignore_errors=True)
    return
