Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 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_2
  9. cursor = collection2.find(no_cursor_timeout=True)
  10. cursor.sort("_id", pymongo.ASCENDING)
  11. collection3 = db.user_post_history_level_3
  12. collection4 = db.done_users
  13.  
  14. import praw
  15.  
  16. my_user_agent = "RandomForest"
  17. my_client_id = "zyyC4k0WkK_glw"
  18. my_client_secret = "Y5NsNv39WgVifcgSz_gqJBpoDxM"
  19. my_username = "RandomForest07"
  20. my_password = "Thesisspring2017"
  21.  
  22. reddit = praw.Reddit(user_agent=my_user_agent,
  23. client_id=my_client_id,
  24. client_secret=my_client_secret)
  25.  
  26. reddit = praw.Reddit(user_agent=my_user_agent,
  27. client_id=my_client_id,client_secret=my_client_secret,
  28. username=my_username,
  29. password=my_password)
  30.  
  31. for c,i in enumerate(cursor):
  32. print c
  33. redditor = i["redditor"]
  34. if redditor == '[deleted]':
  35. continue
  36. # print redditor
  37. posts = i["posts"]
  38. for p in posts:
  39. if p["subreddit"]=="opiates" or p["subreddit"]=="OpiatesRecovery":
  40. # print p["id"]
  41. # print p["post"]
  42. comments= p["comments"]
  43. for c in comments:
  44. comment_redditor = c["author"]
  45. if comment_redditor == "None":
  46. continue
  47. collection = db.done_users
  48. done_users = collection.distinct("redditor")
  49. if comment_redditor in done_users:
  50. print "old stuff"
  51. continue
  52. print "new user"
  53. user_dic={}
  54. user_dic["redditor"]=comment_redditor
  55. print "comment author ",comment_redditor
  56. user = reddit.redditor(comment_redditor)
  57. submissions = user.submissions
  58. posts =[]
  59. try:
  60. for c,s in enumerate(submissions.new(limit=None)):
  61. post_dic={}
  62. post_dic['subreddit']= str(s.subreddit)
  63. post_dic['title'] = s.title
  64. post_dic['post'] = s.selftext
  65. post_dic['created_at'] = s.created_utc
  66. post_dic['num_comments'] = s.num_comments
  67. post_dic['score'] = s.score
  68. post_dic['ups'] = s.ups
  69. post_dic['downs'] = s.downs
  70. post_dic['id'] = s.id
  71. posts.append(post_dic)
  72.  
  73. user_dic['posts']=posts
  74. collection3.insert(user_dic)
  75. collection4.insert({"redditor":comment_redditor})
  76.  
  77. except Exception as exc:
  78. pprint.pprint(vars(exc))
  79. collection4.insert({"redditor":comment_redditor})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement