Football scores to Guardian programatically - Script & Sample BAF

Football, Soccer - whatever you call it. It is the beautiful game.
fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

jimibt wrote:
Fri Mar 26, 2021 12:13 pm


bingo - embarrassed that this didn't pop off the page at me either. perfect and generic solution (for all head to head sports)

and just to clarify, you would simply apply a 1 or 2 to the value in column #4 and a * in column #3 (selection name)... ;)
dont understand how this solves the problem as guardian wouldnt be able to match all these 1 and 2 in the csv file with the correct game. Without the team name idt its not possible to correctly identify the game and score.
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

fxcarllos wrote:
Tue Mar 30, 2021 8:00 am
jimibt wrote:
Fri Mar 26, 2021 12:13 pm


bingo - embarrassed that this didn't pop off the page at me either. perfect and generic solution (for all head to head sports)

and just to clarify, you would simply apply a 1 or 2 to the value in column #4 and a * in column #3 (selection name)... ;)
dont understand how this solves the problem as guardian wouldnt be able to match all these 1 and 2 in the csv file with the correct game. Without the team name idt its not possible to correctly identify the game and score.
Correct. For multiple matches in the CSV you need a short string in the selection name (Col 3) PLUS the row number in Col 4. That would prevent issues with derby matches and mismatches pretty much. I'll adjust the script and repost when I get a sec
fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

sniffer66 wrote:
Tue Mar 30, 2021 8:19 am

Correct. For multiple matches in the CSV you need a short string in the selection name (Col 3) PLUS the row number in Col 4. That would prevent issues with derby matches and mismatches pretty much. I'll adjust the script and repost when I get a sec
That would be very wellcome. Cheers :)
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

Ok, had to get a little creative trying to prevent double matches (2 teams playing as City etc)

I've added in a function that will split the entire team name, delimiting by spaces, and then find the first of those split strings with more than 3 characters. It then checks all previous name matches downloaded on that cycle to see if that string exists for another team (City, United etc) and moves on to the next string > 3 chars if another match is found. It will then wildcard that sub string in the CSV. It will also specify the row number in the CSV (1 for Home Team, 2 for Away)

If it cant find any unique string of more than 3 characters it will return a selection name of "NotFound" and not match any selection. I think it better to not have a score returned at all than to return a score for another team. You'll need to cater for a non entry in your baf

Anything less than a 4 character string will cause too many matches IMO

I've also addedd in a function that will strip out most, if not all special characters - accents, circumflex etc

That's about as much as I can do. I think you'll be pretty unlucky to have an incorrect match, in the same row but I could be wrong. Test and let me know

Stu

Tested fine on Mongolia v Japan

30/03/2021 12:57:16: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:16: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:22: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:22: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:27: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:27: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:32: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:32: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:37: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:37: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:43: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:43: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:45: Guardian has detected that the market is suspended

Current CSV looks like this:

* * *Mongolia* 1 S Score 0
* * *Japan* 2 S Score 8
* * *Maziya* 1 S Score 0
* * *United* 2 S Score 0
* * *Nedlands* 1 S Score 1
* * *Gosnells* 2 S Score 4
* * *Bristol* 1 S Score 0
* * *Millwall* 2 S Score 0
* * *Velsao* 1 S Score 1
* * *Churchill* 2 S Score 2
* * *Eves* 1 S Score 1
* * *Hindustan* 2 S Score 1
* * *Sudeva* 1 S Score 2
* * *Rangers* 2 S Score 1
* * *Arema* 1 S Score 1
* * *PSIS* 2 S Score 0
* * *Maccabi* 1 S Score 0
* * *Bnei* 2 S Score 0
* * *Sport* 1 S Score 0
* * *Hapoel* 2 S Score 1
* * *Ironi* 1 S Score 0
* * *Pardes* 2 S Score 0
* * *Hakoach* 1 S Score 0
* * *Marmorek* 2 S Score 2
* * *Shurtan* 1 S Score 0
* * *Buxoro* 2 S Score 0
* * *Becamex* 1 S Score 0
* * *NotFound* 2 S Score 1

Code: Select all

#include <Inet.au3>
#include <json.au3>
#include <Array.au3>
#include <String.au3>
#include <File.au3>

$TitleString = "Team|Score"


;File to connect Guardian to. Will save in same location as script
$File = "score.csv"


While 1

	$URL = "https://api.sofascore.com/api/v1/sport/football/events/live"
	$data = _INetGetSource($URL)
	$object = json_decode($data)

	Local $Count = Json_Get($object, '.events')

	FileDelete($File)
	FileOpen($File)

	$z = 0

	Local $NameArray[1]

	For $i = 0 To UBound($Count) - 1

		;Writing "1" to first line of the CSV. Required by Guardian
		If $z = 0 Then
			FileWriteLine($File, "1")
		EndIf
		$z = 1

		$HomeTeam = _StringReplaceAccent(Strip(Json_Get($object, '.events' & '[' & $i & '].homeTeam.name')))
		$AwayTeam = _StringReplaceAccent(Strip(Json_Get($object, '.events' & '[' & $i & '].awayTeam.name')))

		$HomeString = "*,*," & "*" & $HomeTeam & "*,1,"
		$AwayString = "*,*," & "*" & $AwayTeam & "*,2,"

		$HomeString = $HomeString & "S,Score," & Json_Get($object, '.events' & '[' & $i & '].homeScore.current')
		$AwayString = $AwayString & "S,Score," & Json_Get($object, '.events' & '[' & $i & '].awayScore.current')

		$URL = "https://api.sofascore.com/api/v1/event/" & Json_Get($object, '.events' & '[' & $i & '].id') & "/statistics"
		;MsgBox("", "", $URL)
		$data1 = _INetGetSource($URL)

		If $data1 <> "" Then
			;Json_Dump($data1)
			;MsgBox("", "", $data1)
		EndIf

		;MsgBox($MB_SYSTEMMODAL, "Score", $HomeString & @CRLF & $AwayString)
		FileWriteLine($File, $HomeString)
		FileWriteLine($File, $AwayString)

		_ArrayAdd($NameArray, $HomeTeam)
		_ArrayAdd($NameArray, $AwayTeam)
		;_ArrayDisplay($NameArray)


	Next

	FileClose($File)

	;Wait time between score refresh in milliseconds
	;#######DO NOT SET SHORTER THAN 10S AS YOUR IP MAY BE BLOCKED#####

	Sleep(30000) ;30 seconds


WEnd

Func Strip($Param)
	$f = "NotFound"
	$e = StringSplit($Param, " ")
	;_ArrayDisplay($e)
	For $z = 1 To UBound($e) - 1
		_ArraySearch($NameArray, $e[$z])
		If @error Then
			If StringLen($e[$z]) > 3 Then
				$f = $e[$z]
				ExitLoop
			EndIf
		EndIf
	Next
	;MsgBox($MB_SYSTEMMODAL, "Result", $f )
	Return $f
EndFunc   ;==>Strip


Func _StringReplaceAccent($sString)
	Local $exp, $rep
	Local $Pattern[29][2] = [ _
			["[ÀÁÂÃÅÆ]", "A"], ["[àáâãåą]", "a"], ["Ä", "Ae"], ["[æä]", "ae"], _
			["Þ", "B"], ["þ", "b"], _
			["ÇĆ", "C"], ["[çćč]", "c"], _
			["[ÈÉÊË]", "E"], ["[èéêë]", "e"], _
			["[ÌÍÎÏ]", "I"], ["[ìíîï]", "i"], _
			["Ñ", "N"], ["ñ", "n"], _
			["[ÒÓÔÕÖØ]", "O"], ["[ðòóôõöø]", "o"], _
			["ř", "r"], _
			["[ŠŚ]", "S"], ["[š]", "s"], _
			["ß", "Ss"], _
			["Ț", "T"], _
			["[ÙÚÛ]", "U"], ["[ùúû]", "u"], ["Ü", "Ue"], ["ü", "ue"], _
			["Ý", "Y"], ["[ýýÿ]", "y"], _
			["Ž", "Z"], ["ž", "z"]]

	For $i = 0 To (UBound($Pattern) - 1)
		$exp = $Pattern[$i][0]
		If $exp = "" Then ContinueLoop
		$rep = $Pattern[$i][1]

		$sString = StringRegExpReplace($sString, $exp, $rep)
		If @error == 0 And @extended > 0 Then
			;ConsoleWrite($sString & @LF & "--> " & $exp & @LF)
		EndIf
	Next

	Return $sString
EndFunc   ;==>_StringReplaceAccent

fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

sniffer66 wrote:
Tue Mar 30, 2021 1:06 pm


Tested fine on Mongolia v Japan

30/03/2021 12:57:16: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:16: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:22: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:22: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:27: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:27: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:32: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:32: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:37: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:37: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:43: [G_Auto 1] : Store Value for Mongolia: Score = 0
30/03/2021 12:57:43: [G_Auto 1] : Store Value for Japan: Score = 7
30/03/2021 12:57:45: Guardian has detected that the market is suspended

Current CSV looks like this:

* * *Mongolia* 1 S Score 0
* * *Japan* 2 S Score 8
* * *Maziya* 1 S Score 0
* * *United* 2 S Score 0
* * *Nedlands* 1 S Score 1
* * *Gosnells* 2 S Score 4
* * *Bristol* 1 S Score 0
* * *Millwall* 2 S Score 0
* * *Velsao* 1 S Score 1
* * *Churchill* 2 S Score 2
* * *Eves* 1 S Score 1
* * *Hindustan* 2 S Score 1
* * *Sudeva* 1 S Score 2
* * *Rangers* 2 S Score 1
* * *Arema* 1 S Score 1
* * *PSIS* 2 S Score 0
* * *Maccabi* 1 S Score 0
* * *Bnei* 2 S Score 0
* * *Sport* 1 S Score 0
* * *Hapoel* 2 S Score 1
* * *Ironi* 1 S Score 0
* * *Pardes* 2 S Score 0
* * *Hakoach* 1 S Score 0
* * *Marmorek* 2 S Score 2
* * *Shurtan* 1 S Score 0
* * *Buxoro* 2 S Score 0
* * *Becamex* 1 S Score 0
* * *NotFound* 2 S Score 1
I really apreciate your effort. my csv file after i run the new code doesnt mention any teams..

*,*,**,1,S,Score,0
*,*,**,2,S,Score,0
*,*,**,1,S,Score,1
*,*,**,2,S,Score,0
*,*,**,1,S,Score,3
*,*,**,2,S,Score,0
*,*,**,1,S,Score,1
*,*,**,2,S,Score,0
*,*,**,1,S,Score,2
*,*,**,2,S,Score,0
*,*,**,1,S,Score,0
*,*,**,2,S,Score,0
*,*,**,1,S,Score,0
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

Weird, I just ran mine again and names are there. Any errors ?

* * *Saudi* 1 S Score 0
* * *Liberia* 2 S Score 0
* * *Novara* 1 S Score 1
* * *Sesto* 2 S Score 0
* * *Setif* 1 S Score 4
* * *M'lila* 2 S Score 0
* * *Kabylie* 1 S Score 1
* * *Relizane* 2 S Score 0
* * *Alger* 1 S Score 2
* * *Chlef* 2 S Score 0
* * *Ouled* 1 S Score 1
* * *Batna* 2 S Score 0
* * *East* 1 S Score 1
* * *Malkiya* 2 S Score 1
* * *Buje* 1 S Score 0
* * *Pazinka* 2 S Score 0
* * *Dinara* 1 S Score 0
* * *Sibenik* 2 S Score 1
* * *Maccabi* 1 S Score 1
fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

sniffer66 wrote:
Tue Mar 30, 2021 4:45 pm
Weird, I just ran mine again and names are there. Any errors ?

* * *Saudi* 1 S Score 0
* * *Liberia* 2 S Score 0
* * *Novara* 1 S Score 1
* * *Sesto* 2 S Score 0
* * *Setif* 1 S Score 4
* * *M'lila* 2 S Score 0
* * *Kabylie* 1 S Score 1
* * *Relizane* 2 S Score 0
* * *Alger* 1 S Score 2
* * *Chlef* 2 S Score 0
* * *Ouled* 1 S Score 1
* * *Batna* 2 S Score 0
* * *East* 1 S Score 1
* * *Malkiya* 2 S Score 1
* * *Buje* 1 S Score 0
* * *Pazinka* 2 S Score 0
* * *Dinara* 1 S Score 0
* * *Sibenik* 2 S Score 1
* * *Maccabi* 1 S Score 1
Its really weird. Run it on 2 dif computers and same result. No errors during compile. Wonder if its something to do with office version.. if that matters for coding. Or maybe geographic location related symbols.
fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

I might have figure out the problem.
Its on this part of the code

Local $Pattern[29][2]..

Doing some tests and will post back
fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

Hi again

Managed to solve the problem removing one character
So this part of the code is like this:

Code: Select all

Local $Pattern[28][2] = [ _
			["[ÀÁÂÃÅÆ]", "A"], ["[àáâãåa]", "a"], ["Ä", "Ae"], ["[æä]", "ae"], _
			["Þ", "B"], ["þ", "b"], _
			["ÇC", "C"], ["[çcc]", "c"], _
			["[ÈÉÊË]", "E"], ["[èéêë]", "e"], _
			["[ÌÍÎÏ]", "I"], ["[ìíîï]", "i"], _
			["Ñ", "N"], ["ñ", "n"], _
			["[ÒÓÔÕÖØ]", "O"], ["[ðòóôõöø]", "o"], _
			["r", "r"], _
			["[ŠS]", "S"], ["[š]", "s"], _
			["ß", "Ss"], _
			["[ÙÚÛ]", "U"], ["[ùúû]", "u"], ["Ü", "Ue"], ["ü", "ue"], _
			["Ý", "Y"], ["[ýýÿ]", "y"], _
			["Ž", "Z"], ["ž", "z"]]
If you noticed.. right now there are 11 Hapoel teams playing. Just be carefull not to run it in these scenarios.

thanks for coding this ;)
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

fxcarllos wrote:
Tue Mar 30, 2021 5:44 pm
Hi again

Managed to solve the problem removing one character
So this part of the code is like this:

Code: Select all

Local $Pattern[28][2] = [ _
			["[ÀÁÂÃÅÆ]", "A"], ["[àáâãåa]", "a"], ["Ä", "Ae"], ["[æä]", "ae"], _
			["Þ", "B"], ["þ", "b"], _
			["ÇC", "C"], ["[çcc]", "c"], _
			["[ÈÉÊË]", "E"], ["[èéêë]", "e"], _
			["[ÌÍÎÏ]", "I"], ["[ìíîï]", "i"], _
			["Ñ", "N"], ["ñ", "n"], _
			["[ÒÓÔÕÖØ]", "O"], ["[ðòóôõöø]", "o"], _
			["r", "r"], _
			["[ŠS]", "S"], ["[š]", "s"], _
			["ß", "Ss"], _
			["[ÙÚÛ]", "U"], ["[ùúû]", "u"], ["Ü", "Ue"], ["ü", "ue"], _
			["Ý", "Y"], ["[ýýÿ]", "y"], _
			["Ž", "Z"], ["ž", "z"]]
If you noticed.. right now there are 11 Hapoel teams playing. Just be carefull not to run it in these scenarios.

thanks for coding this ;)
Are you using a non standard character set ? Guess your device didn't recognise that character
fxcarllos
Posts: 14
Joined: Thu Aug 19, 2010 8:58 pm

sniffer66 wrote:
Tue Mar 30, 2021 6:18 pm

Are you using a non standard character set ? Guess your device didn't recognise that character
I didnt change anything on my keyboard or computer. And i tried 2 dif computers. I´m in Portugal, guess its a country thing.

cheers
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

mongo wrote:
Tue May 18, 2021 12:01 pm
Hi,
New to stored values and automation so in need a bit of assistance as it's doing my head in (probably overthinking it)!

I've managed to get this installed and running fine in the background, I can see the inplay scores being generated in the csv file and in the log for each game in guardian.

How do I know get them in the watchlist to show up as alerts on each selection?

Thanks
I dont usually alert to the watchlist so hopefully someone else will chime in.
Just be careful with using this automated, I only put it up as a quick POC and it's not foolproof as far as the pattern matching on the team names. Pattern matching via the CSV could do with a little enhancement to ensure it's 100% on the matching i.e use of multiple wildcards "*Man*City* etc
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

mongo wrote:
Wed May 19, 2021 1:23 am
sniffer66 wrote:
Tue May 18, 2021 1:07 pm


I dont usually alert to the watchlist so hopefully someone else will chime in.
Just be careful with using this automated, I only put it up as a quick POC and it's not foolproof as far as the pattern matching on the team names. Pattern matching via the CSV could do with a little enhancement to ensure it's 100% on the matching i.e use of multiple wildcards "*Man*City* etc
Must say this is an excellent script.
After a bit of head scratching and some trial and error I've got it working to show the scores on the watchlist.

But I see what you mean about matching the team names, not all are being caught from the csv file into guardian, just one example would be Leicester City tonight, guardian only had the team name 'Leicester' and it wouldn't store the score as the csv had the team name down as 'Leicester City'.

What can I do to add the multiple wildcards? I presume I'd just need to edit the Football.au3 file? or can it be done in the csv file?
Unfortunately it's a limitation of the current pattern matching within BA. You can only use wildcards at the beginning and end of a string in the CSV

i.e *Manchester*

If you have a match where Manchester City are playing Manchester United the string "Man" would end up matching both teams and the goals scored for each team would be incorrect

Ideally you would pattern match using *Man*Ci* (<any string>Man<any string>Ci<any string>) and *Man*Uni*

That would solve the issue. I've been meaning to put a suggestion in for it
sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

sniffer66
Posts: 1666
Joined: Thu May 02, 2019 8:37 am

Little bit more playing around with the script and have current score, In-play time (inc ET), 5m/10m/Match - Average Attack Momentum Graph, SOT and Shots Off T set up for Watch List Alerts and Guardian Automation ;)

Just a shame SS don't provide them for every match



Capture.JPG
You do not have the required permissions to view the files attached to this post.
Post Reply

Return to “Football trading”