Advertisement
Guest User

Untitled

a guest
Oct 29th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import praw
  2. import config
  3. import time
  4. import os
  5. from prawcore.exceptions import ResponseException
  6.  
  7. full_path = os.path.abspath(__file__)
  8. dir_path = os.path.dirname(full_path)
  9. praw_dest_path = os.path.join(dir_path, 'praw.ini')
  10.  
  11. praw_ini = """[DEFAULT]
  12. # A boolean to indicate whether or not to check for package updates.
  13. check_for_updates=True
  14.  
  15. # Object to kind mappings
  16. comment_kind=t1
  17. message_kind=t4
  18. redditor_kind=t2
  19. submission_kind=t3
  20. subreddit_kind=t5
  21. trophy_kind=t6
  22.  
  23. # The URL prefix for OAuth-related requests.
  24. oauth_url=https://oauth.reddit.com
  25.  
  26. # The amount of seconds of ratelimit to sleep for upon encountering a specific type of 429 error.
  27. ratelimit_seconds=5
  28.  
  29. # The URL prefix for regular requests.
  30. reddit_url=https://www.reddit.com
  31.  
  32. # The URL prefix for short URLs.
  33. short_url=https://redd.it
  34.  
  35. # The timeout for requests to Reddit in number of seconds
  36. timeout=16
  37. """
  38.  
  39. if not os.path.exists(praw_dest_path):
  40. with open(praw_dest_path, "w") as f:
  41. f.write(praw_ini)
  42.  
  43. path = "C:\\Users\\dotis\\OneDrive\\Desktop\\m!pin 2.0\\comments_replied_to.txt" #path to comment file
  44. ignored_users = ["automoderator", "DuplicateDestroyer"]
  45.  
  46. def bot_login():
  47. print("Logging in...")
  48. try:
  49. r = praw.Reddit(username = config.username,
  50. password = config.password,
  51. client_id = config.client_id,
  52. client_secret = config.client_secret,
  53. user_agent = "The Reddit commenter v1.0",
  54. check_for_updates=False,
  55. comment_kind="t1",
  56. message_kind="t4",
  57. redditor_kind="t2",
  58. submission_kind="t3",
  59. subreddit_kind="t5",
  60. trophy_kind="t6",
  61. oauth_url="https://oauth.reddit.com",
  62. reddit_url="https://www.reddit.com",
  63. short_url="https://redd.it",
  64. ratelimit_seconds=600,
  65. timeout=16)
  66. except ResponseException:
  67. print("Failed to log in. Please check your credentials.")
  68. else:
  69. print("Logged in!")
  70. return r
  71.  
  72. def run_bot(r):
  73. print("Searching last 10000000000000 comments")
  74. target = "Minecraft2"
  75. #target = "test+"
  76. targets = target.split("+")
  77. while("" in targets):
  78. targets.remove("")
  79. for i in range(len(targets)):
  80. print(f"current target r/{targets[i]}")
  81. for comment in r.subreddit(targets[i]).comments(limit=10):
  82. try:
  83. txt = comment.body
  84. if comment.author is not None:
  85. a = comment.author.name.lower()
  86. if any([entry.lower() == a for entry in ignored_users]):
  87. continue
  88. except Exception as e:
  89. print(e)
  90. else:
  91. try:
  92. ############
  93. if "m!pin" in txt.lower() and comment.id not in comments_replied_to and comment.author != r.user.me():
  94. print(f"String with \"m!pin\" found in comment {comment.id} by {comment.author}")
  95. if comment.author == comment.submission.author:
  96. try:
  97. reply_text = txt.split('m!pin', 1)[1].strip()
  98. if reply_text:
  99. reply = comment.submission.reply(reply_text)
  100. reply.mod.distinguish(how='yes', sticky=True)
  101. reply.report(reason='Pinned comment. Please review')
  102. comments_replied_to.append(comment.id)
  103. else:
  104. print("No text to reply with")
  105. except Exception as e:
  106. print(e)
  107. comments_replied_to.append(comment.id)
  108. with open(path,'a') as f:
  109. f.write(comment.id + "\n")
  110. else:
  111. comments_replied_to.append(comment.id)
  112. print("Replied to comment " + comment.id)
  113. with open(path,'a') as f:
  114. f.write(comment.id + "\n")
  115. except Exception as e:
  116. print(e)
  117.  
  118. print("Search Completed.")
  119. print("Sleeping for 1200 seconds...") #Sleep for 1200 seconds...
  120. time.sleep(1200)
  121.  
  122. def get_saved_comments():
  123. with open(path, "r") as f:
  124. comments_replied_to = f.read()
  125. comments_replied_to = comments_replied_to.split("\n")
  126. return comments_replied_to
  127.  
  128. comments_replied_to = get_saved_comments()
  129.  
  130. while True:
  131. try:
  132. r = bot_login()
  133. except Exception as e:
  134. print(e)
  135. print("\n\nCouldnt login")
  136. else:
  137. run_bot(r)
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement