Asynchronous data through Bloomberg's new data API (COM v3) with Python? - 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

Related

Why are my Spotify requests so high using Spotipy?

I have a script that takes an artist, finds related artists, and then adds their top tracks to a list. Next the script iterates through the list to find songs under a popularity threshold. If the songs are under the threshold then they will be added to a new playlist.
I run into request limits every day after running the script a few times. I am starting to think I have a logical error that I can not find.
My code is below
import os
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util
from spotipy.oauth2 import SpotifyOAuth
from datetime import date
cid = ""
secret = ""
os.environ['SPOTIPY_CLIENT_ID'] = cid
os.environ['SPOTIPY_CLIENT_SECRET'] = secret
os.environ['SPOTIPY_REDIRECT_URI'] = 'http://example.com'
username = ""
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
scope = 'user-top-read'
auth_manager=SpotifyOAuth(scope=scope) # new
spotipy.Spotify(auth_manager=auth_manager) # new
class Spotify_Analysis:
def __init__(self):
self.track_ids = []
self.artist_ids = []
self.sortedsongs = []
self.newartists = []
self.cleaned_up = []
def get_related_artists(self):
artist_name = 'Jim Croce'
print('Starting search for ' + artist_name)
result = sp.search(q='artist:' + artist_name, type='artist')
name = result['artists']['items'][0]['name']
uri = result['artists']['items'][0]['uri']
related = sp.artist_related_artists(uri)
for artist in related['artists']:
self.artist_ids.append(artist['uri'])
def even_more_artists(self):
for uri in self.artist_ids:
related = sp.artist_related_artists(uri)
for artist in related['artists']:
self.newartists.append(artist['uri'])
print('Found ' + str((len(self.newartists))) + ' similar artists')
def get_artist_top_tracks(self):
print('Looking for Artists Songs')
sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
count = 0
for artist in self.newartists:
response = sp.artist_top_tracks(artist)
for track in response['tracks']:
self.track_ids.append(track['id'])
count += 1
print('Found ' + str(count) + ' songs')
print('Found ' + str(len(self.newartists)) + ' Similar Artists')
print('Found ' + str(len(self.track_ids)) + ' Songs from Similar Artists Before Threshold')
def create_playlist(self):
playlist_name = 'Songs You Can Scientifically Say You May Not Know Them (Jim Croce)'
print('Creating playlist named ' + str(playlist_name))
scope = "playlist-modify-public"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
user_id = sp.me()['id']
playlist = sp.user_playlist_create(user_id, name=playlist_name)
self.playlist_id = playlist["id"]
print('Playlist Created')
def cut_tracks(self):
print("Beginning to Narrow Down Tracks")
count = 0
self.track_ids = [*set(self.track_ids)]
for track in self.track_ids:
count = count + 1
print('Examining song #' + str(count) + ' out of ' + str(len(self.track_ids)))
if sp.track(track)['popularity'] < 20:
self.sortedsongs.append(track)
print('Songs under popularity threshhold: ' + str(len(self.sortedsongs)))
def add_to_playlist(self):
print("Adding Tracks to Playlist")
playlist = self.playlist_id
while self.sortedsongs:
sp.playlist_add_items(playlist_id=playlist, items=self.sortedsongs[:100],
position=None)
self.sortedsongs = self.sortedsongs[100:]
print('Added tracks to playlist')
def finished(self):
print('Playlist Compiled')
def main():
r1 = Spotify_Analysis()
r1.get_related_artists()
r1.even_more_artists()
r1.get_artist_top_tracks()
r1.create_playlist()
r1.cut_tracks()
r1.add_to_playlist()
r1.finished()
if __name__ == "__main__":
main()
I apologize if my code is not great I am not proficient yet

How to get Steam ID by username using Python?

I want to get steam ID by user name
using Python.
I tried the following code, but it prints only user names.
and i read the docs -> python-valve.readthedocs.io/en/latest/steamid.html
but i cant understand.
import valve.source.a2s
import valve.steam.id
import steam
import valve
import time
import a2s
import pymysql
Server_IP = ""
ServerPort = 0
Server_IP = input("Server IP : ")
ServerPort = input("Server PORT :")
SERVER_ADDRESS = (Server_IP, int(ServerPort))
while(1):
player_count = 1
with valve.source.a2s.ServerQuerier(SERVER_ADDRESS) as server:
info = server.info()
players = server.players()
steamUserid = server.__str__()
print("{player_count}/{max_players} {server_name}".format(**info))
for player in sorted(players["players"],
key=lambda p: p["score"], reverse=True):
user_name = ("{name}".format(**player))
if user_name == "":
duration = "{duration}".format(**player)
print("{:>2}".format(str(player_count)) + " | duration(sec) = " + "{: <20}".format(duration) + " EpicGames")
player_count += 1
else:
steam_name = "{name}".format(**player)
duration = "{duration}".format(**player)
print("{:>2}".format(str(player_count)) + " | duration(sec) = " + "{: <20}".format(duration) + " Steam" + steam_name)
player_count += 1
time.sleep(1)

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

Infinite while loop sometimes breaks

I wrote a little crypto switcher which checks profit and switches miners. But it works some time and then the loop stops without any error (it may work 15 min or 20 hours but it will stop, tested for 10 days).
code:
import os
import subprocess
import time
import copy
import requests
import configparser
from datetime import datetime
def config_read ():
config = configparser.ConfigParser()
config.read('config.ini')
return config.sections
def start_miner(info):
if info['algorithm'] == 'Equihash':
subprocess.Popen('F:\Claymore\start — music — Peon.bat', cwd='F:\Claymore',
creationflags=subprocess.CREATE_NEW_CONSOLE)
elif info['algorithm'] == 'Ethash':
subprocess.Popen('F:\Claymore\start — music — Peon.bat', cwd='F:\Claymore',
creationflags=subprocess.CREATE_NEW_CONSOLE)
return info
def stop_miner():
os.system("taskkill /f /t /im miner.exe")
os.system("taskkill /f /t /im EthDcrMiner64.exe")
def request_coins():
coins = None
while coins is None:
try:
coins = ((requests.get(
url='https://whattomine.com/coins.json?utf8=✓&eth=true&factor%5Beth_hr%5D=79.0&factor%5Beth_p%5D=0.0&factor%5Bgro_hr%5D=0.0&factor%5Bgro_p%5D=0.0&factor%5Bx11g_hr%5D=20.0&factor%5Bx11g_p%5D=0.0&factor%5Bcn_hr%5D=0.0&factor%5Bcn_p%5D=0.0&eq=true&factor%5Beq_hr%5D=1000.0&factor%5Beq_p%5D=0.0&factor%5Blrev2_hr%5D=80000.0&factor%5Blrev2_p%5D=0.0&factor%5Bns_hr%5D=0.0&factor%5Bns_p%5D=0.0&factor%5Blbry_hr%5D=0.0&factor%5Blbry_p%5D=0.0&factor%5Bbk2b_hr%5D=0.0&factor%5Bbk2b_p%5D=0.0&factor%5Bbk14_hr%5D=0.0&factor%5Bbk14_p%5D=0.0&factor%5Bpas_hr%5D=0.0&factor%5Bpas_p%5D=0.0&bkv=true&factor%5Bbkv_hr%5D=0.0&factor%5Bbkv_p%5D=0.0&factor%5Bcost%5D=0.06&sort=Profitability24&volume=0&revenue=24h&factor%5Bexchanges%5D%5B%5D=&factor%5Bexchanges%5D%5B%5D=bittrex&factor%5Bexchanges%5D%5B%5D=bleutrade&factor%5Bexchanges%5D%5B%5D=btc_e&factor%5Bexchanges%5D%5B%5D=bter&factor%5Bexchanges%5D%5B%5D=c_cex&factor%5Bexchanges%5D%5B%5D=cryptopia&factor%5Bexchanges%5D%5B%5D=poloniex&factor%5Bexchanges%5D%5B%5D=yobit&dataset=Main&commit=Calculate&adapt_q_280x=0&adapt_q_380=0&adapt_q_fury=0&adapt_q_470=0&adapt_q_480=0&adapt_q_750Ti=0&adapt_q_10606=3&adapt_q_1070=0&adapt_q_1080=0&adapt_q_1080Ti=0%27')).json())[
'coins']
except:
print("Site didn't respond. Reconnecting in 10 sec")
time.sleep(10)
return coins
def miner_chose(config, info):
user_coins = {}
coins = request_coins()
for key, value in config['Currency'].items():
if value == 'True':
tag = key.upper()
for key_coin, value_coin in coins.items():
if value_coin['tag'] == info['temp_currency']:
info['temp_profit'] = value_coin['btc_revenue24']
if value_coin['tag'] == info['currency']:
info['profit'] = value_coin['btc_revenue24']
if value_coin['tag'] == tag:
user_coins[key_coin] = value_coin
for key, value in user_coins.items():
if float(value['btc_revenue24']) >= float(info['profit']) * (float(config['CheckOptions']['profitprocent']) +100) / 100:
if not info['currency'] == value['tag']:
if float(value['btc_revenue24']) > float(info['temp_profit']):
if not info['temp_currency'] == value['tag']:
info['check_times'] = 0
info['temp_profit'] = value['btc_revenue24']
info['temp_currency'] = value['tag']
info['check_times'] += 1
if int(info['check_times']) >= int(config['CheckOptions']['times']):
info['profit'] = value['btc_revenue24']
info['currency'] = value['tag']
info['algorithm'] = value['algorithm']
info['check_times'] = 0
return info
Part of code that uses while loop and stops:
def main():
info = {'profit': 0, 'check_times': 200, 'currency': None, 'temp_profit': 0, 'temp_currency': None}
config = config_read()
while True:
if info['profit'] == 0:
stop_miner()
info = start_miner(miner_chose(config,info))
print(str(datetime.now()) + " - Starting miner first time. Currency: " + info['currency'] + '. Profit: ' +
info['profit'] + ' BTC/Day')
time.sleep(int(config['CheckOptions']['period']) * 60)
else:
old_info = copy.deepcopy(info)
info = miner_chose(config, info)
print(
str(datetime.now()) + ' - Checking profit. Current currency: ' + info['currency'] + '. Profit: ' + info[
'profit'] + ' BTC/Day')
if info['currency'] != old_info['currency']:
print('Changing miner. Currency ' + info['currency'] + '. Profit: ' + info['profit'] + ' BTC/Day')
elif info['currency'] == old_info['currency']:
print('Curency SAME')
# stop_miner()
# start_miner(info)
time.sleep(int(config['CheckOptions']['period']) * 60)
I want that after sleeping time.sleep(int(config['CheckOptions']['period']) * 60) script have to start again and make all check again but sometimes script sleep for time i have put in config and don't want to check again. It may stop after 10 or 20 check without any errors.
Need to add to coins = ((requests.get(url='https://whattomine.com/coins.json', timeout=3)).json())['coins'] cause the server something stack and don't send response.

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.

Categories