<s> from __future__ import absolute_import , unicode_literals <EOL> import xml . sax <EOL> try : <EOL> from io import BytesIO as _StringIO <EOL> except ImportError : <EOL> try : <EOL> from cStringIO import StringIO as _StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO as _StringIO <EOL> try : <EOL> import urllib . parse <EOL> except ImportError : <EOL> from urlparse import urlparse <EOL> class urllib ( object ) : <EOL> class parse ( object ) : <EOL> urlparse = staticmethod ( urlparse ) <EOL> from . datetimes import registerDateHandler , _parse_date <EOL> from . encodings import convert_to_utf8 <EOL> from . exceptions import * <EOL> from . html import _BaseHTMLProcessor <EOL> from . import http <EOL> from . import mixin <EOL> from . mixin import _FeedParserMixin <EOL> from . parsers . loose import _LooseFeedParser <EOL> from . parsers . strict import _StrictFeedParser <EOL> from . sanitizer import replace_doctype <EOL> from . sgml import * <EOL> from . urls import _convert_to_idn , _makeSafeAbsoluteURI <EOL> from . util import FeedParserDict <EOL> bytes_ = type ( b'<STR_LIT>' ) <EOL> unicode_ = type ( '<STR_LIT>' ) <EOL> try : <EOL> unichr <EOL> basestring <EOL> except NameError : <EOL> unichr = chr <EOL> basestring = str <EOL> PREFERRED_XML_PARSERS = [ "<STR_LIT>" ] <EOL> RESOLVE_RELATIVE_URIS = <NUM_LIT:1> <EOL> SANITIZE_HTML = <NUM_LIT:1> <EOL> _XML_AVAILABLE = True <EOL> mixin . RESOLVE_RELATIVE_URIS = RESOLVE_RELATIVE_URIS <EOL> mixin . SANITIZE_HTML = SANITIZE_HTML <EOL> SUPPORTED_VERSIONS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def _open_resource ( url_file_stream_or_string , etag , modified , agent , referrer , handlers , request_headers , result ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( url_file_stream_or_string , '<STR_LIT>' ) : <EOL> return url_file_stream_or_string . read ( ) <EOL> if isinstance ( url_file_stream_or_string , basestring ) and urllib . parse . urlparse ( url_file_stream_or_string ) [ <NUM_LIT:0> ] in ( '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:file>' , '<STR_LIT>' ) : <EOL> return http . get ( url_file_stream_or_string , etag , modified , agent , referrer , handlers , request_headers , result ) <EOL> try : <EOL> with open ( url_file_stream_or_string , '<STR_LIT:rb>' ) as f : <EOL> data = f . read ( ) <EOL> except ( IOError , UnicodeEncodeError , TypeError , ValueError ) : <EOL> pass <EOL> else : <EOL> return data <EOL> if not isinstance ( url_file_stream_or_string , bytes_ ) : <EOL> return url_file_stream_or_string . encode ( '<STR_LIT:utf-8>' ) <EOL> return url_file_stream_or_string <EOL> LooseFeedParser = type ( str ( '<STR_LIT>' ) , ( <EOL> _LooseFeedParser , _FeedParserMixin , _BaseHTMLProcessor , object <EOL> ) , { } ) <EOL> StrictFeedParser = type ( str ( '<STR_LIT>' ) , ( <EOL> _StrictFeedParser , _FeedParserMixin , xml . sax . handler . ContentHandler , object <EOL> ) , { } ) <EOL> def parse ( url_file_stream_or_string , etag = None , modified = None , agent = None , referrer = None , handlers = None , request_headers = None , response_headers = None ) : <EOL> '''<STR_LIT>''' <EOL> if not agent : <EOL> agent = USER_AGENT <EOL> result = FeedParserDict ( <EOL> bozo = False , <EOL> entries = [ ] , <EOL> feed = FeedParserDict ( ) , <EOL> headers = { } , <EOL> ) <EOL> data = _open_resource ( url_file_stream_or_string , etag , modified , agent , referrer , handlers , request_headers , result ) <EOL> if not data : <EOL> return result <EOL> result [ '<STR_LIT>' ] . update ( response_headers or { } ) <EOL> data = convert_to_utf8 ( result [ '<STR_LIT>' ] , data , result ) <EOL> use_strict_parser = result [ '<STR_LIT>' ] and True or False <EOL> result [ '<STR_LIT:version>' ] , data , entities = replace_doctype ( data ) <EOL> contentloc = result [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> href = result . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> baseuri = _makeSafeAbsoluteURI ( href , contentloc ) or _makeSafeAbsoluteURI ( contentloc ) or href <EOL> baselang = result [ '<STR_LIT>' ] . get ( '<STR_LIT>' , None ) <EOL> if isinstance ( baselang , bytes_ ) and baselang is not None : <EOL> baselang = baselang . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:ignore>' ) <EOL> if not _XML_AVAILABLE : <EOL> use_strict_parser = <NUM_LIT:0> <EOL> if use_strict_parser : <EOL> feedparser = StrictFeedParser ( baseuri , baselang , '<STR_LIT:utf-8>' ) <EOL> saxparser = xml . sax . make_parser ( PREFERRED_XML_PARSERS ) <EOL> saxparser . setFeature ( xml . sax . handler . feature_namespaces , <NUM_LIT:1> ) <EOL> try : <EOL> saxparser . setFeature ( xml . sax . handler . feature_external_ges , <NUM_LIT:0> ) <EOL> except xml . sax . SAXNotSupportedException : <EOL> pass <EOL> saxparser . setContentHandler ( feedparser ) <EOL> saxparser . setErrorHandler ( feedparser ) <EOL> source = xml . sax . xmlreader . InputSource ( ) <EOL> source . setByteStream ( _StringIO ( data ) ) <EOL> try : <EOL> saxparser . parse ( source ) <EOL> except xml . sax . SAXException as e : <EOL> result [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> result [ '<STR_LIT>' ] = feedparser . exc or e <EOL> use_strict_parser = <NUM_LIT:0> <EOL> if not use_strict_parser and _SGML_AVAILABLE : <EOL> feedparser = LooseFeedParser ( baseuri , baselang , '<STR_LIT:utf-8>' , entities ) <EOL> feedparser . feed ( data . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) ) <EOL> result [ '<STR_LIT>' ] = feedparser . feeddata <EOL> result [ '<STR_LIT>' ] = feedparser . entries <EOL> result [ '<STR_LIT:version>' ] = result [ '<STR_LIT:version>' ] or feedparser . version <EOL> result [ '<STR_LIT>' ] = feedparser . namespacesInUse <EOL> return result </s>
