JL iz&dZddlZddlZddlZddlmZdZdZdZdZ dZ d Z dd Z ed dd Z dd Z ddZdZdZy)zu Utility functions for the `twitterclient` module which do not require the `twython` library to have been installed. N) deprecated.cpg}|D]} t||||S#t$r}td||d}~wwxYw)z Extract field values from a full tweet and return them as a list :param json tweet: The tweet in JSON format :param list fields: The fields to be extracted from the tweet :rtype: list(str) z6Fatal error when extracting fields. Cannot find field N)_add_field_to_out TypeError RuntimeError)tweetfieldsoutfieldes Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/twitter/common.pyextract_fieldsrsW C  eUC 0 J  H%  s  5 05cjt|rt|\}}t||||y|||gz }yN)_is_composed_key_get_key_value_composedr)jsonr r keyvalues rrr)s8,U3 U$s)UC0 U }ct|vSr)HIER_SEPARATOR)r s rrr1s U ""rcn|jt}|d}tj|dd}||fS)Nr)splitrjoin)r r rrs rrr5s9 ++n %C a&C   AB (E :rc|syt|tr@|jD]+\}}||k(r|cS|dk(s|dk(st||}|)|cSyt|tr|D]}t||}||cSyy)Nentitiesextended_entities) isinstancedictitems_get_entity_recursivelist)rentityrr candidateitems rr$r$=s  D$ $$**, %JCf} j C+>$>1%@ ($$ % D$  !D-dF;I$   !rct||||\}}|j||D]4}tj|} t | |} |j| 6|j y)a; Extract selected fields from a file of line-separated JSON tweets and write to a file in CSV format. This utility function allows a file of full tweets to be easily converted to a CSV file for easier processing. For example, just TweetIDs or just the text content of the Tweets can be extracted. Additionally, the function allows combinations of fields of other Twitter objects (mainly the users, see below). For Twitter entities (e.g. hashtags of a Tweet), and for geolocation, see `json2csv_entities` :param str infile: The name of the file containing full tweets :param str outfile: The name of the text file where results should be written :param list fields: The list of fields to be extracted. Useful examples are 'id_str' for the tweetID and 'text' for the text of the tweet. See for a full list of fields. e. g.: ['id_str'], ['id', 'text', 'favorite_count', 'retweet_count'] Additionally, it allows IDs from other Twitter objects, e. g., ['id', 'text', 'user.id', 'user.followers_count', 'user.friends_count'] :param error: Behaviour for encoding errors, see https://docs.python.org/3/library/codecs.html#codec-base-classes :param gzip_compress: if `True`, output files are compressed with gzip N) _outf_writerwriterowrloadsrclose) fpoutfiler encodingerrors gzip_compresswriteroutfliner rows rjson2csvr7WseD"'8V]KNVT OOF 4 UF+ JJLrz-Use open() and csv.writer() directly instead.ct||||S)z+Get a CSV writer with optional compression.)r*)r/r0r1r2s routf_writer_compatr9s 6= AArc|rtj|dd||}nt|dd||}tj|}||fS)Nwt)newliner0r1w)gzipopencsvr3)r/r0r1r2r4r3s rr*r*sEyy$XfUGS"xO ZZ F D>rct||||\}} t|||} |j| |D]} tj| } t |rDt |\} }t| | }|s@t||}t||}t||||gt| |}t| |}t||||| jy)a Extract selected fields from a file of line-separated JSON tweets and write to a file in CSV format. This utility function allows a file of full Tweets to be easily converted to a CSV file for easier processing of Twitter entities. For example, the hashtags or media elements of a tweet can be extracted. It returns one line per entity of a Tweet, e.g. if a tweet has two hashtags there will be two lines in the output file, one per hashtag :param tweets_file: the file-like object containing full Tweets :param str outfile: The path of the text file where results should be written :param list main_fields: The list of fields to be extracted from the main object, usually the tweet. Useful examples: 'id_str' for the tweetID. See for a full list of fields. e. g.: ['id_str'], ['id', 'text', 'favorite_count', 'retweet_count'] If `entity_type` is expressed with hierarchy, then it is the list of fields of the object that corresponds to the key of the entity_type, (e.g., for entity_type='user.urls', the fields in the main_fields list belong to the user object; for entity_type='place.bounding_box', the files in the main_field list belong to the place object of the tweet). :param list entity_type: The name of the entity: 'hashtags', 'media', 'urls' and 'user_mentions' for the tweet object. For a user object, this needs to be expressed with a hierarchy: `'user.urls'`. For the bounding box of the Tweet location, use `'place.bounding_box'`. :param list entity_fields: The list of fields to be extracted from the entity. E.g. `['text']` (of the Tweet) :param error: Behaviour for encoding errors, see https://docs.python.org/3/library/codecs.html#codec-base-classes :param gzip_compress: if `True`, output files are compressed with gzip N) r*get_header_field_listr+rr,rrr$r_write_to_filer-) tweets_filer/ main_fields entity_type entity_fieldsr0r1r2r3r4headerr5r rr object_json object_fieldsr# tweet_fieldss rjson2csv_entitiesrMsd"'8V]KNVT "; ] KF OOFG 4  K (0=JC/s;K*; DM)+u=E =% G)%=L)%=E < v FG JJLrct|rt|\}}|}|}nd}|}|r%|Dcgc]}tj||g}}n|}|Dcgc]}tj||g} }|| zScc}wcc}wr)rrrr) rFrGrHrr main_entity sub_entityxoutput1output2s rrCrCs $,[9 U    BMNQ>&& Q'78NN=JK~""J?3KGK W  OLs A5A:c |syt|tr|}|Dcgc]}t|r|}}|Dcgc]}t|s|}}|D]#}||} t| tr|| z }|| gz }%|D]H} t | \} } || } t| tst dj | || | gz }J|j|y|D]"}|t||z}|j|$ycc}wcc}w)NzEKey {} does not contain a dictionary in the json file) r!r"rr%rrformatr+r)rKr#rHr3r6rQentity_field_valuesentity_field_composedr rdkdvd json_dictr(s rrDrDs1  %*7SQ?OPQ?RqSS,9 Qq=Ma=P Q Q( E%LE%&u w  ' #A,Q/FBb Ii."$$*F% IbM? "C # nT=AA/T QsC;C;DD)utf8replaceF)F)__doc__rAr?rnltk.internalsrrrrrrr$r7r9r*rMrCrDrrras %&#6KP*Z ;<B=B  DN"#r