Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import pymongo
  2. import praw
  3. import pprint
  4. import pickle
  5.  
  6. my_user_agent = "orecovery"
  7. my_client_id = "_El8RNVxmx59Mg"
  8. my_client_secret = "0FEASY-6G8KBaMaS0bCx1nKxooA"
  9. my_username = "singhlab"
  10. my_password = "RahulSingh!@#$"
  11.  
  12. reddit = praw.Reddit(user_agent=my_user_agent,
  13. client_id=my_client_id,
  14. client_secret=my_client_secret)
  15.  
  16. reddit = praw.Reddit(user_agent=my_user_agent,
  17. client_id=my_client_id,client_secret=my_client_secret,
  18. username=my_username,
  19. password=my_password)
  20.  
  21. client = pymongo.MongoClient('130.212.214.188', 27017)
  22. db = client.ieee_big_data
  23. collection = db.user_post_history_opiatesrecovery_copy
  24. cursor = collection.find(no_cursor_timeout=True)
  25. cursor=cursor.sort("_id",-1)
  26. collection2 = db.user_comment_history
  27. # comments=[]
  28. missed_users=[]
  29. done_users =[]
  30. cursor2 = collection2.find()
  31. users_done=0
  32. # for c,j in enumerate(cursor2):
  33. # print c
  34. # done_users.append(j["redditor"])
  35. for i in cursor:
  36. # if i["redditor"]=="santiaguina":
  37. # continue
  38. print i["redditor"]
  39. if "got_comments" in i:
  40. continue
  41. user_redditor= i["redditor"]
  42. comments=[]
  43. try:
  44. user = reddit.redditor(user_redditor)
  45. comments_new = user.comments.new(limit=None)
  46. count=0
  47. for c in comments_new:
  48. comment_dic={}
  49. # comment_dic["body"]= c.body
  50. # print c.body
  51. # print c.subreddit.display_name
  52. comment_dic["subreddit"]= c.subreddit.display_name
  53. comment_dic["created_at"]= c.created_utc
  54. comments.append(comment_dic)
  55. count +=1
  56. print count
  57. except Exception as exc:
  58. pprint.pprint(vars(exc))
  59. try:
  60. dic={}
  61. dic["redditor"]= i["redditor"]
  62. dic["comments"]=comments
  63. collection2.insert(dic)
  64. users_done +=1
  65. i["got_comments"]=1
  66. collection.save(i)
  67. except Exception as exc:
  68. missed_users.append(i["redditor"])
  69. pprint.pprint(vars(exc))
  70. users_done +=1
  71. print "users_done ",users_done
  72.  
  73.  
  74. with open("test.txt", "wb") as fp: #Pickling
  75. pickle.dump(missed_users, fp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement