Jump to content

Trying to convert a Python script to Autoit


Recommended Posts

Hi Guys,

I am trying to convert a python script to AutoIT, but it is not returning any data. :(

My aim is to eventually have a script which sends me an email whenever a goal is scored in the English Premiership.

Is there any guru who can have a quick look over the below script and see if there are any obvious errors?

To use a football analogy, I think that I have hit the bar a couple of times, but not yet found the back of the net! 

#cs ----------------------------------------------------------------------------
PYTHON CODE:
import requests
import json
endpoint = "https://api.betfair.com/exchange/betting/rest/v1.0/"
header = { 'X-Application' : 'APP_KEY_HERE', 'X-Authentication' : 'SESSION_TOKEN_HERE' ,'content-type' : 'application/json' }
json_req='{"filter":{ }}'
url = endpoint + "listEventTypes/"
response = requests.post(url, data=json_req, headers=header)
print json.dumps(json.loads(response.text), indent=3)
#ce ----------------------------------------------------------------------------

#include <WinHttp.au3>
Opt("MustDeclareVars", 1)
Opt("TrayIconDebug", 1)

 

Global $sAddress     = 'https://api.betfair.com/exchange/betting/json-rpc/v1/'
Global $sPostData    = 'https://api.betfair.com/exchange/betting/json-rpc/v1/listEventTypes/'
Global $sAppKey      = '4YpsEhdsgtedjd'     
Global $sSession_key = 'BhKl7Ijdu4E3pzHhKu7K5fQrvzf9p3wq/diRMt7bZka7'
Global $sjson_req    = '{"filter":{ }}'

 
Global $hOpen    = _WinHttpOpen()  ; Get Session handle 
; ##### CHECKED AND VALID HANDLE IS RETURNED #####

Global $hConnect = _WinHttpConnect($hOpen, $sAddress) ; Get connection handle
; ##### CHECKED AND VALID HANDLE IS RETURNED #####

Global $hRequest = _WinHttpOpenRequest($hConnect, 'POST') ; Make a request
; ##### CHECKED AND VALID HANDLE IS RETURNED #####

  
; Add header fields to the request
_WinHttpAddRequestHeaders($hRequest, 'X-Application: ' & $sAppKey)
_WinHttpAddRequestHeaders($hRequest, 'X-Authentication: ' & $sSession_key)
_WinHttpAddRequestHeaders($hRequest, 'Accept: application/json')
_WinHttpAddRequestHeaders($hRequest, 'Content-Type: application/json') 
_WinHttpSendRequest($sPostData, 'Data=' & $sjson_req, $hRequest) ; Send it
_WinHttpReceiveResponse($hRequest) ; Wait for the response

; Check if there is a response
Global $sHeader, $sReturned

If _WinHttpQueryDataAvailable($hRequest) Then
    $sHeader = _WinHttpQueryHeaders($hRequest)
    MsgBox(64, 'Header', $sHeader)
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
    ConsoleWrite($sReturned) ; Print returned
Else
    ConsoleWriteError('!No data available.' & @CRLF)
    MsgBox(48, 'Failure', 'No data available.')  ;##### <==== ALWAYS FAILS HERE! #####
EndIf

; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I am sure that I have probably missed out a vital line, but for the life of me I can't see the wood for the trees anymore.

Anyone got any suggestions?

Many thanks,

Pete

Link to comment
Share on other sites

Maybe like this:

#include "WinHttp.au3"

$sAddress = 'https://api.betfair.com/exchange/betting/json-rpc/v1/'
$sAppKey = '4YpsEhdsgtedjd'
$sSession_key = 'BhKl7Ijdu4E3pzHhKu7K5fQrvzf9p3wq/diRMt7bZka7'
$sjson_req = '{"filter":{ }}'

$hOpen = _WinHttpOpen()

$hConnect = _WinHttpConnect($hOpen, $sAddress)

$sReturned = _WinHttpSimpleSSLRequest($hConnect, "POST", "listEventTypes/", Default, $sjson_req, 'X-Application: ' & $sAppKey & @CRLF & 'X-Authentication: ' & $sSession_key & @CRLF & 'Content-Type: application/json', Default, Default, Default, Default, 1)

_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

ConsoleWrite($sReturned & @CRLF)

 

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Hi Trancexx,

Many thanks for your quick response.

I have actually seen your solutions posted on many queries. You are seriously knowledgable my friend.

The script now returns the message:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Service not Found</title>  
    </head>
    <body>
        <b>The URL you specified did not correspond to a service.x</b>
    </body>
</html>

Normally, it should return a JSON string which the user then decodes.

It is much better than anything which I have got so far.

I will have a play about with it 🙂

Many thanks for your help.

Best regards,

Pete

 

Link to comment
Share on other sites

Any reason your code needs to be in AutoIt? You could run your python script and get the results in AutoIt through the command line (since your python code already works)

#include <AutoItConstants.au3>

; Start your python code
Local $iPID = Run('"C:\python\path\python.exe"' & '"C:\other\path\script.py"', @SW_HIDE, $STDOUT_CHILD)

; Wait for the python code to end
ProcessWaitClose($iPID)

; Read the results
Local $sResults = StdoutRead($iPID)

Edit: This would read everything you "print" out in your python script. Alternately, you could have your python write the results to a file

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

4 hours ago, pete_wilde said:

Hi Trancexx,

Many thanks for your quick response.

I have actually seen your solutions posted on many queries. You are seriously knowledgable my friend.

The script now returns the message:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Service not Found</title>  
    </head>
    <body>
        <b>The URL you specified did not correspond to a service.x</b>
    </body>
</html>

Normally, it should return a JSON string which the user then decodes.

It is much better than anything which I have got so far.

I will have a play about with it 🙂

Many thanks for your help.

Best regards,

Pete

 

Maybe besause it shoud be like in your original code unlike your AutoIt one: "https://api.betfair.com/exchange/betting/rest/v1.0/"

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

19 hours ago, trancexx said:

Maybe besause it shoud be like in your original code unlike your AutoIt one: "https://api.betfair.com/exchange/betting/rest/v1.0/" 

Tried the alternative URL and got the same result.

Might just need something else added to the URL.

I will play about with it and see what happens.


You have definitely got me much further than I previously was, so many thanks for that. It is much appreciated 🙂

 

Link to comment
Share on other sites

Hello. If you want to use the JSON-RPC protocol endpoint you need to define your json correclty "method,params ,id." Otherwise you could use regular JSON REST endpoints and send just parameters.

Check Doc Table:

Interface
Endpoint
JSON-RPC Prefix
<method> Example
JSON-RPC https://api.betfair.com/exchange/betting/json-rpc/v1 <method> SportsAPING/v1.0/listMarketBook
JSON REST https://api.betfair.com/exchange/betting/rest/v1.0/   listMarketBook/

Saludos

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...