Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. ...
  2. user__entities__url__urls [u'http://website.com']
  3. user__entities__description__urls []
  4. user__profile_location u'None'
  5. user__url u'http://t.co/stuff'
  6. user__profile_image_url_https u'https://image_url.jpeg'
  7. user__profile_sidebar_fill_color u'EFEFEF'
  8. user__location u'Los Angeles, CA'
  9. ...
  10.  
  11. response = [{u'contributors': None, u'truncated': False, u'text': u'Hate It or love It? Kim Kardashian in Balmain & Alexander McQueen [Photos] http://t.co/UrNFK5yDPU via @lovebscott http://t.co/lZW9GAzvhx', u'in_reply_to_status_id': None, u'id': 537357629975064577, u'favorite_count': 0, u'source': u'<a href="http://tapbots.com/software/tweetbot/mac" rel="nofollow">Tweetbot for Mac</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [{u'id': 14521926, u'indices': [106, 117], u'id_str': u'14521926', u'screen_name': u'lovebscott', u'name': u'B. Scott'}], u'hashtags': [], u'urls': [{u'url': u'http://t.co/UrNFK5yDPU', u'indices': [79, 101], u'expanded_url': u'http://www.lovebscott.com/fashion/hate-it-or-love-it-kim-kardashian-in-balmain-alexander-mcqueen-photos', u'display_url': u'lovebscott.com/fashion/hate-iu2026'}], u'media': [{u'expanded_url': u'http://twitter.com/lovebscott/status/537357629975064577/photo/1', u'display_url': u'pic.twitter.com/lZW9GAzvhx', u'url': u'http://t.co/lZW9GAzvhx', u'media_url_https': u'https://pbs.twimg.com/media/B3UT6skCMAIDw62.jpg', u'id_str': u'537357629656281090', u'sizes': {u'large': {u'h': 612, u'resize': u'fit', u'w': 610}, u'small': {u'h': 341, u'resize': u'fit', u'w': 340}, u'medium': {u'h': 601, u'resize': u'fit', u'w': 600}, u'thumb': {u'h': 150, u'resize': u'crop', u'w': 150}}, u'indices': [118, 140], u'type': u'photo', u'id': 537357629656281090, u'media_url': u'http://pbs.twimg.com/media/B3UT6skCMAIDw62.jpg'}]}, u'in_reply_to_screen_name': None, u'in_reply_to_user_id': None, u'retweet_count': 0, u'id_str': u'537357629975064577', u'favorited': False, u'user': {u'follow_request_sent': None, u'profile_use_background_image': False, u'profile_text_color': u'333333', u'default_profile_image': False, u'id': 14521926, u'profile_background_image_url_https': u'https://pbs.twimg.com/profile_background_images/457457546580602880/VxHBaVbH.jpeg', u'verified': True, u'profile_location': None, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/531605236696104960/cG-Lu2y6_normal.jpeg', u'profile_sidebar_fill_color': u'EFEFEF', u'entities': {u'url': {u'urls': [{u'url': u'http://t.co/3nt6d6jM9p', u'indices': [0, 22], u'expanded_url': u'http://lovebscott.com', u'display_url': u'lovebscott.com'}]}, u'description': {u'urls': []}}, u'followers_count': 161968, u'profile_sidebar_border_color': u'FFFFFF', u'id_str': u'14521926', u'profile_background_color': u'131516', u'listed_count': 1905, u'is_translation_enabled': False, u'utc_offset': -18000, u'statuses_count': 58304, u'description': u'#KingofFabulous - #TheMultimediaMaven - Mogul - TV / Internet Personality - @EBONYMag Advice Columnist - @glam_com Contributing Editor', u'friends_count': 373, u'location': u'Los Angeles, CA', u'profile_link_color': u'009999', u'profile_image_url': u'http://pbs.twimg.com/profile_images/531605236696104960/cG-Lu2y6_normal.jpeg', u'following': None, u'geo_enabled': True, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/14521926/1403029806', u'profile_background_image_url': u'http://pbs.twimg.com/profile_background_images/457457546580602880/VxHBaVbH.jpeg', u'name': u'B. Scott', u'lang': u'en', u'profile_background_tile': True, u'favourites_count': 14, u'screen_name': u'lovebscott', u'notifications': None, u'url': u'http://t.co/3nt6d6jM9p', u'created_at': u'Fri Apr 25 03:29:42 +0000 2008', u'contributors_enabled': False, u'time_zone': u'Quito', u'protected': False, u'default_profile': False, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': None, u'possibly_sensitive': True, u'lang': u'en', u'created_at': u'Tue Nov 25 21:30:17 +0000 2014', u'in_reply_to_status_id_str': None, u'place': None, u'metadata': {u'iso_language_code': u'en', u'result_type': u'recent'}}]
  12.  
  13. {
  14. key0:values_0,
  15. key_0:{
  16. key_1:values_1,
  17. key_1:{
  18. key_2:values_2,
  19. key_2:{
  20. key_3:values_3
  21. }
  22. }
  23. }
  24. }
  25.  
  26. def test_for_entity(root,key,entity_value):
  27. # test if list is entity
  28. parent_key = root.split("__")[-1:][0]
  29. if 'entities'in root.split("__"):
  30. # Entities for tweets
  31. if key in ("symbols","hashtags"):
  32. list_items = [list_item['text'] for list_item in entity_value]
  33. print root+"__"+key,list_items
  34. elif key == "media":
  35. list_items = [[list_item['type'],list_item['media_url']] for list_item in entity_value]
  36. print root+"__"+key,list_items
  37. elif key == "urls":
  38. list_items = [list_item['expanded_url'] for list_item in entity_value]
  39. print root+"__"+key,list_items
  40. elif key == "user_mentions":
  41. list_items = [list_item['screen_name'] for list_item in entity_value]
  42. print root+"__"+key,list_items
  43. # Entities for users
  44. elif key == "url":
  45. list_items = [list_item['expanded_url'] for list_item in entity_value['urls']]
  46. print root+"__"+key,list_items
  47. elif key == "description":
  48. list_items = [list_item['expanded_url'] for list_item in entity_value['urls']]
  49. print root+"__"+key,list_items
  50. else:
  51. print "[ERROR: unknown entity name'"+str(key)+"']","list",parent_key+"__"+key,list_items
  52. else:
  53. list_items = [list_item for list_item in entity_value]
  54. print root+"__"+key,list_items,parent_key
  55.  
  56. for tweet in response:
  57. for key_0,value_0 in tweet.items():
  58. if type(value_0) is dict:
  59. for key_1,value_1 in value_0.items():
  60. if type(value_1) is dict:
  61. for key_2,value_2 in value_1.items():
  62. if type(value_2) is dict:
  63. for key_3,value_3 in value_2.items():
  64. if type(value_3) is dict:
  65. # Limit of recursive unpacking...
  66. print key_0+"__"+key_1+"__"+key_2+"__"+key_3,value_3
  67. elif type(value_3) is list:
  68. test_for_entity(root = key_0+"__"+key_1+"__"+key_2,key = key_3,entity_value=value_3)
  69. else:
  70. print key_0+"__"+key_1+"__"+key_2+"__"+key_3,value_3
  71. elif type(value_2) is list:
  72. test_for_entity(root = key_0+"__"+key_1,key = key_2,entity_value=value_2)
  73. else:
  74. print key_0+"__"+key_1+"__"+key_2,value_2
  75. elif type(value_1) is list:
  76. test_for_entity(root=key_0,key = key_1,entity_value=value_1)
  77. else:
  78. print key_0+"__"+key_1,value_1
  79. elif type(value_0) is list:
  80. test_for_entity(root="",key = key_0,entity_value=value_0)
  81. else:
  82. print key_0,value_0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement