Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Reddit Poll Creator by martmists
- # (Hugely inefficient)
- # !/usr/bin/python
- import praw
- import pdb
- import re
- import os
- import requests
- import time
- import codecs
- import CREDENTIALS # Custom file, defines USER, PASS and SUB
- def strawpoll_url(poll_title, poll_options):
- base_url = 'http://strawpoll.me/api/v2/polls'
- data = {
- 'title': poll_title,
- 'options': poll_options
- }
- response = requests.post(base_url, data=data)
- if response.status_code != 201:
- return None
- poll_id = response.json().get('id', None)
- if poll_id == None:
- return None
- poll_id = str(poll_id)
- return 'http://strawpoll.me/' + poll_id
- REDDIT_USERNAME = CREDENTIALS.USER
- REDDIT_PASS = CREDENTIALS.PASS
- REDDIT_SUB = CREDENTIALS.SUB
- # Create the Reddit instance
- user_agent = ("Strawpoll creator v1.9 by /u/martmists")
- r = praw.Reddit(user_agent=user_agent)
- # and login
- r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
- # Have we run this code before? If not, create an empty list
- if not os.path.isfile("posts_data.txt"):
- poll_data = []
- # If we have run the code before, load the list of posts we have replied to
- else:
- # Read the file into a list and remove any empty values
- with open("posts_data.txt", "r", encoding='utf-8') as f:
- poll_data = f.read()
- poll_data = poll_data.split("\n")
- print ('Current data: ', poll_data)
- # Have we run this code before? If not, create an empty list
- if not os.path.isfile("posts_replied_to.txt"):
- posts_replied_to = []
- # If we have run the code before, load the list of posts we have replied to
- else:
- # Read the file into a list and remove any empty values
- with open("posts_replied_to.txt", "r", encoding='utf-8') as f:
- posts_replied_to = f.read()
- posts_replied_to = posts_replied_to.split("\n")
- print ('Posts replied to: ', posts_replied_to)
- subreddit = r.get_subreddit(REDDIT_SUB)
- active = True
- while active:
- update = False
- print ('checking for new Polls...')
- for comment in subreddit.get_comments(limit=None):
- options = []
- options2 = []
- options3 = []
- options4 = []
- options5 = []
- options6 = []
- options7 = []
- options8 = []
- options9 = []
- urls = []
- AllOptions = []
- # If we haven't replied to this post before
- if comment.id not in posts_replied_to:
- print("checking comment: " + comment.id)
- # print(comment)
- body = str(comment.body)
- subBody = body.split(' | ')
- author = comment.author
- # print (len(subBody))
- if len(subBody) <= 32:
- commentdata = """
- Please follow the following link to vote on the Strawpoll:
- [{title}]({url})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 62 and len(subBody) > 32:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 92 and len(subBody) > 62:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 122 and len(subBody) > 92:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- [{title} (4)]({url4})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 152 and len(subBody) > 122:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- [{title} (4)]({url4})
- [{title} (5)]({url5})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 182 and len(subBody) > 152:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- [{title} (4)]({url4})
- [{title} (4)]({url4})
- [{title} (5)]({url5})
- [{title} (6)]({url6})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 212 and len(subBody) > 182:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- [{title} (4)]({url4})
- [{title} (4)]({url4})
- [{title} (5)]({url5})
- [{title} (6)]({url6})
- [{title} (7)]({url7})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 242 and len(subBody) > 212:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- [{title} (4)]({url4})
- [{title} (5)]({url5})
- [{title} (6)]({url6})
- [{title} (7)]({url7})
- [{title} (8)]({url8})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if len(subBody) <= 272 and len(subBody) > 242:
- commentdata = """
- Please follow the following links to vote on the Strawpoll:
- [{title}]({url})
- [{title} (2)]({url2})
- [{title} (3)]({url3})
- [{title} (4)]({url4})
- [{title} (5)]({url5})
- [{title} (6)]({url6})
- [{title} (7)]({url7})
- [{title} (8)]({url8})
- [{title} (9)]({url9})
- -------------
- ^^^^[Source](http://pastebin.com/C4Mi4anq) ^^^^// ^^^^[Creator](http://www.reddit.com/u/martmists) ^^^^// [^^^^Report ^^^^an ^^^^error](https://www.reddit.com/message/compose/?to=martmists)
- """
- if re.search('CreatePoll', body):
- print ('Bot replying to ID: ', comment.id, ', Poll creator: ', author)
- print ('Comment to reply to: ', body)
- # Reply to the post
- a = 0
- for word in subBody:
- if a == 1:
- title = word
- print ('Setting title: ', title)
- if a >= 2:
- AllOptions.append(word + ' | ')
- if a <= 31:
- print ('Added option: ', word, ' to list 1')
- options.append(word)
- if a > 31 and a <= 61:
- print ('Added option: ', word, ' to list 2')
- options2.append(word)
- if a > 61 and a <= 91:
- print ('Added option: ', word, ' to list 3')
- options3.append(word)
- if a > 91 and a <= 121:
- print ('Added option: ', word, ' to list 4')
- options4.append(word)
- if a > 121 and a <= 151:
- print ('Added option: ', word, ' to list 5')
- options5.append(word)
- if a > 151 and a <= 181:
- print ('Added option: ', word, ' to list 6')
- options6.append(word)
- if a > 181 and a <= 211:
- print ('Added option: ', word, ' to list 7')
- options7.append(word)
- if a > 211 and a <= 241:
- print ('Added option: ', word, ' to list 8')
- options8.append(word)
- if a > 241 and a <= 271:
- print ('Added option: ', word, ' to list 9')
- options9.append(word)
- a = a + 1
- print ('Options for 1: ', options)
- print ('Options for 2: ', options2)
- print ('Options for 3: ', options3)
- print ('Options for 4: ', options4)
- print ('Options for 5: ', options5)
- print ('Options for 6: ', options6)
- print ('Options for 7: ', options7)
- print ('Options for 8: ', options8)
- print ('Options for 9: ', options9)
- url = strawpoll_url(title, options)
- urls.append(url)
- if len(subBody) > 32:
- url2 = strawpoll_url(title, options2)
- urls.append(url2)
- if len(subBody) > 62:
- url3 = strawpoll_url(title, options3)
- urls.append(url3)
- if len(subBody) > 92:
- url4 = strawpoll_url(title, options4)
- urls.append(url4)
- if len(subBody) > 122:
- url5 = strawpoll_url(title, options5)
- urls.append(url5)
- if len(subBody) > 152:
- url6 = strawpoll_url(title, options6)
- urls.append(url6)
- if len(subBody) > 182:
- url7 = strawpoll_url(title, options7)
- urls.append(url7)
- if len(subBody) > 212:
- url8 = strawpoll_url(title, options8)
- urls.append(url8)
- if len(subBody) > 242:
- url9 = strawpoll_url(title, options9)
- urls.append(url9)
- if url == None:
- print ('Export Failed')
- continue
- if len(subBody) <= 32:
- comment.reply(commentdata.strip().format(title=title, url=url))
- if len(subBody) > 32 and len(subBody) <= 62:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2))
- if len(subBody) > 62 and len(subBody) <= 92:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3))
- if len(subBody) > 92 and len(subBody) <= 122:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4))
- if len(subBody) > 122 and len(subBody) <= 152:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5))
- if len(subBody) > 152 and len(subBody) <= 182:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6))
- if len(subBody) > 182 and len(subBody) <= 212:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6, url7=url7))
- if len(subBody) > 212 and len(subBody) <= 242:
- comment.reply(commentdata.strip().format(title=title, url=url, url2=url2, url3=url3, url4=url4, url5=url5, url6=url6, url7=url7, url8=url8))
- if len(subBody) > 242:
- 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))
- print ('Finished replying to ID: ', comment.id)
- print ('Strawpoll: ', url)
- # Store the current id into our list
- print ('Storing ID: ', comment.id)
- posts_replied_to.append(comment.id)
- poll_data.append('ID: ' + comment.id)
- poll_data.append('Poll ID: ' + str(urls))
- poll_data.append('Title: ' + title)
- poll_data.append('Options: ' + str(AllOptions))
- poll_data.append('Poll Creator: ' + str(author))
- poll_data.append('------------------------------------')
- if not update:
- update = True
- # Write our updated list back to the file
- if update:
- with open("posts_replied_to.txt", "w", encoding='utf-8') as f:
- for post_id in posts_replied_to:
- if not post_id == '':
- f.write(post_id + "\n")
- with open("posts_data.txt","w", encoding='utf-8') as f:
- for item in poll_data:
- f.write(item + '\n')
- print ('Waiting 10 minutes...')
- time.sleep(600)
Add Comment
Please, Sign In to add comment