Variable being sent as blank in message - python

I'm trying to write a previous messages bot for (he)xchat. Saving and printing the messages is fine, but when it sends it doesn't retain the message.
I've tried all sorts of methods to fix it but it's always blank.
Problem area:
for y in range(0, 14):
if msg_last[y]:
msg_to_say = 'privmsg ' + triggernick + ' <previously_bot> '
msg_to_say = msg_to_say + msg_last[y]
print (msg_to_say)
destination.command(msg_to_say)
Inside of Xchat it'll print:
privmsg <nick> <previously_bot> $msg
but upon sending it it'll be:
privmsg <nick> <previously_bot>
Can anyone spot what I've missed?
Full code:
from random import randint
import xchat
__module_name__ = "Blake's Previous Messages Bot"
__module_version__ = " v0.1 "
__module_description__ = "A previous message bot"
print("\0034", __module_name__, __module_version__, __module_description__, "!\003")
#---Globals---#000000#FFFFFF----------------------------------------------------
msg_last = [str()] * 15
msg_count = 0
def on_chat(word, word_eol, userdata):
global msg_count
global msg_last
msg = word[1]
if msg_count == 15:
#print ("Full, reloading list")
for x in range(0, 14):
msg_last[x + 1] = msg_last[x]
msg_last[0] = msg
msg_count = 0
msg_last[msg_count] = msg
msg_count = msg_count + 1
def on_join(word, word_eol, userdata):
global msg_last
triggernick, triggerchannel, triggerhost = word
destination = xchat.get_context()
print ("Someone joined, pulling up log")
for y in range(0, 14):
if msg_last[y]:
msg_to_say = 'privmsg ' + triggernick + ' <previously_bot> '
msg_to_say = msg_to_say + msg_last[y]
print (msg_to_say)
destination.command(msg_to_say)
return xchat.EAT_ALL
def on_kevin(word, word_eol, userdata):
triggernick, triggerchannel, triggerhost = word
destination = xchat.get_context()
if triggernick == 'TenEighths':
rnd = randint(0,2)
if rnd == 2:
rnd2 = randint(0,3)
rnd_str = heyYouKevin()
destination.command("say " + rnd_str)
#print ("Someone joined, pulling up log")
def heyYouKevin(x):
return {
'0': 'be quiet Kevin',
'1': 'omg TenEighths, will you be quiet already?',
'2': 'Kevin is spouting nonsense',
'3': "be quiet, TenEighths.",
}[x]
xchat.hook_print('Join', on_join)
xchat.hook_print('Channel Message', on_chat)
xchat.hook_print('Channel Message', on_kevin)

If you use the raw irc command PRIVMSG the syntax requires that any strings with spaces start with :. So PRIVMSG nick :This is a long string. The better solution is to just use xchat.command('msg nick ...') since it knows what to do.

Related

Twitter bot not to interact with certain phrases

I'm making a Twitter bot that replies or likes tweets. I want it not to interact with certain phrases. For example: don't interact with tweets that have the phrase ''yellow banana". Here's my code and my attempted solution isn't working. I'm trying to get the tweet text and compare it to the "frases_naocurtir" variable that has phrase that there shouldn't be interaction. It keeps picking up these tweets and interacting.
rc = random.choice(frases)
for tweet in tweepy.Cursor(api.search_tweets, rc).items(1):
time.sleep(random.randint(time_init, time_final))
try:
cstatus = api.get_status(tweet.id, tweet_mode="extended")
text = cstatus.full_text
time.sleep(random.randint(time_init, time_final))
if rc in frases_curtir and text not in frases_naocurtir and vaicurtir != 0:
print('\nFrase buscada curtir: ' + str(rc))
tweet.favorite()
print('\nTweet curtido de: ' + tweet.user.screen_name + '\nTweet ID: ' + str(tweet.id) +
'\nTweet date: ' + str(tweet.created_at.strftime('%d/%m/%Y')))
time.sleep(random.randint(time_init, time_final))
if vairesponder != 0 and vaicurtir == 0:
rc = random.choice(frases_responder)
continue
if vairesponder == 0 and vaicurtir != 0:
rc = random.choice(frases_curtir)
continue
if vairesponder != 0 and vaicurtir != 0:
rc = random.choice(frases)
continue

Table values not getting inserted into mysql database until cntrl+c is entered to close python file in linux

Edited
Q)Can someone help getting the values inserted into mysql database , just confused where place mydb function
Reason :Once I manually enter cntrl+c for .py , then only the values are getting inserted into mysql database
Used in the .py file
here is the complete code , where should i place the mydb function?
Table values not getting inserted into mysql database until cntrl+c is entered to close python file in linux
import os
import re
from builtins import len, Exception
import slack
import logging
from subprocess import check_output
import datetime
import mysql.connector
import time
import json
import requests
#user_threads_info = {}
#thread_ts = ""
#slack.RTMClient.run_on(event='message')
def say_hello(**payload):
try:
##0 get clients and payload
logging.info('msg received')
data = payload['data']
web_client = payload['web_client']
rtm_client = payload['rtm_client']
##0 - 1 Check if it is the first msg, not replied msg by me
# print(data)
if data.get('text') == None:
logging.info('This msg is my replied msg.')
return False
##0-2 Get channel info
channel_id = data['channel']
thread_ts = data['ts']
global user
user = data['user']
#user_info = get_userinfo(user)
#print(user_info)
msg = data['text']
##1 get scenario submsg
retVal = analysis_msg(msg)
# print(retVal)
response = web_client.users_list()
assert(response['ok'])
user_map = {x['id']: x['name'] for x in response['members']}
global user_name
user_name = user_map[user] if user in user_map else None
print(user_name)
if retVal[0] == False:
retMsg = retVal[1] + "\nI can create the following orders. \n" \
"a) spu - store pickup \n" \
"b) sth - ship to home \n" \
"c) del - delivery \n" \
"d) digitalAsGuest - Digital item \n" \
" \n" \
"Please provide information as mentioned in below example.\n" \
" \n" \
"Example: spu:3646989:sftqa3:AMEX\n" \
"\n" \
"Sample SKUS:\n" \
"spu - [3646989,8862011]\n" \
"sth - [2592015,6140094]\n" \
"del - [5592005,8862011]\n" \
"digitalAsGuest - [2810037,5057400]"
send_msg(web_client, channel_id, thread_ts, user, retMsg)
return False
##2 form cmd
retVal = form_cmd(retVal[1])
print(retVal)
if retVal == False:
return False
##3 execute cmd
# inform the start of test
retMsg = "Creating an order,Please wait for the result."
send_msg(web_client, channel_id, thread_ts, user, retMsg)
global res
try:
res1 = os.popen(retVal).read()
print("Printing result...")
print(res1)
print("end of print")
res = reg_result_new(res1)
if res == False:
print("reg_function failure")
retMsg = "The test order placement failed."
else:
retMsg = "Order Id - " + res['id'] + "\nFirst Name - " + res['firstName'] + "\nLast Name - " + res['lastName'] + "\n PhoneNumber - " + res['dayPhoneNumber'] + "\n Email - " + res['email'] + "\n"
except Exception as ee:
retMsg = "The test scenario has a failure. Please Check the feature file."
## 4 send result to slack
# retMsg = "Order Id - " + res['id'] + "\nFirst Name - " + res['firstName'] + "\nLast Name - " + res['lastName'] + "\n PhoneNumber - " + res['day PhoneNumber'] + "\n Email - " + res['email'] + "\n"
create_result_file(user, res)
send_msg(web_client, channel_id, thread_ts, user, retMsg)
print(retVal)
except Exception as e:
print("error")
logging.critical(str(e))
############################ My handlers ##############################
def create_result_file(user, res):
try:
cur_time = datetime.datetime.now()
file_name = user + str(cur_time.year) + str(cur_time.month) + str(cur_time.day) + str(cur_time.hour) + str(
cur_time.minute) + str(cur_time.second) + '.txt'
file = open(file_name, 'w')
file.write(res)
file.close()
except Exception as e:
print(str(e))
def send_msg(web_client, channel_id, thread_ts,user,mgs):
print("thread_ts value is:"+thread_ts)
web_client.chat_postMessage(
channel=channel_id,
text=f"```Hi <#{user}>! \n " + mgs + "```",
thread_ts=thread_ts
)
#def get_userinfo(user):
# payload = {'token': slack_token, 'user': user}
# r = requests.get('https://slack.com/api/users.info', params=payload)
# print(r.text)
# return json.loads(r.text)["user"]
# error code mgmt.
def error_code(code):
# reserved
print(code)
return [False, code]
# break down msg to the test scenario submsgs
def analysis_msg(msg):
global submsg
submsg = msg.split(":")
for value in submsg:
print(value)
if len(submsg) != 4:
logging.warning("This msg not test scenario")
return error_code("Please check the format")
res = {}
res["feature"] = submsg[0]
res["sku"] = submsg[1]
res["env"] = submsg[2]
res["payment"] = submsg[3]
###check
if validate_sku(res["sku"]) == False:
return error_code("INVALID_SKU \n")
if validate_env(res["env"]) == False:
return error_code("INVALID_ENV \n")
if validate_payment(res["payment"]) == False:
return error_code("INVALID_payment \n")
if check_specialCharacter(res["feature"]) == False:
return error_code("INVALID_PROFILE_WITH_SPECIAL_CHARACTER")
return [True, res]
# form cmd for test bat files ! reserved
def form_cmd(submsg):
cmd = 'sh /home/iptbot/iptautobot/test.sh ' + submsg['env'] + ' ' + submsg['feature'] + ' ' + submsg["sku"] + ' ' + submsg["payment"]
return cmd
#code to print user details
#code to print user details
def reg_result_new(res):
start = 'COP Order Response :'
end = 'isGuestMode'
start_index = res.find(start) + len(start)
res = res[start_index:]
end_index = res.find(end) + 22
global data
data = res[:end_index]
try:
print('Data -> ' + str(data))
data = json.loads(data.strip())
new_data = {}
new_data['id'] = data['id']
new_data['firstName'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['firstName']
new_data['lastName'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['lastName']
new_data['dayPhoneNumber'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['dayPhoneNumber']
new_data['email'] = data['lineItems'][0]['fulfillmentInfo']['storeInfo']['agentInfo']['email']
#new_data['firstName'] = data['paymentInfo']['billingAddressInfo']['firstName']
return new_data
except Exception as e:
print('Here error -> '+str(e))
return False
#def reg_result(res):
# "COP Order Response"
# lines = res.split('\n')
# for line in lines:
# pattern = "COP Order Response*"
# prog = re.compile(pattern)
# result = prog.search(line)
# if result == None:
# continue
# res1 = result.string.split('{')
# if len(res1) < 2:
# continue
# res2 = res1[1].split(',')
# if len(res2) < 2:
# continue
# res3 = res2[0].split(':')
# if len(res3) < 2:
# continue
# return res3[1]
# COP Order Response : {"id":"BBY01-200001878853"
# return False
# return val is Boolean
# True/False
# Input type: String
# for positive integer only
# alternative way: Handle exception for int(d)
def validate_sku(sku_val):
return sku_val.isnumeric()
# input val : string
# return val: Boolean
def validate_env(env_val):
env_list = [
"sftqa1" , "sftqa2" , "sftqa3" , "sftqa4"
]
if env_val in env_list:
return True
else:
return False
def validate_payment(payment_val):
env_payment = [
"AMEX","VISA"
]
if payment_val in env_payment:
return True
else:
return False
# input val : string
# return val: Boolean
def check_specialCharacter(s):
if s == "":
return False
if s.isspace():
return False
return s.isalnum()
slack_token = os.environ["SLACK_API_TOKEN"]
rtm_client = slack.RTMClient(token=slack_token)
rtm_client.start()
#database connction
mydb = mysql.connector.connect(
host="host",
user="user",
passwd="pass",
database="db"
)
mycursor = mydb.cursor()
for value in submsg:
print(value)
fulfilment=submsg[0]
sku=submsg[1]
environment=submsg[2]
payment=submsg[3]
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
orderNumber=data['id']
username=user_name
print(fulfilment)
print(sku)
print(environment)
print(payment)
print(username)
print(orderNumber)
sqlformula = "INSERT INTO orderDetails (fulfilment,sku,environment,payment,orderNumber,date,user) VALUES (%s,%s,%s,%s,%s,%s,%s)"
#order=("sth",3643387,"sftqa2","AMEX")
#mycursor.execute(sqlformula,order)
mycursor.execute(sqlformula,(fulfilment,sku,environment,payment,orderNumber,date,username))
mydb.commit()
mydb.close()
Output
1 sh /home/iptbot/iptautobot/test.sh sftqa3 spu 3646989 AMEX
2 error
3 CRITICAL:root:'user'
4 error
5 CRITICAL:root:'user' // clicking Control+C values get inserted
6 ^CWARNING:slack.rtm.client:Websocket was closed.
7 3646989
8 sftqa3
9 AMEX
10 spu
11 3646989
12 sftqa3
13 AMEX
14 a6002043
15 BBY01-200002091354
You are stuck at this point because rtm_client.start() is a synchronous call.
If you want it to be asynchronous (non-blocking) then you should run:
rtm_client.start(run_async=True)
Here it is good walk-through on how to setup async usage of the library. Also have a look at the method signature for RTMClient to get an idea of how it works.
Here's a good example detailing a lot of what you would need in your case.
Then you will hit your db execution code where you will need to have a while loop to go through the data you want to add to the DB.
I would recommend that you use a Queue for this as it is synchronised and will be easier to manage than a global list which is overwritten on every order. Preferably you could use asyncio.Queue with an example of implementation here
When an order has passed the validation steps add it to the queue. Here is some pseudo code describing the flow with a basic (not asyncio) Queue:
import queue
q = queue.Queue()
def validate_order(order):
valid_order_data = ......
q.put(valid_order_data)
while True:
valid_order = q.get() # Will wait until there is a value on the queue
mycursor.execute(sqlformula, (valid_order))

Identify sending user, Python IRC

So I made a Twitch.tv bot for my own channel, after having fun with it a little bit, I wanted to have some command restricted to some users, and some commands that can say the users name, for example:
Username reply example:
Person1: !tea
PythonBot: Would you like some tea, Person1?
Admin restriction example:
Person1: !ban Person2
PythonBot: I'm sorry, Person1, This command is restricted to admins only.
Ok, So here is the code I'm using (I will be modifying it soon to make it my own)
import socket
import threading
bot_owner = '~Not Today~'
nick = '~Not Today~'
channel = '~Not Today~'
server = 'irc.twitch.tv'
password = '~Not Today~'
queue = 13
irc = socket.socket()
irc.connect((server, 6667))
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n')
def message(msg):
global queue
queue = 5
if queue < 20:
irc.send('PRIVMSG' + channel + ' :' + msg + '\r\n')
else:
print 'Message Deleted'
def queuetimer():
global queue
queue = 0
threading.Timer(30,queuetimer).start()
queuetimer()
while True:
botdata = irc.recv(1204)
botuser = botdata.split(':')[1]
botuser = botuser.split('!')[0]
print botdata
if botdata.find('PING') != -1:
irc.send(botdata.replace('PING', 'PONG'))
if botdata.find('!res') != -1:
irc.send(botdata.replace('!res', '1600x900'))
The twitch IRC raw message is like
:jkm!jkm#jkm.tmi.twitch.tv PRIVMSG #trumpsc :needs Kappa
for above msg, it actually means userjkm at channel trumpsc saying needs Kappa
for your code, the method to get botuser is right, but you don't have the message the user sent, add following code should get the message
botmsg = botdata.split(':')[2]
so you get the message and username, the next step would be handling them.
Here would be some example for your need. For me, I will prepared a adminuserList and commmandList for other command, but I will simplify it here.
def commmanHandler(botuser, botmsg): # botmsg = '!ban user1'
command = botmsg.split(' ')[0] # command = '!ban'
command = command[1:] # command = 'ban'
argu = message.split(' ')[1] # argu = 'user1'
if command not in commmandList:
raise Exception("command not support")
if command == 'ban': # ban command, or using switch
# check if admin
if botuser not in adminList:
raise Exception("I'm sorry, " + botuser + ", This command is restricted to admins only.")
# admin, able to ban
irc.send('PRIVMSG' + channel + ' :' + '.ban ' + argu)
then call this function in your while loop to handle all message you get
try:
commmanHandler(botuser, botmsg)
except Exception, e:
print e
irc.send('PRIVMSG' + channel + ' :' + e)
here would be my solution, and also, don't forget to give the bot moderator privilege.

Python Run Two Codes At Once

not sure if the title makes sense, but is there a simple way for 2 'lines' to be run simultaneously? (Quotation marks used as I'm not sure how to put it)
Anyway, what I am trying to do right now is make a Skype bot with Skype4Py.
I am making a script that does stuff, as the below script suggests. But I am faced with a problem. One part of the script detects command spamming, but I want to make some type of timer that would remove the user from its spam check database after a while. (Did that make sense?). In other words, let's say that the spam tolerance is spamming 6 times. After a user enters a command (eg. !help) maybe 5 times, and stops, for let's say 3 minutes, and does it again 5 times, he won't be banned from using commands.
Currently with this code, if a user, at any time (eg. 3 commands at 4:00pm, 2 command at 4:03pm), the user would be banned, but I don't want that to work like that.
#IMPORTS
import hashlib
import os
import random
import re
import string
import sys
sys.path.append('lib')
import time
import urllib
import Skype4Py
import urllib2
#CONFIG
admin = '...'
adflyKey = '...'
adflyUID = '...'
nick = ''
#SETUP
accessList = []
bannedList = []
safeuserList = []
vwordList = []
bcheck = []
quoteList = []
commandList = []
lock = False
msgcount = 1
#ADF.LY GENERATOR
def adfLY(url):
global adflyKey
global adflyUID
try:
location = 'http://api.adf.ly/api.php?key=' + adflyKey + '&uid=' + adflyUID + '&advert_type=int&domain=adf.ly&url=' + url
link = urllib.urlopen(location).read()
return link
except:
return url
#RANDOM STRING
def getRandom(length):
length = int(length)
charSet = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
randomChar = ''.join(random.sample(charSet,length))
return randomChar
#GET URL TITLE
def getTitle(url, maxRead = 10000):
try:
url = url.lower()
url = url.replace('cmyip', 'google', 1);
website = urllib2.urlopen(url)
title = re.compile('<title>(.+?)</title>')
buffer = ''
while True:
data = website.read(100)
if not data:
return 'Unknown'
buffer += data
match = title.search(buffer)
if match:
return ' '.join(line.strip() for line in match.group(1).strip().split('\n'))
elif len(buffer) >= maxRead:
return 'Unknown'
except:
return 'Unknown'
#IS URL UP
def isUP(url):
try:
results = getTitle('http://downforeveryoneorjustme.com/' + url)
results = results.replace('Is Up -> Check if your website is up or down?', ' is UP.', 1);
results = results.replace('Is Down -> Check if your website is up or down?', ' is DOWN.', 1);
results = results.replace(' -> Huh? Error... - Check if your website is up or down?', ' is INVALID.', 1);
return results
except:
return url + ' is UNKNOWN.'
#MD5 HASH
def md5(word):
md5 = hashlib.md5(word)
return md5.hexdigest()
#UPDATE ACCESS/BANNED LIST
def updateList(list):
global accessList
global bannedList
global vwordList
global commandList
if list == 'access':
accessFile = open('database/access.txt', 'w')
for name in accessList:
accessFile.write(name + '\n')
accessList.sort()
accessFile.close
elif list == 'banned':
bannedFile = open('database/banned.txt', 'w')
for name in bannedList:
bannedFile.write(name + '\n')
bannedList.sort()
bannedFile.close
elif list == 'vword':
vwordFile = open('database/vword.txt', 'w')
for word in vwordList:
vwordFile.write(word + '\n')
vwordList.sort()
vwordFile.close
elif list == 'safeuser':
safeuserFile = open('database/safeuser.txt', 'w')
for word in safeuserFile:
safeuserFile.write(word + '\n')
safeuserFile.sort()
safeuserFile.close
#SKYPE4PY API
def OnAttach(status):
if status == Skype4Py.apiAttachAvailable:
skype.Attach()
return
if status == Skype4Py.apiAttachSuccess:
print('API connected to the Skype process!')
print '------------------------------------------------------------------------------'
return
statusAPI = skype.Convert.AttachmentStatusToText(status)
print 'API '+ statusAPI.lower() + '...'
#deny calls
#AllowedCallTargets = set (['echo123', 'echo223']);
#
#class receive_set:
# def __init__(self):
# pass
# def OnCall(self, call, status):
# print "status is ", status, " Peer is: ", call.PartnerHandle, " Show name is ", call.PartnerDisplayName
# print "length of active calls are ",len(self.skype.ActiveCalls)
# inprogress = False
# if (status == Skype4Py.clsRinging) and (call.Type == Skype4Py.cltIncomingP2P or call.Type == Skype4Py.cltIncomingPSTN):
# for curr in self.skype.ActiveCalls:
# print "Call status is ", curr.Type, " status is ", curr.Status
# if curr.Status == Skype4Py.clsInProgress :
# inprogress = True
# if not inprogress:
# call.Answer()
# if (status == Skype4Py.clsInProgress):
# print "Call's video send status is ",call.VideoSendStatus, " Recv status is ", call.VideoReceiveStatus, " Video Status is ",call.VideoStatus
## cmd = self.skype.Command("ALTER CALL <id> START_VIDEO_SEND")
## self.skype.SendCommand(cmd)
#
## if (status == "ROUTING") and (not call.PartnerHandle in AllowedCallTargets):
# call.Finish()
# print 'Terminating call'
#
# def OnCallVideoReceiveStatusChanged(self, status):
# pass
#
# def OnCallVideoSendStatusChanged(self, status):
# pass
#
# def OnCallVideoStatusChanged(self, status):
# pass
#
# def OnAttach(self, status):
# print 'API attachment status:'+self.skype.Convert.AttachmentStatusToText(status)
# if status == Skype4Py.apiAttachAvailable:
# self.skype.Attach()
#
# def start(self):
# self.skype = Skype4Py.Skype()
# self.skype.OnAttachmentStatus = self.OnAttach
# self.skype.OnCallStatus = self.OnCall
#
#
# def Attach(self):
# self.skype.Attach()
#
# def Callout(self, callee):
# self.skype.PlaceCall(callee)
#
#
#if __name__ == "__main__":
# rec = receive_set()
# rec.start()
# rec.Attach()
#
# while 1:
# time.sleep(1)
#COMMANDS
def OnMessageStatus(Message, Status):
global admin
global nick
global lock
global accessList
global bannedList
global safewordList
global commandList
global bcheck
global vwordList
global quoteList
global msgcount
try:
msg = Message.Body
chat = Message.Chat
send = chat.SendMessage
senderDisplay = Message.FromDisplayName
senderHandle = Message.FromHandle
message = ''
if lock == True:
if Status == 'SENT' or (Status == 'RECEIVED' and senderHandle in accessList):
if msg == '!unlock':
lock = False
send(nick + ' Unlocked!');
if lock == False:
if Status == 'RECEIVED' and senderHandle not in bannedList:
msgcount = msgcount + 1
if msgcount == 30:
option = random.randint(1, 3)
time.sleep(3)
if option == 1:
send('Type "!info" or "!help" for common information and help.');
elif option == 2:
send(nick);
elif option == 3:
send('');
msgcount = 1
messageTemp = msg.split()
n = 0
did_it_work = False
if msg.startswith('!'):
for x in commandList:
if messageTemp[0] == x:
did_it_work = True
if did_it_work == True:
print('[NOTICE] '+ senderDisplay +' ('+senderHandle+') issued command: '+"'"+msg+"'")
if senderHandle not in safeuserList:
for x in bcheck:
if x == senderHandle:
n += 1
if n == 9: #<--- ###Trigger word is 1 above the number### aka the 10th command is the trigger.
send(nick + senderDisplay + ', you are now banned from using commands due to flooding!');
bannedList.append(senderHandle)
updateList('banned')
while n < 0:
bcheck.remove(senderHandle)
n -= 1
else:
bcheck.append(senderHandle)
n = 0
else:
n = 0
if msg.lower().startswith(admin):
print '\a'
if Status == 'SENT' or (Status == 'RECEIVED' and senderHandle not in bannedList):
### # if msg in vwordList :
### # send.can;
### # print 'A'
if msg == '!help':
helpFile = open('help.txt','r')
for line in helpFile.readlines():
message = message + nick + line
send(message);
if msg == '!info':
infoFile = open('info.txt','r')
for line in infoFile.readlines():
message = message + nick + line
send(message);
if msg.startswith('!isup '):
url = msg.replace('!isup ', '', 1);
send(nick + isUP(url));
if msg.startswith('!md5 '):
word = msg.replace('!md5 ', '', 1);
send(nick + 'MD5 Hash : ' + md5(word));
if msg.startswith('!os '):
if senderHandle in safeuserList:
command = msg.replace('!os ', '', 1);
os.system(command);
if msg.startswith('!topic '):
topic = msg.replace('!topic ', '', 1);
# Message.Body = 'Changing topic name to...'
send("[NOTICE] Changing topic by user's request");
send('/topic ' + topic);
if Status == 'SENT' or (Status == 'RECEIVED' and senderHandle in accessList):
if msg.startswith('!access '):
if msg.startswith('!access add '):
name = msg.replace('!access add ', '', 1);
if name in accessList:
send(nick + 'User [' + name + '] already has access!');
elif name not in accessList:
accessList.append(nick)
accessList.sort()
updateList('access')
send(nick + 'User [' + name + '] has gained access!');
elif msg.startswith('!access list'):
name = msg.replace('!access list ', '', 1);
for name in accessList:
message = message + nick + name + '\n'
send(message);
elif msg.startswith('!access remove '):
name = msg.replace('!access remove ', '', 1);
if name in accessList:
accessList.remove(name)
accessList.sort()
updateList('access')
send(nick + 'User [' + name + '] has lost access!');
elif nick not in accessList:
send(nick + 'User [' + name + '] has no access!');
if msg.startswith('!vword '):
if msg.startswith('!vword add '):
name = msg.replace('!vword add ', '', 1);
if name in vwordList:
send('Word Already Stored!');
elif name not in vwordList:
vwordList.append(nick)
vwordList.sort()
updateList('vword')
send('Word Stored');
elif msg.startswith('!vword list'):
name = msg.replace('!vword list ', '', 1);
send('Please refer to the vword.txt');
if msg.startswith('!ban '):
if msg.startswith('!ban add '):
name = msg.replace('!ban add ', '', 1);
if name in bannedList:
send(nick + 'User [' + name + '] is already banned!');
elif name not in bannedList:
bannedList.append(nick)
bannedList.sort()
updateList('banned')
send(nick + 'User [' + name + '] has been banned!');
elif msg.startswith('!ban list'):
name = msg.replace('!ban list ', '', 1);
for name in bannedList:
message = message + nick + name + '\n'
send(message);
elif msg.startswith('!ban remove '):
name = msg.replace('!ban remove ', '', 1);
if name in bannedList:
bannedList.remove(name)
bannedList.sort()
updateList('banned')
send(nick + 'User [' + name + '] has been unbanned!');
elif nick not in bannedList:
send(nick + 'User [' + name + '] is not banned!');
# if msg.contains('youtube.com/watch?v='):
# for friend in skype.Friends:
# if not friend.OnlineStatus == Skype4Py.olsOffline:
# try:
# skype.CreateChatWith(friend.Handle).SendMessage(nick + '[GLOBAL MESSAGE]\r\n' + nick + message)
# except:
# print '[ERROR] ' + str(sys.exc_info()[1])
if msg.startswith('!global '):
message = msg.replace('!global ', '', 1);
for friend in skype.Friends:
if not friend.OnlineStatus == Skype4Py.olsOffline:
try:
skype.CreateChatWith(friend.Handle).SendMessage(nick + '[GLOBAL MESSAGE]\r\n' + nick + message)
except:
print '[ERROR] ' + str(sys.exc_info()[1])
if msg == '!lock':
lock = True
send(nick);
# if msg == '!party':
# send('/topic PARTY HARD!');
# for friend in skype.Friends:
# if not friend.OnlineStatus == Skype4Py.olsOffline:
# try:
# send('/add ' + friend.Handle);
# except:
# print '[ERROR] ' + str(sys.exc_info()[1])
if msg == '!restart':
os.system('python restart.py');
sys.exit();
except:
send(nick + '[ERROR] ' + str(sys.exc_info()[1]));
#START INSTANCE
import os
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
print '******************************************************************************'
infoFile = open('info.txt','r')
for line in infoFile.readlines():
print '- ' + line.replace('\n', '')
print 'Checking for Skype4Py API...'
try:
import Skype4Py
skype = Skype4Py.Skype();
skype.OnAttachmentStatus = OnAttach
skype.OnMessageStatus = OnMessageStatus
skype.FriendlyName = ''
print 'Skype4Py API found!'
except:
print 'Failed to locate Skype4Py API! Quitting...'
print '******************************************************************************'
sys.exit()
print 'Checking for Skype process...'
if skype.Client.IsRunning:
print 'Skype process found!'
elif not skype.Client.IsRunning:
print 'Skype process not found!'
try:
print 'Starting Skype process...'
skype.Client.Start()
except:
print 'Failed to start Skype process! Quitting...'
print '******************************************************************************'
sys.exit()
print 'Connecting API to Skype...'
try:
skype.Attach();
except:
print 'Failed to connect API to Skype! Quitting...'
print '******************************************************************************'
sys.exit()
print 'Loading access list...'
accessFile = open('database/access.txt','r')
for line in accessFile.readlines():
name = line.replace('\n', '');
accessList.append(name)
accessList.sort()
accessFile.close()
print 'Access list contains ' + str(len(accessList)) + ' names!'
print 'Loading banned list...'
bannedFile = open('database/banned.txt','r')
for line in bannedFile.readlines():
name = line.replace('\n', '');
bannedList.append(name)
bannedList.sort()
bannedFile.close()
print 'Banned list contains ' + str(len(bannedList)) + ' names!'
print 'Loading VWORD list...'
vwordFile = open('database/vword.txt','r')
for line in vwordFile.readlines():
name = line.replace('\n', '');
vwordList.append(name)
vwordList.sort()
vwordFile.close()
print 'VWORD list contains ' + str(len(vwordList)) + ' words!'
print 'Loading quote list...'
quoteFile = open('database/quote.txt','r')
for line in quoteFile.readlines():
quote = line.replace('\n', '');
quoteList.append(quote)
quoteList.sort()
quoteFile.close()
print 'Quote list contains ' + str(len(quoteList)) + ' quotes!'
print 'Loading safe user list...'
safeuserFile = open('database/safeuser.txt','r')
for line in safeuserFile.readlines():
safeuser = line.replace('\n', '');
safeuserList.append(safeuser)
safeuserList.sort()
safeuserFile.close()
print 'SafeUser list contains ' + str(len(safeuserList)) + ' names!'
print 'Loading command list...'
commandFile = open('database/commands.txt','r')
for line in commandFile.readlines():
command = line.replace('\n', '');
commandList.append(command)
commandList.sort()
commandFile.close()
print 'Command list contains ' + str(len(commandList)) + ' commands!'
print '******************************************************************************'
#ENDLESS LOOP
while True:
raw_input('');
I was going to put some sort of code like this:
(with time already imported)
timer=180
while timer >0:
time.sleep(1)
timer -=1
But I don't know where to place it, or how
Any type of help will be appreciated.
Thanks!
EDIT: Changed last line to:
timer=16
while timer >0:
time.sleep(1)
timer -=1
if timer == 12:
ccheck = bcheck
ccheck.reverse()
dcheck = len(ccheck)
while dcheck !=0:
for x in ccheck:
if x == ccheck[0]:
bcheck.remove(x)
ccheck = []
#raw_input('');
http://docs.python.org/2/library/os.html
os.fork might be of use
pid = os.fork()
if pid == 0:
print("I am the child!")
else:
print("I am the parent!")
The child and parent should run at the same time as they are now 2 different processes.
When you are checking whether you should ban the user, you can check the time of the last post and reset the count if it is past a certain time. You will know better then I how to do this in your code. You shouldn't need any kind of concurrency to do what you want.

Asynchronous data through Bloomberg's new data API (COM v3) with Python?

Does anyone know how to get asynchronous data through Bloomberg's new data API (COM v3) with Python? I found this code below on wilmott.com and it works just fine, but it's for the old API version.
Does anyone know the corresponding code for the new version?
from win32com.client import DispatchWithEvents
from pythoncom import PumpWaitingMessages, Empty, Missing
from time import time
class BBCommEvent:
def OnData(self, Security, cookie, Fields, Data, Status):
print 'OnData: ' + `Data`
def OnStatus(self, Status, SubStatus, StatusDescription):
print 'OnStatus'
class TestAsync:
def __init__(self):
clsid = '{F2303261-4969-11D1-B305-00805F815CBF}'
progid = 'Bloomberg.Data.1'
print 'connecting to BBComm'
blp = DispatchWithEvents(clsid, BBCommEvent)
blp.AutoRelease = False
blp.Subscribe('EUR Curncy', 1, 'LAST_PRICE', Results = Empty)
blp.Flush()
end_time = time() + 5
while 1:
PumpWaitingMessages()
if end_time < time():
print 'timed out'
break
if __name__ == "__main__":
ta = TestAsync()
I finally figured it out. I did a fair bit of combrowse.py detective work, and I compared with the JAVA, C, C++, and .NET examples in the BBG API download. Interestingly enough the Bloomberg Helpdesk people knew pretty much null when it came to these things, or perhaps I was just talking to the wrong person.
Here is my code.
asynchronousHandler.py:
import win32com.client
from pythoncom import PumpWaitingMessages
from time import time, strftime
import constants
class EventHandler:
def OnProcessEvent(self, result):
event = win32com.client.gencache.EnsureDispatch(result)
if event.EventType == constants.SUBSCRIPTION_DATA:
self.getData(event)
elif event.EventType == constants.SUBSCRIPTION_STATUS:
self.getStatus(event)
else:
self.getMisc(event)
def getData(self, event):
iterator = event.CreateMessageIterator()
while iterator.Next():
message = iterator.Message
dataString = ''
for fieldIndex, field in enumerate(constants.fields):
if message.AsElement.HasElement(field):
element = message.GetElement(field)
if element.IsNull:
theValue = ''
else:
theValue = ', Value: ' + str(element.Value)
dataString = dataString + ', (Type: ' + element.Name + theValue + ')'
print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + dataString
def getMisc(self, event):
iterator = event.CreateMessageIterator()
while iterator.Next():
message = iterator.Message
print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString
def getStatus(self, event):
iterator = event.CreateMessageIterator()
while iterator.Next():
message = iterator.Message
if message.AsElement.HasElement('reason'):
element = message.AsElement.GetElement('reason')
print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + ', Category: ' + element.GetElement('category').Value + ', Description: ' + element.GetElement('description').Value
if message.AsElement.HasElement('exceptions'):
element = message.AsElement.GetElement('exceptions')
exceptionString = ''
for n in range(element.NumValues):
exceptionInfo = element.GetValue(n)
fieldId = exceptionInfo.GetElement('fieldId')
reason = exceptionInfo.GetElement('reason')
exceptionString = exceptionString + ', (Field: ' + fieldId.Value + ', Category: ' + reason.GetElement('category').Value + ', Description: ' + reason.GetElement('description').Value + ') '
print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + exceptionString
class bloombergSource:
def __init__(self):
session = win32com.client.DispatchWithEvents('blpapicom.Session' , EventHandler)
session.Start()
started = session.OpenService('//blp/mktdata')
subscriptions = session.CreateSubscriptionList()
for tickerIndex, ticker in enumerate(constants.tickers):
if len(constants.interval) > 0:
subscriptions.AddEx(ticker, constants.fields, constants.interval, session.CreateCorrelationId(tickerIndex))
else:
subscriptions.Add(ticker, constants.fields, session.CreateCorrelationId(tickerIndex))
session.Subscribe(subscriptions)
endTime = time() + 2
while True:
PumpWaitingMessages()
if endTime < time():
break
if __name__ == "__main__":
aBloombergSource = bloombergSource()
constants.py:
ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
fields = ['BID']
tickers = ['AUD Curncy']
interval = '' #'interval=5.0'
For historical data I used this simple script:
import win32com.client
session = win32com.client.Dispatch('blpapicom.Session')
session.QueueEvents = True
session.Start()
started = session.OpenService('//blp/refdata')
dataService = session.GetService('//blp/refdata')
request = dataService.CreateRequest('HistoricalDataRequest')
request.GetElement('securities').AppendValue('5 HK Equity')
request.GetElement('fields').AppendValue('PX_LAST')
request.Set('periodicitySelection', 'DAILY')
request.Set('startDate', '20090119')
request.Set('endDate', '20090130')
cid = session.SendRequest(request)
ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
stayHere = True
while stayHere:
event = session.NextEvent();
if event.EventType == PARTIAL_RESPONSE or event.EventType == RESPONSE:
iterator = event.CreateMessageIterator()
iterator.Next()
message = iterator.Message
securityData = message.GetElement('securityData')
securityName = securityData.GetElement('security')
fieldData = securityData.GetElement('fieldData')
returnList = [[0 for col in range(fieldData.GetValue(row).NumValues+1)] for row in range(fieldData.NumValues)]
for row in range(fieldData.NumValues):
rowField = fieldData.GetValue(row)
for col in range(rowField.NumValues+1):
colField = rowField.GetElement(col)
returnList[row][col] = colField.Value
stayHere = False
break
element = None
iterator = None
message = None
event = None
session = None
print returnList
For it to work you need to install Bloomberg Desktop v3 API SDK, I did that, restarted my machine, seems to work. Without the restart it just crashed.
If you use Com explorer, you will see the bloomberg elements are now present

Categories