Python Forum
Need help with 3 errors when running script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with 3 errors when running script
#1
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]

Config.py contents:(fill in apostophies with apropriate info)

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 
The two txt files contain messages and test user.



Now this is the code main python file:

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'

    loginpost           = 'https://www.reddit.com/api/login/'+USER
    homeurl             = 'https://www.reddit.com'


    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()
All help is much apreciated. I am not experienced with python if anyone could help me get this script working Smile
Reply
#2
The code lines shown are the line numbers that were executed prior to receiving the error.
So in Your case, the sequence is: line 89 (main()), then line 85, and finally line 54 where the error actually occurred.

The code of that line is then shown, and the error message explains that the subscript used is the issue.

Looking at that, you can't use a string 'value' as an index. use without quotes, which is the variable name.
Reply
#3
(Dec-04-2020, 10:50 PM)Larz60+ Wrote: The code lines shown are the line numbers that were executed prior to receiving the error.
So in Your case, the sequence is: line 89 (main()), then line 85, and finally line 54 where the error actually occurred.

The code of that line is then shown, and the error message explains that the subscript used is the issue.

Looking at that, you can't use a string 'value' as an index. use without quotes, which is the variable name.

thanks for the tip, but it says value is not defined
Reply
#4
try removing ['value'] entirely.
I don't know what that is supposed to be.
this part:MODHASH = soup.find('input',{'name':'uh'})
says to find a tag named input, with a name='uh' attribute.
I can't see the page you're scraping, so it's difficult.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 577 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 466 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Help Running Python Script in Mac OS emojistickers 0 337 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  Trying to make a board with turtle, nothing happens when running script Quascia 3 657 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Python script running under windows over nssm.exe JaroslavZ 0 704 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Running script with subprocess in another directory paul18fr 1 3,701 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Running script on multiple files Afrodizzyjack 1 2,505 May-14-2021, 10:49 PM
Last Post: Yoriz
  Error when running script on startup in Linux NoahTheNerd 0 1,952 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  Refresh data in python script while running in Terminal frankenchrist 4 7,244 Feb-03-2021, 09:54 AM
Last Post: Larz60+
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,431 Jan-16-2021, 10:20 PM
Last Post: Miletkir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020