Advertisement
dmesticg

Untitled

Dec 16th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import praw
  2. import config
  3. import time
  4. import os
  5. import random
  6. import re
  7. import requests
  8.  
  9. def bot_login():
  10. print ("Loggin in...")
  11. global r
  12. r = praw.Reddit(username = config.username,
  13. password = config.password,
  14. client_id = config.client_id,
  15. client_secret = config.client_secret,
  16. user_agent = config.user_agent)
  17. print ("Logged in!")
  18.  
  19.  
  20. def search():
  21. global r
  22. global comments_dealt_with
  23. for subreddit in config.subreddits:
  24. print ("Obtaining 25 comments in " + subreddit)
  25. for comment in r.subreddit(subreddit).comments(limit=25):
  26. if ("RemindMeBTC!" in comment.body and comment.id not in comments_dealt_with and comment.author != r.user.me()):
  27. print ("String with \"RemindMeBTC!\" found in comment " + comment.id)
  28. thecomment = comment.body
  29. try:
  30. if "RemindMeBTC!" in thecomment:
  31. thecomment = thecomment.split(" ")
  32. price = thecomment[1]
  33. comment.reply("Alright, I'll send you a message when bitcoin hits " + price)
  34. print("Replying!")
  35. with open ("need_to_message.txt", "a") as a:
  36. a.write(str(comment.author) + " " + price + "\n")
  37. comments_dealt_with.append(comment.id)
  38. with open ("comments_dealt_with.txt", "a") as a:
  39. a.write(comment.id + "\n")
  40. except (AttributeError, ValueError):
  41. print ("Invalid formatting, ignoring")
  42. comments_dealt_with.append(comment.id)
  43. with open ("comments_dealt_with.txt", "a") as a:
  44. a.write(comment.id + "\n")
  45.  
  46. def message():
  47. global r
  48. global comments_dealt_with
  49.  
  50. data = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
  51. current_price = data.json()['bpi']['USD']['rate_float']
  52.  
  53. with open("need_to_message.txt", "r") as b:
  54. need_to_message = [line.strip() for line in b if line.strip()]
  55.  
  56. for i in range(len(need_to_message)):
  57. splitted = need_to_message[i].split(" ")
  58. username = splitted[0]
  59. price = splitted[1]
  60. if (current_price - 100 < float(price) < current_price + 100):
  61. r.redditor(username).message("Bitcoin Price Reminder", "You asked for a reminder when bitcoin hits the price of $" + price + " USD, well it is currently $" + str(current_price) + " USD")
  62. print("Sent a message to /u/" + username)
  63. with open("need_to_message.txt", "w") as b:
  64. lines = b.readlines()
  65. for line in lines:
  66. if line != need_to_message[i]:
  67. b.write(line)
  68.  
  69. def get_saved_comments():
  70. global comments_dealt_with
  71. if not os.path.isfile("comments_dealt_with.txt"):
  72. comments_dealt_with = []
  73. else:
  74. with open("comments_dealt_with.txt", "r") as f:
  75. comments_dealt_with = f.read()
  76. comments_dealt_with = comments_dealt_with.split("\n")
  77. #comments_dealt_with = filter(None, comments_dealt_with)
  78.  
  79. bot_login()
  80. get_saved_comments()
  81. while True:
  82. search()
  83. message()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement