Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #this file was used to get and save the name of users who commented on the posts of users in level 1 and were not present in level 1
  2. import pprint
  3.  
  4. import pymongo
  5. client = pymongo.MongoClient('130.212.214.188', 27017)
  6. db = client.bioinformatics_857
  7.  
  8. collection2 = db.user_post_history_level_1
  9. cursor = collection2.find(no_cursor_timeout=True)
  10. collection3 = db.user_post_history_level_2
  11. collection4 = db.done_users
  12.  
  13. import praw
  14.  
  15. my_user_agent = "RandomForest"
  16. my_client_id = "zyyC4k0WkK_glw"
  17. my_client_secret = "Y5NsNv39WgVifcgSz_gqJBpoDxM"
  18. my_username = "RandomForest07"
  19. my_password = "Thesisspring2017"
  20.  
  21. reddit = praw.Reddit(user_agent=my_user_agent,
  22. client_id=my_client_id,
  23. client_secret=my_client_secret)
  24.  
  25. reddit = praw.Reddit(user_agent=my_user_agent,
  26. client_id=my_client_id,client_secret=my_client_secret,
  27. username=my_username,
  28. password=my_password)
  29.  
  30. for c,i in enumerate(cursor):
  31. print c
  32. redditor = i["redditor"]
  33. if redditor == '[deleted]':
  34. continue
  35. # print redditor
  36. posts = i["posts"]
  37. for p in posts:
  38. if p["subreddit"]=="opiates" or p["subreddit"]=="OpiatesRecovery":
  39. # print p["id"]
  40. # print p["post"]
  41. comments= p["comments"]
  42. for c in comments:
  43. comment_redditor = c["author"]
  44. if comment_redditor == "None":
  45. continue
  46. collection = db.done_users
  47. done_users = collection.distinct("redditor")
  48. if comment_redditor in done_users:
  49. continue
  50. print "new user"
  51. user_dic={}
  52. user_dic["redditor"]=comment_redditor
  53. print "comment author ",comment_redditor
  54. user = reddit.redditor(comment_redditor)
  55. submissions = user.submissions
  56. posts =[]
  57. try:
  58. for c,s in enumerate(submissions.new(limit=None)):
  59. post_dic={}
  60. post_dic['subreddit']= str(s.subreddit)
  61. post_dic['title'] = s.title
  62. post_dic['post'] = s.selftext
  63. post_dic['created_at'] = s.created_utc
  64. post_dic['num_comments'] = s.num_comments
  65. post_dic['score'] = s.score
  66. post_dic['ups'] = s.ups
  67. post_dic['downs'] = s.downs
  68. post_dic['id'] = s.id
  69. posts.append(post_dic)
  70.  
  71. user_dic['posts']=posts
  72. collection3.insert(user_dic)
  73. collection4.insert({"redditor":comment_redditor})
  74.  
  75. except Exception as exc:
  76. pprint.pprint(vars(exc))
  77. collection4.insert({"redditor":comment_redditor})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement