Advertisement
Guest User

Untitled

a guest
May 16th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.31 KB | None | 0 0
  1. #TODO - Parse typo's
  2. #TODO2 - Create more functions (e.g. post building into a function)
  3.  
  4. import time
  5. import praw
  6. import re
  7. import urllib2
  8. import signal, sys
  9.  
  10. # This string is sent by praw to reddit in accordance to the API rules
  11. user_agent = ("REDDIT Bot v1.4 by /u/USERNAME")
  12. r = praw.Reddit(user_agent=user_agent)
  13.  
  14. # Fill in the bot's username and password here
  15. username = "USERNAME"
  16. password = "PASSWORD"
  17. r.login(username, password)
  18.  
  19. # Fill in the subreddit(s) here. Multisubs are done with + (e.g. MagicTCG+EDH)
  20. subreddit = r.get_subreddit('INSERT_SUBREDDITS')
  21.  
  22. # This loads the already parsed comments from a backup text file
  23. already_done = []
  24. with open('magictcg_done.txt', 'r') as f:
  25.     for i in f:
  26.         already_done.append(i.replace("\n", ""))
  27.  
  28. # Function that does all the magic
  29. def bot_comments():
  30.     ids = []
  31.     sub_comments = subreddit.get_comments()
  32.     for comment in sub_comments:
  33.         ids.append(comment.id)
  34.         # Checks if the post is not actually the bot itself (since the details say [[CARDNAME]]
  35.         if comment.id not in already_done and not str(comment.author) == username:
  36.             # Regex Magic that finds the text encaptured with [[ ]]
  37.             cards = re.findall("\[\[([^\[\]]*)\]\]", comment.body)
  38.             reply = ""
  39.             # Because a comment can only have a max length, limit to only the first 30 requests
  40.             if len(cards) > 30: cards = cards[0:30]
  41.             # Set removes any duplicates
  42.             for i in set(cards):
  43.                 print i
  44.                 i = i.split('/')[0]
  45.                 # Converts obscure characters like AE to a URL-valid text
  46.                 j = urllib2.quote(i.encode('utf-8'))
  47.                 # Checks if a card exists
  48.                 card_id = card_check(i, j)
  49.                 if card_id:
  50.                     # Builds the post
  51.                    if card_id:
  52.                     reply += "[%s](http://db.fowtcg.us/cards/%s)" % (card_id[0], card_id[1])
  53.                     reply += " - "
  54.                     reply += "[DB](http://db.fowtcg.us/index.php?p=card&code=TAT-014+R%s)" % card_id[2]
  55.                     reply += "\n\n"
  56.             # If a post was built before, complete it and post it to reddit
  57.             if reply:
  58.                 reply += "^^Questions? ^^Message ^^/u/CREATOR ^^- ^^Call ^^cards ^^with ^^[[CARDNAME]] ^^- ^^Format: ^^Image ^^- ^^URL ^^to ^^Gatherer"
  59.                 # Possible advice text to advice using "AutocardAnywhere" instead
  60.                 #reply += "\n\n^^^Try ^^^the ^^^browser ^^^plugin ^^^'AutocardAnywhere' ^^^instead ^^^of ^^^the ^^^bot: ^^^Personal ^^^card-links!"
  61.                 # Posting might fail (too long, ban, reddit down etc), so cancel the post and print the error
  62.                 try:
  63.                     comment.reply(reply)
  64.                 except Exception,e: print str(e)
  65.             # Add the post to the list of parsed comments
  66.             already_done.append(comment.id)
  67.     # Finally, return the list of parsed comments (seperate from already_done)
  68.     return ids
  69.  
  70. # This function is nearly the same as comment parsing, except it takes submissions (should be combined later)
  71. def bot_submissions():
  72.     sub_ids = []
  73.     sub_subs = subreddit.get_new(limit=5)
  74.     for submission in sub_subs:
  75.         sub_ids.append(submission.id)
  76.         if submission.id not in already_done:
  77.             cards = re.findall("\[\[([^\[\]]*)\]\]", submission.selftext)
  78.             reply = ""
  79.             if len(cards) > 30: cards = cards[0:30]
  80.             for i in set(cards):
  81.                 print i
  82.                 i = i.split('/')[0]
  83.                 j = urllib2.quote(i.encode('utf-8'))
  84.                 card_id = card_check(i, j)
  85.                 if card_id:
  86.                     reply += "[%s](http://db.fowtcg.us/cards/%s)" % (card_id[0], card_id[1])
  87.                     reply += " - "
  88.                     reply += "[DB](http://db.fowtcg.us/index.php?p=card&code=TAT-014+R%s)" % card_id[2]
  89.                     reply += "\n\n"
  90.             if reply:
  91.                 reply += "^^Questions? ^^Message ^^/u/xslicer ^^- ^^Call ^^cards ^^with ^^[[CARDNAME]] ^^- ^^Format: ^^Image ^^- ^^URL ^^to ^^Gatherer"
  92.                 try:
  93.                     submission.add_comment(reply)
  94.                 except Exception,e: print str(e)
  95.             already_done.append(submission.id)
  96.     return sub_ids
  97.  
  98. # Function that checks if the requested card exist and returns the card id (card id is unneccesary
  99. # for linking since the gatherer will also link the image with it's name, but this is still valid
  100. # to check if the card exists).
  101. def card_check(card, enc_card):
  102.     try:
  103.         # Opens the Gatherer page and looks for the card ID with Regex - Replaces & because it breaks URLs
  104.         page = urllib2.urlopen("http://db.fowtcg.us/index.php?do=search&q=%s&exact=yes&infields[]=name&orderby=setnum" % enc_card.replace("&", "%26")).read()
  105.         # [0] is the Name, [1] is the Card ID, [2] is the img file
  106.         return [re.search("alt=\"([a-zA-Z- 0-9]*)\"", page).group(1).replace(" ", "+"), re.search("data-id=\"([a-zA-Z- 0-9]*)\"", page).group(1).replace(" ", "+"), re.search("src=\"thumbs/(([a-zA-Z- 0-9]*)\"", page).group(1)]
  107.     except AttributeError:
  108.         print "ERROR"
  109.         return False
  110.  
  111. # Function that backs up current parsed comments
  112. def write_done():
  113.     with open("magictcg_done.txt", "w") as f:
  114.         for i in already_done:
  115.             f.write(str(i) + '\n')
  116.  
  117. # Function that is called when ctrl-c is pressed. It backups the current parsed comments into a backup file and then quits.
  118. def signal_handler(signal, frame):
  119.     write_done()
  120.     sys.exit(0)
  121. signal.signal(signal.SIGINT, signal_handler)
  122.  
  123. # Infinite loop that calls the function. The function outputs the post-ID's of all parsed comments.
  124. # The ID's of parsed comments is compared with the already parsed comments so the list stays clean
  125. # and memory is not increased. It sleeps for 15 seconds to wait for new posts.
  126. while True:
  127.     ids = bot_comments()
  128.     time.sleep(5)
  129.     sub_ids = bot_submissions()
  130.     new_done = []
  131.     # Checks for both comments and submissions
  132.     for i in already_done:
  133.         if i in ids:
  134.             new_done.append(i)
  135.         if i in sub_ids:
  136.             new_done.append(i)
  137.     already_done = new_done[:]
  138.     # Back up the parsed comments to a file
  139.     write_done()
  140.     time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement