common.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- coding: utf-8 -*-
  2. # Natural Language Toolkit: Twitter client
  3. #
  4. # Copyright (C) 2001-2020 NLTK Project
  5. # Author: Ewan Klein <ewan@inf.ed.ac.uk>
  6. # Lorenzo Rubio <lrnzcig@gmail.com>
  7. # URL: <http://nltk.org/>
  8. # For license information, see LICENSE.TXT
  9. """
  10. Utility functions for the :module:`twitterclient` module which do not require
  11. the `twython` library to have been installed.
  12. """
  13. import csv
  14. import gzip
  15. import json
  16. from nltk.internals import deprecated
  17. HIER_SEPARATOR = "."
  18. def extract_fields(tweet, fields):
  19. """
  20. Extract field values from a full tweet and return them as a list
  21. :param json tweet: The tweet in JSON format
  22. :param list fields: The fields to be extracted from the tweet
  23. :rtype: list(str)
  24. """
  25. out = []
  26. for field in fields:
  27. try:
  28. _add_field_to_out(tweet, field, out)
  29. except TypeError:
  30. raise RuntimeError(
  31. "Fatal error when extracting fields. Cannot find field ", field
  32. )
  33. return out
  34. def _add_field_to_out(json, field, out):
  35. if _is_composed_key(field):
  36. key, value = _get_key_value_composed(field)
  37. _add_field_to_out(json[key], value, out)
  38. else:
  39. out += [json[field]]
  40. def _is_composed_key(field):
  41. if HIER_SEPARATOR in field:
  42. return True
  43. return False
  44. def _get_key_value_composed(field):
  45. out = field.split(HIER_SEPARATOR)
  46. # there could be up to 3 levels
  47. key = out[0]
  48. value = HIER_SEPARATOR.join(out[1:])
  49. return key, value
  50. def _get_entity_recursive(json, entity):
  51. if not json:
  52. return None
  53. elif isinstance(json, dict):
  54. for key, value in json.items():
  55. if key == entity:
  56. return value
  57. # 'entities' and 'extended_entities' are wrappers in Twitter json
  58. # structure that contain other Twitter objects. See:
  59. # https://dev.twitter.com/overview/api/entities-in-twitter-objects
  60. if key == "entities" or key == "extended_entities":
  61. candidate = _get_entity_recursive(value, entity)
  62. if candidate is not None:
  63. return candidate
  64. return None
  65. elif isinstance(json, list):
  66. for item in json:
  67. candidate = _get_entity_recursive(item, entity)
  68. if candidate is not None:
  69. return candidate
  70. return None
  71. else:
  72. return None
  73. def json2csv(
  74. fp, outfile, fields, encoding="utf8", errors="replace", gzip_compress=False
  75. ):
  76. """
  77. Extract selected fields from a file of line-separated JSON tweets and
  78. write to a file in CSV format.
  79. This utility function allows a file of full tweets to be easily converted
  80. to a CSV file for easier processing. For example, just TweetIDs or
  81. just the text content of the Tweets can be extracted.
  82. Additionally, the function allows combinations of fields of other Twitter
  83. objects (mainly the users, see below).
  84. For Twitter entities (e.g. hashtags of a Tweet), and for geolocation, see
  85. `json2csv_entities`
  86. :param str infile: The name of the file containing full tweets
  87. :param str outfile: The name of the text file where results should be\
  88. written
  89. :param list fields: The list of fields to be extracted. Useful examples\
  90. are 'id_str' for the tweetID and 'text' for the text of the tweet. See\
  91. <https://dev.twitter.com/overview/api/tweets> for a full list of fields.\
  92. e. g.: ['id_str'], ['id', 'text', 'favorite_count', 'retweet_count']\
  93. Additionally, it allows IDs from other Twitter objects, e. g.,\
  94. ['id', 'text', 'user.id', 'user.followers_count', 'user.friends_count']
  95. :param error: Behaviour for encoding errors, see\
  96. https://docs.python.org/3/library/codecs.html#codec-base-classes
  97. :param gzip_compress: if `True`, output files are compressed with gzip
  98. """
  99. (writer, outf) = _outf_writer(outfile, encoding, errors, gzip_compress)
  100. # write the list of fields as header
  101. writer.writerow(fields)
  102. # process the file
  103. for line in fp:
  104. tweet = json.loads(line)
  105. row = extract_fields(tweet, fields)
  106. writer.writerow(row)
  107. outf.close()
  108. @deprecated("Use open() and csv.writer() directly instead.")
  109. def outf_writer_compat(outfile, encoding, errors, gzip_compress=False):
  110. """Get a CSV writer with optional compression."""
  111. return _outf_writer(outfile, encoding, errors, gzip_compress)
  112. def _outf_writer(outfile, encoding, errors, gzip_compress=False):
  113. if gzip_compress:
  114. outf = gzip.open(outfile, "wt", encoding=encoding, errors=errors)
  115. else:
  116. outf = open(outfile, "w", encoding=encoding, errors=errors)
  117. writer = csv.writer(outf)
  118. return (writer, outf)
  119. def json2csv_entities(
  120. tweets_file,
  121. outfile,
  122. main_fields,
  123. entity_type,
  124. entity_fields,
  125. encoding="utf8",
  126. errors="replace",
  127. gzip_compress=False,
  128. ):
  129. """
  130. Extract selected fields from a file of line-separated JSON tweets and
  131. write to a file in CSV format.
  132. This utility function allows a file of full Tweets to be easily converted
  133. to a CSV file for easier processing of Twitter entities. For example, the
  134. hashtags or media elements of a tweet can be extracted.
  135. It returns one line per entity of a Tweet, e.g. if a tweet has two hashtags
  136. there will be two lines in the output file, one per hashtag
  137. :param tweets_file: the file-like object containing full Tweets
  138. :param str outfile: The path of the text file where results should be\
  139. written
  140. :param list main_fields: The list of fields to be extracted from the main\
  141. object, usually the tweet. Useful examples: 'id_str' for the tweetID. See\
  142. <https://dev.twitter.com/overview/api/tweets> for a full list of fields.
  143. e. g.: ['id_str'], ['id', 'text', 'favorite_count', 'retweet_count']
  144. If `entity_type` is expressed with hierarchy, then it is the list of\
  145. fields of the object that corresponds to the key of the entity_type,\
  146. (e.g., for entity_type='user.urls', the fields in the main_fields list\
  147. belong to the user object; for entity_type='place.bounding_box', the\
  148. files in the main_field list belong to the place object of the tweet).
  149. :param list entity_type: The name of the entity: 'hashtags', 'media',\
  150. 'urls' and 'user_mentions' for the tweet object. For a user object,\
  151. this needs to be expressed with a hierarchy: `'user.urls'`. For the\
  152. bounding box of the Tweet location, use `'place.bounding_box'`.
  153. :param list entity_fields: The list of fields to be extracted from the\
  154. entity. E.g. `['text']` (of the Tweet)
  155. :param error: Behaviour for encoding errors, see\
  156. https://docs.python.org/3/library/codecs.html#codec-base-classes
  157. :param gzip_compress: if `True`, ouput files are compressed with gzip
  158. """
  159. (writer, outf) = _outf_writer(outfile, encoding, errors, gzip_compress)
  160. header = get_header_field_list(main_fields, entity_type, entity_fields)
  161. writer.writerow(header)
  162. for line in tweets_file:
  163. tweet = json.loads(line)
  164. if _is_composed_key(entity_type):
  165. key, value = _get_key_value_composed(entity_type)
  166. object_json = _get_entity_recursive(tweet, key)
  167. if not object_json:
  168. # this can happen in the case of "place"
  169. continue
  170. object_fields = extract_fields(object_json, main_fields)
  171. items = _get_entity_recursive(object_json, value)
  172. _write_to_file(object_fields, items, entity_fields, writer)
  173. else:
  174. tweet_fields = extract_fields(tweet, main_fields)
  175. items = _get_entity_recursive(tweet, entity_type)
  176. _write_to_file(tweet_fields, items, entity_fields, writer)
  177. outf.close()
  178. def get_header_field_list(main_fields, entity_type, entity_fields):
  179. if _is_composed_key(entity_type):
  180. key, value = _get_key_value_composed(entity_type)
  181. main_entity = key
  182. sub_entity = value
  183. else:
  184. main_entity = None
  185. sub_entity = entity_type
  186. if main_entity:
  187. output1 = [HIER_SEPARATOR.join([main_entity, x]) for x in main_fields]
  188. else:
  189. output1 = main_fields
  190. output2 = [HIER_SEPARATOR.join([sub_entity, x]) for x in entity_fields]
  191. return output1 + output2
  192. def _write_to_file(object_fields, items, entity_fields, writer):
  193. if not items:
  194. # it could be that the entity is just not present for the tweet
  195. # e.g. tweet hashtag is always present, even as [], however
  196. # tweet media may not be present
  197. return
  198. if isinstance(items, dict):
  199. # this happens e.g. for "place" of a tweet
  200. row = object_fields
  201. # there might be composed keys in de list of required fields
  202. entity_field_values = [x for x in entity_fields if not _is_composed_key(x)]
  203. entity_field_composed = [x for x in entity_fields if _is_composed_key(x)]
  204. for field in entity_field_values:
  205. value = items[field]
  206. if isinstance(value, list):
  207. row += value
  208. else:
  209. row += [value]
  210. # now check required dictionaries
  211. for d in entity_field_composed:
  212. kd, vd = _get_key_value_composed(d)
  213. json_dict = items[kd]
  214. if not isinstance(json_dict, dict):
  215. raise RuntimeError(
  216. """Key {0} does not contain a dictionary
  217. in the json file""".format(
  218. kd
  219. )
  220. )
  221. row += [json_dict[vd]]
  222. writer.writerow(row)
  223. return
  224. # in general it is a list
  225. for item in items:
  226. row = object_fields + extract_fields(item, entity_fields)
  227. writer.writerow(row)