IThelp
IThelp

Reputation: 57

Sending json data over irc (python3)

In this project I am trying to send json data to a specific irc channel. The irc bot is supposed to do two things at a time, check for a certain message, and send the json data (Sorry for repeating json data so often :).)

I created a class, for the function which, searches on a website for the data it is supposed to send over irc (search.py):

import re
import json
import requests


class search:



    def run():

        data = requests.get("http://boards.4chan.org/g/catalog").text
        match = re.match(".*var catalog = (?P<catalog>\{.*\});.*", data)

        if not match:
            print("Couldn't scrape catalog")
            exit(1)

        catalog = json.loads(match.group('catalog'))

        running = True
        while running:

                try:
                    filtertext = ("tox")
                    for number, thread in catalog['threads'].items():
                        sub, teaser = thread['sub'], thread['teaser']
                        if filtertext in sub.lower() or filtertext in teaser.lower():
                            #liste.append(teaser)
                            #return(teaser)

                            #print(liste[0])
                            return(teaser)
                            running = False

The file which is supposed to send the data to the data to the irc channel (irctest.py):

import socket,threading,time
from search import search

# Some basic variables used to configure the bot        
server = b"irc.freenode.net" # Server
channel = b"#volafile" # Channel
botnick = b"Mybot" # Your bots nick
troo = True

def ping(): # This is our first function! It will respond to server Pings.
  ircsock.send(b"PONG :pingis\n")  

def sendmsg(chan , msg): # This is the send message function, it simply sends messages  to the channel.
  ircsock.send(b"PRIVMSG "+ chan +b" :"+ msg +b"\n") 

def joinchan(chan): # This function is used to join channels.
  ircsock.send(b"JOIN "+ chan + b"\n")

def worker():
  print(threading.currentThread().getName(), 'Starting')
  while True:
      #liste3= search.run()
      #teaser1 = str(search.teaser)
      ircsock.send(b"PRIVMSG "+ channel + b" :"+ search.run() + b"\n")

  print(threading.currentThread().getName(), 'Exiting')


ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))

ircsock.send(b"USER "+ botnick + b" "+ botnick + b" "+ botnick + b" :This bot is a   result of a tutoral covered on http://shellium.org/wiki.\n")
ircsock.send(b"NICK "+ botnick + b"\n")

joinchan(channel) # Join the channel using the functions we previo

time.sleep(10)

w = threading.Thread(name='worker', target=worker) 
i=0
w.start()
while 1: # Be careful with these! it might send you to an infinite loop
  ircmsg = ircsock.recv(2048) # receive data from the server
  ircmsg = ircmsg.strip(b'\n\r') # removing any unnecessary linebreaks.
  print(ircmsg) # Here we print what's coming from the server

  if ircmsg.find(b":Hello "+ botnick) != -1: # If we can find "Hello Mybot" it will call the function hello()
    hello()

  if ircmsg.find(b"PING :") != -1: # if the server pings us then we've got to respond!
    ping()

but I get this error message:

        Exception in thread worker:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.4/threading.py", line 868, in run
    self._target(*self._args, **self._kwargs)
  File "irctest.py", line 24, in worker
    ircsock.send(b"PRIVMSG "+ channel + b" :"+ bytes(search.run()) + b"\n"\
TypeError: string argument without an encoding

Upvotes: 0

Views: 479

Answers (1)

miindlek
miindlek

Reputation: 3563

When you call ircsock.send() in your function worker(), your last string isn't a bytestring ("\n"). You should also cast the search.run() return value to a bytestring with bytes(search.run(), "utf-8"). Change the specific line to:

ircsock.send(b"PRIVMSG "+ channel + b" :"+ bytes(search.run(), "utf-8") + b"\n")

Upvotes: 1

Related Questions