Advertisement
Guest User

jerrys_doctor_bot_script

a guest
Nov 19th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import io
  3. import time
  4. import traceback
  5. import config
  6. import praw
  7. import sys
  8.  
  9. jerrynames = ['jerry','jerry\'s','jerrys','gary','garrys','garys','garry','garry\'s','gary\'s','larry','larrys','larry\'s','terry','terrys','terry\'s','mayor gergich','mayor gergichs','mayor gergich\'s']
  10. #jerrynames = ['test9']
  11.  
  12. start_time = time.time()
  13.  
  14. # File input
  15. f = open('cachefile.txt','r')
  16.  
  17. with open('cachefile.txt') as f:
  18.     id_cache = f.read().splitlines()
  19.  
  20. f.close()
  21.  
  22. def botlogin():
  23.   print ("Logging in...")
  24.   r = praw.Reddit(username=config.BOT_USERNAME,
  25.             password=config.BOT_PASSWORD,
  26.             client_id=config.BOT_CLIENT_ID,
  27.             client_secret=config.BOT_CLIENT_SECRET,
  28.             user_agent='Jerry\'s doctor bot v0.1')
  29.   print ("Logged in!\n")
  30.   return r
  31.  
  32. def runbot(r):
  33.   print ("\nSearching for a new Jerry post...\n")
  34.   try:
  35.     for submission in r.subreddit(config.TARGET_SUBREDDIT).stream.submissions():
  36.       # Sterilizing the input
  37.       lower_title = submission.title.lower()
  38.       normalized_title = lower_title
  39.       encoded_title = normalized_title.encode('utf-8')
  40.  
  41.       for name in jerrynames:
  42.         if name in encoded_title:
  43.           if submission.id not in id_cache:
  44.          
  45.               # Case switch using custom dictionary so that the comment matches the jerry name used in the post title
  46.             def jerry():
  47.                 return "Jerry"
  48.  
  49.             def garry():
  50.                 return "Garry"
  51.  
  52.             def larry():
  53.                 return "Larry"
  54.  
  55.             def terry():
  56.                 return "Terry"
  57.  
  58.             nameSwitch = {
  59.                             "jerry" : jerry,
  60.                             "jerry\'s" : jerry,
  61.                             "jerrys" : jerry,
  62.                             "gary" : garry,
  63.                             "garys" : garry,
  64.                             "gary\'s" : garry,                            
  65.                             "garry" : garry,
  66.                             "garrys" : garry,
  67.                             "garry\'s" : garry,
  68.                             "larry" : larry,
  69.                             "larrys" : larry,
  70.                             "larry\'s" : larry,
  71.                             "terry" : terry,
  72.                             "terrys" : terry,
  73.                             "terry\'s" : terry,
  74.                             "mayor gergich" : jerry,
  75.                             "mayor gergichs" : jerry,
  76.                             "mayor gergich\'s" : jerry,
  77.             }
  78.              
  79.             replyName = nameSwitch[name]()
  80.              
  81.             # New formatting experiment 10_16_2018
  82.             reply_text = (
  83.                 "I noticed you mentioned " + str(replyName) + " Gergich...\n\nI'm [Dr. Harris, ](https://i.imgur.com/Tw6fQHA.jpg)his physician. The first time I saw him was for a routine mumps checkup."
  84.                 "\n\nThat man has *the largest penis I have ever seen*."
  85.                 "\n\nI actually don\'t even know if he has mumps. Forgot to look. I was distracted by..."
  86.                 "\r#the largest penis I have ever seen."
  87.                 "\n\n___"
  88.                 "\n\n[*I am just a bot.*](https://imgur.com/a/6Q93L6k) *That Eagletonian prick* /u/BackyrdFurnitureFire *built me.*")
  89.  
  90.             # This actually replies to the reddit post with a comment
  91.             submission.reply(reply_text)
  92.             print('Located a compatible Jerry submission!\nReplying to PandR post entitled: "{}"'.format(encoded_title))
  93.             id_cache.append(submission.id)
  94.  
  95.             cachefile = open('cachefile.txt', 'w')
  96.             for item in id_cache:
  97.                 cachefile.write(item + "\n")
  98.             cachefile.close()
  99.  
  100.             print "\nid_cache = ",
  101.             print (id_cache)
  102.  
  103.             # This just waits for a bit so we don't spam the sub too bad.
  104.             print "\nSleeping for 10 seconds..."
  105.             time.sleep(1)
  106.             print "10..."
  107.             time.sleep(1)
  108.             print "9..."
  109.             time.sleep(1)
  110.             print "8..."
  111.             time.sleep(1)
  112.             print "7..."
  113.             time.sleep(1)
  114.             print "6..."
  115.             time.sleep(1)
  116.             print "5..."
  117.             time.sleep(1)
  118.             print "4..."
  119.             time.sleep(1)
  120.             print "3..."
  121.             time.sleep(1)
  122.             print "2..."
  123.             time.sleep(1)
  124.             print "1..."
  125.             time.sleep(1)
  126.             print "\nSearching for a new Jerry post...\n"
  127.             break
  128.  
  129.   # This just lets us get out if we want to
  130.   except BaseException as e:
  131.     if type(e) == KeyboardInterrupt:
  132.       sys.exit(0)
  133.     else:
  134.       traceback.print_exc()
  135.  
  136. # This actually runs it
  137. r = botlogin()
  138. while True:  # This is what keeps it running
  139.   runbot(r)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement