Martmists

Reddit StrawPoll bot

Mar 12th, 2016
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.68 KB | None | 0 0
  1. # Reddit Poll Creator by martmists
  2. # (Hugely inefficient)
  3. # !/usr/bin/python
  4. import praw
  5. import pdb
  6. import re
  7. import os
  8. import requests
  9. import time
  10. import codecs
  11. import CREDENTIALS # Custom file, defines USER, PASS and SUB
  12.  
  13. def strawpoll_url(poll_title, poll_options):
  14.     base_url = 'http://strawpoll.me/api/v2/polls'
  15.     data = {
  16.         'title': poll_title,
  17.         'options': poll_options
  18.     }
  19.  
  20.     response = requests.post(base_url, data=data)
  21.     if response.status_code != 201:
  22.         return None
  23.  
  24.     poll_id = response.json().get('id', None)
  25.     if poll_id == None:
  26.         return None
  27.     poll_id = str(poll_id)
  28.     return 'http://strawpoll.me/' + poll_id
  29.  
  30. REDDIT_USERNAME = CREDENTIALS.USER
  31. REDDIT_PASS = CREDENTIALS.PASS
  32. REDDIT_SUB = CREDENTIALS.SUB
  33.  
  34. # Create the Reddit instance
  35. user_agent = ("Strawpoll creator v1.9 by /u/martmists")
  36. r = praw.Reddit(user_agent=user_agent)
  37.  
  38. # and login
  39. r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
  40.  
  41.  
  42.  
  43. # Have we run this code before? If not, create an empty list
  44. if not os.path.isfile("posts_data.txt"):
  45.     poll_data = []
  46.  
  47. # If we have run the code before, load the list of posts we have replied to
  48. else:
  49.     # Read the file into a list and remove any empty values
  50.     with open("posts_data.txt", "r", encoding='utf-8') as f:
  51.         poll_data = f.read()
  52.         poll_data = poll_data.split("\n")
  53.         print ('Current data: ', poll_data)
  54.        
  55.        
  56.  
  57. # Have we run this code before? If not, create an empty list
  58. if not os.path.isfile("posts_replied_to.txt"):
  59.     posts_replied_to = []
  60.  
  61. # If we have run the code before, load the list of posts we have replied to
  62. else:
  63.     # Read the file into a list and remove any empty values
  64.     with open("posts_replied_to.txt", "r", encoding='utf-8') as f:
  65.         posts_replied_to = f.read()
  66.         posts_replied_to = posts_replied_to.split("\n")
  67.         print ('Posts replied to: ', posts_replied_to)
  68.  
  69.  
  70. subreddit = r.get_subreddit(REDDIT_SUB)
  71.  
  72. active = True
  73. while active:
  74.     update = False
  75.     print ('checking for new Polls...')
  76.     for comment in subreddit.get_comments(limit=None):  
  77.         options = []
  78.         options2 = []
  79.         options3 = []
  80.         options4 = []
  81.         options5 = []
  82.         options6 = []
  83.         options7 = []
  84.         options8 = []
  85.         options9 = []
  86.         urls = []
  87.         AllOptions = []
  88.         # If we haven't replied to this post before
  89.         if comment.id not in posts_replied_to:
  90.             print("checking comment: " + comment.id)
  91.             # print(comment)
  92.             body = str(comment.body)
  93.             subBody = body.split(' | ')
  94.             author = comment.author
  95.             # print (len(subBody))
  96.             if len(subBody) <= 32:
  97.                 commentdata = """
  98.                Please follow the following link to vote on the Strawpoll:    
  99.                [{title}]({url})    
  100.                -------------    
  101.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  102.                """
  103.                
  104.                
  105.             if len(subBody) <= 62 and len(subBody) > 32:
  106.                 commentdata = """
  107.                Please follow the following links to vote on the Strawpoll:    
  108.                [{title}]({url})    
  109.                [{title} (2)]({url2})      
  110.                -------------    
  111.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  112.                """
  113.                
  114.                
  115.             if len(subBody) <= 92 and len(subBody) > 62:
  116.                 commentdata = """
  117.                Please follow the following links to vote on the Strawpoll:    
  118.                [{title}]({url})    
  119.                [{title} (2)]({url2})    
  120.                [{title} (3)]({url3})    
  121.                -------------    
  122.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  123.                """            
  124.                  
  125.             if len(subBody) <= 122 and len(subBody) > 92:
  126.                 commentdata = """
  127.                Please follow the following links to vote on the Strawpoll:    
  128.                [{title}]({url})    
  129.                [{title} (2)]({url2})    
  130.                [{title} (3)]({url3})    
  131.                [{title} (4)]({url4})    
  132.                -------------    
  133.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  134.                """
  135.                    
  136.             if len(subBody) <= 152 and len(subBody) > 122:
  137.                 commentdata = """
  138.                Please follow the following links to vote on the Strawpoll:    
  139.                [{title}]({url})    
  140.                [{title} (2)]({url2})    
  141.                [{title} (3)]({url3})    
  142.                [{title} (4)]({url4})    
  143.                [{title} (5)]({url5})    
  144.                -------------    
  145.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  146.                """
  147.            
  148.             if len(subBody) <= 182 and len(subBody) > 152:
  149.                 commentdata = """
  150.                Please follow the following links to vote on the Strawpoll:    
  151.                [{title}]({url})    
  152.                [{title} (2)]({url2})    
  153.                [{title} (3)]({url3})    
  154.                [{title} (4)]({url4})    
  155.                [{title} (4)]({url4})  
  156.                [{title} (5)]({url5})    
  157.                [{title} (6)]({url6})    
  158.                -------------    
  159.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  160.                """
  161.            
  162.             if len(subBody) <= 212 and len(subBody) > 182:
  163.                 commentdata = """
  164.                Please follow the following links to vote on the Strawpoll:    
  165.                [{title}]({url})    
  166.                [{title} (2)]({url2})    
  167.                [{title} (3)]({url3})    
  168.                [{title} (4)]({url4})    
  169.                [{title} (4)]({url4})  
  170.                [{title} (5)]({url5})    
  171.                [{title} (6)]({url6})    
  172.                [{title} (7)]({url7})    
  173.                -------------    
  174.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  175.                """
  176.             if len(subBody) <= 242 and len(subBody) > 212:
  177.                 commentdata = """
  178.                Please follow the following links to vote on the Strawpoll:    
  179.                [{title}]({url})    
  180.                [{title} (2)]({url2})    
  181.                [{title} (3)]({url3})    
  182.                [{title} (4)]({url4})  
  183.                [{title} (5)]({url5})    
  184.                [{title} (6)]({url6})    
  185.                [{title} (7)]({url7})    
  186.                [{title} (8)]({url8})                      
  187.                -------------    
  188.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  189.                """
  190.                
  191.             if len(subBody) <= 272 and len(subBody) > 242:
  192.                 commentdata = """
  193.                Please follow the following links to vote on the Strawpoll:    
  194.                [{title}]({url})    
  195.                [{title} (2)]({url2})    
  196.                [{title} (3)]({url3})    
  197.                [{title} (4)]({url4})    
  198.                [{title} (5)]({url5})    
  199.                [{title} (6)]({url6})    
  200.                [{title} (7)]({url7})    
  201.                [{title} (8)]({url8})    
  202.                [{title} (9)]({url9})    
  203.                -------------    
  204.                ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
  205.                """
  206.                
  207.             if re.search('CreatePoll', body):
  208.                 print ('Bot replying to ID: ', comment.id, ', Poll creator: ', author)
  209.                 print ('Comment to reply to: ', body)
  210.                 # Reply to the post
  211.                 a = 0
  212.                 for word in subBody:
  213.                     if a == 1:
  214.                         title = word
  215.                         print ('Setting title: ', title)
  216.                     if a >= 2:
  217.                         AllOptions.append(word + ' | ')
  218.                         if a <= 31:
  219.                             print ('Added option: ', word, ' to list 1')
  220.                             options.append(word)
  221.                         if a > 31 and a <= 61:
  222.                             print ('Added option: ', word, ' to list 2')
  223.                             options2.append(word)
  224.                         if a > 61 and a <= 91:
  225.                             print ('Added option: ', word, ' to list 3')
  226.                             options3.append(word)
  227.                         if a > 91 and a <= 121:
  228.                             print ('Added option: ', word, ' to list 4')
  229.                             options4.append(word)
  230.                         if a > 121 and a <= 151:
  231.                             print ('Added option: ', word, ' to list 5')
  232.                             options5.append(word)
  233.                         if a > 151 and a <= 181:
  234.                             print ('Added option: ', word, ' to list 6')
  235.                             options6.append(word)
  236.                         if a > 181 and a <= 211:
  237.                             print ('Added option: ', word, ' to list 7')
  238.                             options7.append(word)
  239.                         if a > 211 and a <= 241:
  240.                             print ('Added option: ', word, ' to list 8')
  241.                             options8.append(word)
  242.                         if a > 241 and a <= 271:
  243.                             print ('Added option: ', word, ' to list 9')
  244.                             options9.append(word)
  245.                     a = a + 1
  246.                    
  247.                 print ('Options for 1: ', options)
  248.                 print ('Options for 2: ', options2)
  249.                 print ('Options for 3: ', options3)
  250.                 print ('Options for 4: ', options4)
  251.                 print ('Options for 5: ', options5)
  252.                 print ('Options for 6: ', options6)
  253.                 print ('Options for 7: ', options7)
  254.                 print ('Options for 8: ', options8)
  255.                 print ('Options for 9: ', options9)
  256.                
  257.  
  258.                 url = strawpoll_url(title, options)
  259.                 urls.append(url)
  260.                 if len(subBody) > 32:
  261.                     url2 = strawpoll_url(title, options2)
  262.                     urls.append(url2)
  263.                     if len(subBody) > 62:
  264.                         url3 = strawpoll_url(title, options3)
  265.                         urls.append(url3)
  266.                         if len(subBody) > 92:
  267.                             url4 = strawpoll_url(title, options4)
  268.                             urls.append(url4)
  269.                             if len(subBody) > 122:
  270.                                 url5 = strawpoll_url(title, options5)
  271.                                 urls.append(url5)
  272.                                 if len(subBody) > 152:
  273.                                     url6 = strawpoll_url(title, options6)
  274.                                     urls.append(url6)
  275.                                     if len(subBody) > 182:
  276.                                         url7 = strawpoll_url(title, options7)
  277.                                         urls.append(url7)
  278.                                         if len(subBody) > 212:
  279.                                             url8 = strawpoll_url(title, options8)
  280.                                             urls.append(url8)
  281.                                             if len(subBody) > 242:
  282.                                                 url9 = strawpoll_url(title, options9)
  283.                                                 urls.append(url9)
  284.                 if url == None:
  285.                     print ('Export Failed')
  286.                     continue
  287.                
  288.                 if len(subBody) <= 32:
  289.                     comment.reply(commentdata.strip().format(title=title, url=url))
  290.                 if len(subBody) > 32 and len(subBody) <= 62:
  291.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2))
  292.                 if len(subBody) > 62 and len(subBody) <= 92:
  293.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3))
  294.                 if len(subBody) > 92 and len(subBody) <= 122:
  295.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4))
  296.                 if len(subBody) > 122 and len(subBody) <= 152:
  297.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5))
  298.                 if len(subBody) > 152 and len(subBody) <= 182:
  299.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6))
  300.                 if len(subBody) > 182 and len(subBody) <= 212:
  301.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6, url7=url7))
  302.                 if len(subBody) > 212 and len(subBody) <= 242:
  303.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6, url7=url7, url8=url8))
  304.                 if len(subBody) > 242:
  305.                     comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6, url7=url7, url8=url8, url9=url9))
  306.                 print ('Finished replying to ID: ', comment.id)
  307.                 print ('Strawpoll: ', url)
  308.                
  309.                
  310.                
  311.                 # Store the current id into our list
  312.                 print ('Storing ID: ', comment.id)
  313.                 posts_replied_to.append(comment.id)
  314.                 poll_data.append('ID: ' + comment.id)
  315.                 poll_data.append('Poll ID: ' + str(urls))
  316.                 poll_data.append('Title: ' + title)
  317.                 poll_data.append('Options: ' + str(AllOptions))
  318.                 poll_data.append('Poll Creator: ' + str(author))
  319.                 poll_data.append('------------------------------------')
  320.                 if not update:
  321.                     update = True
  322.                
  323.     # Write our updated list back to the file
  324.     if update:
  325.         with open("posts_replied_to.txt", "w", encoding='utf-8') as f:
  326.             for post_id in posts_replied_to:
  327.                 if not post_id == '':
  328.                     f.write(post_id + "\n")
  329.         with open("posts_data.txt","w", encoding='utf-8') as f:
  330.             for item in poll_data:
  331.                 f.write(item + '\n')
  332.     print ('Waiting 10 minutes...')
  333.     time.sleep(600)
Add Comment
Please, Sign In to add comment