Sending json data over irc (python3) - python

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

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")

Related

Use Python and bleak library to notify a bluetooth GATT device, but the result is not stable

I try to use Python to control some BLE GATT devices.
I find Bleak (https://bleak.readthedocs.io/en/latest/) this library to communicate with GATT device.
When I use it, most of the time it works really well.
But once I try to communicate with a BLE GATT medical equipment, I found I can't always notify with this equipment.
Here is my code, I just do a little changes from Bleak's github example:(https://github.com/hbldh/bleak/blob/develop/examples/enable_notifications.py)
import asyncio
import logging
from bleak import discover
from bleak import BleakClient
devices_dict = {}
devices_list = []
receive_data = []
#To discover BLE devices nearby
async def scan():
dev = await discover()
for i in range(0,len(dev)):
#Print the devices discovered
print("[" + str(i) + "]" + dev[i].address,dev[i].name,dev[i].metadata["uuids"])
#Put devices information into list
devices_dict[dev[i].address] = []
devices_dict[dev[i].address].append(dev[i].name)
devices_dict[dev[i].address].append(dev[i].metadata["uuids"])
devices_list.append(dev[i].address)
#An easy notify function, just print the recieve data
def notification_handler(sender, data):
print(', '.join('{:02x}'.format(x) for x in data))
async def run(address, debug=False):
log = logging.getLogger(__name__)
if debug:
import sys
log.setLevel(logging.DEBUG)
h = logging.StreamHandler(sys.stdout)
h.setLevel(logging.DEBUG)
log.addHandler(h)
async with BleakClient(address) as client:
x = await client.is_connected()
log.info("Connected: {0}".format(x))
for service in client.services:
log.info("[Service] {0}: {1}".format(service.uuid, service.description))
for char in service.characteristics:
if "read" in char.properties:
try:
value = bytes(await client.read_gatt_char(char.uuid))
except Exception as e:
value = str(e).encode()
else:
value = None
log.info(
"\t[Characteristic] {0}: (Handle: {1}) ({2}) | Name: {3}, Value: {4} ".format(
char.uuid,
char.handle,
",".join(char.properties),
char.description,
value,
)
)
for descriptor in char.descriptors:
value = await client.read_gatt_descriptor(descriptor.handle)
log.info(
"\t\t[Descriptor] {0}: (Handle: {1}) | Value: {2} ".format(
descriptor.uuid, descriptor.handle, bytes(value)
)
)
#Characteristic uuid
CHARACTERISTIC_UUID = "put your characteristic uuid"
await client.start_notify(CHARACTERISTIC_UUID, notification_handler)
await asyncio.sleep(5.0)
await client.stop_notify(CHARACTERISTIC_UUID)
if __name__ == "__main__":
print("Scanning for peripherals...")
#Build an event loop
loop = asyncio.get_event_loop()
#Run the discover event
loop.run_until_complete(scan())
#let user chose the device
index = input('please select device from 0 to ' + str(len(devices_list)) + ":")
index = int(index)
address = devices_list[index]
print("Address is " + address)
#Run notify event
loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(run(address, True))
In most situation, this code can work well as this image.
But sometimes (about 25% ratio), this problem just happened:
[0]08:6B:D7:12:F1:33 Nonin3150_502892837['uuid']
[1]1D:BD:4A:69:8B:AB Unknown []
[2]73:15:CD:47:AF:08 Unknown []
[3]40:4E:36:5B:8D:1B HTC BS 1BBDB9 ['uuid']
[4]6B:FB:E5:DD:7F:4E Unknown []
[5]69:A7:87:23:5C:7C Unknown []
please select device from 0 to 6:0
Address is 08:6B:D7:12:F1:33
Traceback (most recent call last):
File "D:/Bletest/nonin_test.py", line 91, in <module>
loop.run_until_complete(run(address, True))
File "C:\Users\rizal\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "D:/Bletest/nonin_test.py", line 36, in run
async with BleakClient(address) as client:
File "C:\Users\rizal\AppData\Local\Programs\Python\Python37\lib\site-packages\bleak\backends\client.py", line 60, in __aenter__
await self.connect()
File "C:\Users\rizal\AppData\Local\Programs\Python\Python37\lib\site-packages\bleak\backends\dotnet\client.py", line 154, in connect
"Device with address {0} was not found.".format(self.address)
bleak.exc.BleakError: Device with address 08:6B:D7:12:F1:33 was not found.
Process finished with exit code 1
I have no idea why the program can discover this equipment, but can't notify with it.
Is this my code's problem, or it may caused by this equipment's program flow?
I do have the same problem. However, I think it has something to do with the implementation of BLE in Windows. When you scan for devices in the Windows interface you are sometimes able to see how devices appear and dissapear. The devices Advertising Interval could be to long.
However, it is fixable by wrapping it in a try catch section.
Something like this could work for you.
async def connect_to_device(self):
while True:
if self.connection_enabled:
try:
await self.client.connect()
self.connected = await self.client.is_connected()
if self.connected:
print("Connected to Device")
self.client.set_disconnected_callback(self.on_disconnect)
await self.client.start_notify(
self.notify_characteristic, self.notify_callback,
)
while True:
if not self.connected:
break
await asyncio.sleep(1.0)
else:
print(f"Failed to connect to Device")
except Exception as e:
print(e)
else:
await asyncio.sleep(1.0)
You just need to add this task to the loop
asyncio.ensure_future(self.connect_to_device(), loop)
define the client
self.client = BleakClient(self.connected_device.address, loop=loop)
and enable the connection
self.connection_enabled = true

Python string split() method causes index error when reading IRC

I'm creating a twitch chat bot to read the chat on my stream. But when I try to .split() the incoming string into separate strings to isolate the username and message, it displays an extra ' and ["'"]. when I try to print the strings separately by index I get an index error.
Following is the code which connects the the twitch chat fine, and the result when I type "test" into the chat.
from settings import *
import socket
import threading
class twitch:
def __init__(self, host, port, nick, pwd, channel):
self.s = socket.socket()
self.s.connect((host, port))
self.s.send(bytes("PASS " + pwd + "\r\n", "UTF-8"))
self.s.send(bytes("NICK " + nick + "\r\n", "UTF-8"))
self.s.send(bytes("JOIN #" + channel + " \r\n", "UTF-8"))
self.s.send(bytes("PRIVMSG #" + channel + " :" + "Connected " + "\r\n", "UTF-8"))
self.alive = True
readerthread = threading.Thread(target=self.read_chat)
readerthread.start()
def read_chat(self):
while self.alive:
for line in str(self.s.recv(1024)).split('\\r\\n'):
if "PING :tmi.twitch.tv" in line:
print(time.strftime("%H:%M:%S"), "PONG :tmi.twitch.tv")
s.send(bytes("PONG :tmi.twitch.tv\r\n", "UTF-8"))
else:
print(line)
parts = line.split(":")
print(parts)
def main():
tc = twitch(HOST, PORT, NICK, PASS, CHANNEL)
Printing the string (line) to the console produces: b':username!username#username.tmi.twitch.tv PRIVMSG #username :test
However when I split the string and print the list of strings (parts) it produces this:
["b'", 'username!username#username.tmi.twitch.tv PRIVMSG #username ', 'test']
'
["'"]
You are reading bytes. Hence the b'...'.
What does the 'b' character do in front of a string literal?
Convert it to a string and then handle it.
Convert bytes to a string?
code from the link.
>>> b"abcde"
b'abcde'
# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8")
'abcde'

Threading-related issue - Python 3 (Discord bot)

I tried to look up this issue, but it seems like I've done something I can no longer look up on my own, so I'll need your guys' help. This will probably be a rather difficult one, but I really hope that someone will help me with this one!
The code: (Apologies for the messy code beforehand)
import json, requests, threading, discord, math, random
from requests import adapters
from requests_testadapter import Resp
from discord.ext.commands import Bot
TOKEN = 'SOMETOKENIDTHATICANTREVEALANDYOUKNOWWHY'
BOT_PREFIX = ('!')
url_api = 'http://api.lambdawars.com/player/matches/list/{}' # input the
SteamID64
client = Bot(command_prefix=BOT_PREFIX)
#client.async_event
class LocalFileAdapter(requests.adapters.HTTPAdapter):
def build_response_from_file(self,request,steamid):
file_path = request.url_api.format(steamid)[7:]
with open(file_path, 'rb') as file:
buff = bytearray(file.read())
resp = Resp(buff)
r = self.build_response(request, resp)
return r
def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
return self.build_response_from_file(request)
requests_session = requests.session()
requests_session.mount('file://', LocalFileAdapter())
old_response = ''
monitor_ids = []
#client.command(name='addID')
async def monitoring(steamid):
global old_response
monitor_ids.append(steamid)
while True:
await client.say("Loading results for: " + steamid + "...")
print('Loading results for: ' + steamid)
url_new = url_api
response = requests_session.get(url_new.format(steamid))
if response != old_response:
old_response = response
try:
global idresponse
idresponse = str('-\nPlayer ID {} **' + response.json()['matches'][0]['end_state'] + "** on **" +
response.json()['matches'][0]['start_date'] + "** on map **" + response.json()['matches'][0][
'map'] + "**. \nGame mode: **" + response.json()['matches'][0]['mode'] + "**, type: **" +
response.json()['matches'][0]['type'] + "**. \nThe match lasted **" + str(
math.floor(float(response.json()['matches'][0]['duration']) / 60)) + "** minutes").format(id)
except TypeError:
print("duration type error - bust be a number")
# await client.say(' | '.join(god_damnit_sandern))
await client.say(random.choice["God damnit, Sandern! That match failed to be recorded properly. " + '<:hmm:453711378067226630>',
"God damnit, Sandern! He stole your match data!" + '<:hmm:453711378067226630>',
"God damnit, Sandern! Your match data was not found, it's all his fault!" + '<:hmm:453711378067226630>'])
except KeyError:
print("Wrong SteamID64 number!")
# await client.say('|'.join(god_damnit_sandern))
await client.say("-"
"\nThis (" + steamid + ") isn't SteamID64. <:hardfacepalm:453711378272878592> "
"\nCheck for typos?")
except IndexError:
print("This SteamID64 has no game records.")
await client.say('-'
'\nThis user (' + steamid + ') has never ever played Lambda Wars online yet! <:youserious:453711378687983626>'
'\nWhat a loser!')
await client.say(idresponse)
thread = threading.Thread(target=monitoring)
thread.start()
client.run(TOKEN)
I was working on a Discord bot. The idea was to make a command, namely !addID 76561197995806465 where the ID itself is a SteamID64 for tracking API results on the following page: http://api.lambdawars.com/player/matches/list/76561197995806465
This is basically a JSON result with some of its pieces taken out from the first bit (['matches'][0][{a key, such as "duration" or "start_date"}]).
The plan was to make it automatically get the results, as soon as these results are given, using the following piece in the monitoring method:
if response != old_response:
old_response = response
It does work as a command and makes the Discord bot leave a message when given the SteamID64 and an optional number, such as 1 (0 being default):
LamBOTa Wars: -
Player ID 76561198223952276 lost on Sat, 16 Jun 2018 10:23:49 GMT on map hlw_breakout.
Game mode: destroyhq, type: FFA.
The match lasted 13 minutes
(If you would like to see the code for this one, I'll post it)
So when I run my code, I get the following:
Exception in thread Thread-1:
Traceback (most recent call last):
File "D:\Program Files\Python\Python3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "D:\Program Files\Python\Python3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: 'Command' object is not callable
Help would be really appreciated!
Also before you ask, yes, I am a newb at programming in general. That's my first experience with bots.

python irc bot - not returning full list of channels

I copied an already made irc script and modified it a bit.
# Import some necessary libraries.
import socket
channelList = []
# Some basic variables used to configure the bot
server = "irc.freenode.net" # Server
channel = "#kiwiirc-default" # Channel
botnick = "Mybot" # Your bots nick
def ping(): # This is our first function! It will respond to server Pings.
ircsock.send("PONG :pingis\n")
def sendmsg(chan , msg): # This is the send message function, it simply sends messages to the channel.
ircsock.send("PRIVMSG "+ chan +" :"+ msg +"\n")
def joinchan(chan): # This function is used to join channels.
ircsock.send("JOIN "+ chan +"\n")
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667
ircsock.send("USER "+ botnick +" "+ botnick +" "+ botnick +" :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n") # user authentication
ircsock.send("NICK "+ botnick +"\n") # here we actually assign the nick to the bot
joinchan(channel) # Join the channel using the functions we previously defined
send = True
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('\n\r') # removing any unnecessary linebreaks.
#print ircmsg # Here we print what's coming from the server
if send:
ircsock.send('LIST\r\n')
send = False
x = ircmsg.split()
x = [val for i, val in enumerate(x) if '#' in val]
channelList += x
print len(channelList)
if 'PING :' in ircmsg: # if the server pings us then we've got to respond!
ping()
raw_input()
It sends the LIST to the server and the server will send back the information. I then add it to a channelList and print out the length.
When I type /list into my irc client, it returns "49400 channels found"
The final channelList length should be 49400, but it is always 18,000ish (forgot the exact number)
I'm using https://kiwiirc.com/client

Unindent does not match any outer indentation level

On line 51/52, I get the error:
Unindent does not match any outer indentation level.
I understand this has something to do with tabs and spaces.
Note I did not write this code, I found it online and plan to modify it.
Full code (also at http://pastebin.com/n7ML6Rpz)
import os
import re
import socket
import sys
import time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket
server_socket.bind(("", 9020)) #Bind server to this socket
server_socket.listen(4) #Max number of queued connections
# Welcome message
print ("TCP chat server now awaiting client connection on port 9020...")
chat_log = [] #Contains chat log
time = time.strftime('%l:%M %p %Z on %b %d, %Y') #Server start time formatted nicely
start_time = str(time) #Convert server start time to string
username = "ChatUser" #Default server username if user does not provide one
# Support ~2^x client connections, where x is the number of process forks
os.fork()
os.fork()
os.fork()
# This variable contains the help documentation for the "help" command
chatHelp = ("The chat server accepts the following commands:\n"
+ "adios Closes the program\n"
+ "connection Shows client connection info (IP, port)\n"
+ "get Returns complete chat log\n"
+ "getrange <#> <#> Get chat log entries from <#> to <#> (starts at 1)\n"
+ "help Lists valid commands\n"
+ "name: <text> Sets your username to <text>\n"
+ "test: <text> Echo data back to you <text>\n"
+ "time Shows time when server was initiated\n"
+ "push: <text> Add <text> to chat log\n"
+ "save Save chat log to file\n")
while 1:
# Accept connection
client_socket, address = server_socket.accept()
# Print connection info from client for server log
print ("Received connection from client at"), address
# Used in the connection command function (client request) below
connection = str(address)
# Send welcome string to client
client_socket.send("Welcome to Nigel's chat room! You are logged in as ChatUser.\n Type help for a list of valid commands.\n")
# Loop indefinitely while server running
while 1:
data = client_socket.recv(2048) #Receive client data into buffer
process_data = data.lower() #Lowercase received data for processing
print ("Data received from client>>"), process_data #Print data received from client for log reference
# Functions for the received commands (I use the find library to reduce compatibility errors with other languages)
# ---"adios" command function---
if (process_data.find("adios") == 0):
client_socket.close() #Close socket connection
print ("<Ctrl+C to exit.>>")
break;
# ---"connection:" command function---
elif(process_data.find("connection") == 0):
client_socket.send("Client connection info: " + connection + "\n")
print "User requested connection information"
# ---"getrange" command function w/ regular expression filtering (must be BEFORE "get" command function)---
elif(re.match(r'getrange\s+(\d+)\s+(\d+)',process_data)): # Regex to find correct match with dynamic numbers input
match = re.match(r'getrange\s+(\d+)\s+(\d+)',process_data)
getValue = "Chat log from range "+ match.group(1) + " and " + match.group(2) + ":\n" # Grab first and second range number provided by client
if(len(chat_log) >= int(match.group(1)) and len(chat_log) >= int(match.group(2))): # Check to see if chat log extends to given range
count = int(match.group(1)) - 1
while(count < int(match.group(2))):
getValue += chat_log[count] + "\n"
count += 1
else:
getValue += "<>\n" #No data in range provided by client
client_socket.send(getValue) #Send results to client
# ---"get" command function---
elif(process_data.find("get") == 0):
log = "Chat log: \n"
for item in chat_log:
log += item+" \n"
client_socket.send(log)
# ---"help:" command function---
elif(process_data.find("help") == 0):
client_socket.send(chatHelp + "\n")
print "User requested help"
# ---"name:" command function---
elif(process_data.find("name:") == 0):
username = data[5:].strip() #Only grab the value client set (not "name:")
client_socket.send("Username set to: " + data[5:] + "\n")
# ---"test:" command function---
elif(process_data.find("test:") == 0):
client_socket.send(data[5:]+"\n") #Echo last 5 elements to client
print data
# ---"time" command function---
elif(process_data.find("time") == 0):
client_socket.send("Chat server was started at: " + start_time + "\n")
print "User requested server start time"
# ---"save" command function---
elif(process_data.find("save") == 0):
print "(Saving chat log to file)"
client_socket.send("Saving chat log to file..." + "\n")
filename = "chat.log"
file = open(filename,"w") #Create file
for item in chat_log: #Loop through elements in chat_log
file.write("%s\n" % item) #Write elements one by one on a new line
file.close() #Close/write file
# ---"push" command function---
elif(process_data.find("push:") == 0):
print "(Pushing data to chat log)"
if(username != ""):
chat_log.append(username + ": " + data[5:].strip()) #Save actual chat text to log (not "push:")
else:
chat_log.append(data[5:].strip())
client_socket.send("OK\n")
else:
print "<<Unknown Data Received>>",data #Server log
try:
client_socket.send("Unrecognized command: " + data + "") #Advise client of invalid command
except socket.error, e:
print "<<Ctrl+C to exit>>" #Server log
break;
Python code is sensitive to the indent level you use. Your code reads:
if (process_data.find("adios") == 0):
client_socket.close() #Close socket connection
print ("<Ctrl+C to exit.>>")
break;
Inside the if block, the statements must all line up. Notice how client_socket.close() and the following print statement have different indent levels. You need to change this so that they both have the same indent, like this:
if (process_data.find("adios") == 0):
client_socket.close() #Close socket connection
print ("<Ctrl+C to exit.>>")
break;
The code presently reads:
if (process_data.find("adios") == 0):
client_socket.close() #Close socket connection
print ("<Ctrl+C to exit.>>")
break;
The first statement in the body of the if is indented 6 spaces, while the last two statements are only indented by 1 space. The indentation level ought to be same and consistent throughout the program, something like this:
if (process_data.find("adios") == 0):
client_socket.close() #Close socket connection
print ("<Ctrl+C to exit.>>")
break;
The indentation in the code doesn't seem very standard (or consistent). For instance the body of the while loop in 41/42 is indented more than other bodies of similar statements, e.g., lines 31-33, that's trouble in a language like Python where whitespace matters.
Finally, note it's not a good idea to mix tabs and spaces. PEP 8 -- Style Guide for Python Code recommends the use of spaces over tabs and an indentation of 4 spaces per level.

Categories