Dec-04-2020, 09:02 PM
So I got this bot script for reddit that I want to use for some automation for my business, it works up to the point of login to reddit user but fails at sending message, I get these 3 errors:
![[Image: GjNUls3.png]](https://i.imgur.com/GjNUls3.png)
Config.py contents:(fill in apostophies with apropriate info)
The two txt files contain messages and test user.
Now this is the code main python file:
All help is much apreciated. I am not experienced with python if anyone could help me get this script working
![[Image: GjNUls3.png]](https://i.imgur.com/GjNUls3.png)
Config.py contents:(fill in apostophies with apropriate info)
1 2 3 4 |
username = ' ' #reddit username of your account password = ' ' #reddit password of your account messagesfile = 'example.txt ' #file with messages 1 per line usersfile = 'example2.txt ' #file with users to DM 1 per line |
Now this is the code main python file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import time import string import requests from bs4 import BeautifulSoup import sys from random import randint from config import * def redditlogin(username, password,c): USER = username PASSWD = password API_TYPE = 'json' OP = 'login-main' RENDERSTYLE = 'html' ACTION = 'sub' print ( "[+]Logging in as " + USER + "..." ) login_data = dict (user = USER, passwd = PASSWD, api_type = API_TYPE, op = OP) login = c.post(loginpost, data = login_data, headers = { 'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" , "x-requested-with" : 'XMLHttpRequest' , 'referer' : 'https://www.reddit.com' , 'origin' : 'https://www.reddit.com' }) if 'reddit_session' in str (login.headers): print ( '[+]Successfully logged into account ' + USER) else : print ( '[-]Could not login' ) def sendmessages(userslist, messagelist,c): USER_LIST = [] MESSAGE_LIST = [] for line in messagelist.readlines(): message = line.strip( '\n' ) MESSAGE_LIST.append(message) for line in userslist.readlines(): user = line.strip( '\n' ) USER_LIST.append(user) for user in USER_LIST: MESSAGERECIEVER = user SELECTION = (randint( 0 , len (MESSAGE_LIST) - 1 )) MESSAGE_TEXT = MESSAGE_LIST[SELECTION] print ( "[+]Messaging user " + user + " with " + MESSAGE_TEXT) #print "[+]Retrieving http://reddit.com/u/" +MESSAGERECIEVER r = c.get( 'http://reddit.com/u/' + MESSAGERECIEVER,headers = { 'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" , "x-requested-with" : 'XMLHttpRequest' , 'referer' : 'https://www.reddit.com' , 'origin' : 'https://www.reddit.com' }).content soup = BeautifulSoup(r, 'html.parser' ) MODHASH = soup.find( 'input' ,{ 'name' : 'uh' })[ 'value' ] REDDIT_SESSION = c.cookies[ 'reddit_session' ] params = ( ( 'app' , 'res' ), ) data = [ ( 'api_type' , 'json' ), ( 'from_sr' , ''), ( 'subject' , 'twqfqawfct' ), ( 'text' ,MESSAGE_TEXT), ( 'to' , MESSAGERECIEVER), ] sendrequest = c.post( 'https://www.reddit.com/api/compose' , headers = { 'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" , "x-requested-with" : 'XMLHttpRequest' , 'referer' : 'https://www.reddit.com' , 'origin' : 'https://www.reddit.com' , 'x-modhash' :MODHASH, 'reddit_session' :REDDIT_SESSION}, params = params, data = data) def main(): with requests.session() as c: try : userslist = open (usersfile, 'r' ) except IOError: sys.exit( "[-]Invalid user list!" ) try : messagelist = open (messagesfile, 'r' ) except IOError: sys.exit( "[-]Invalid message list!" ) redditlogin(username,password,c) sendmessages(userslist,messagelist,c) if __name__ = = '__main__' : main() |
