Help with grabbing a number from a website and subtracting it to a exciting value Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
burken
Posts: 5
Joined: 10 May 2021, 20:21

Help with grabbing a number from a website and subtracting it to a exciting value

Post by burken » 10 May 2021, 20:45

Hello!

I would like to create a hotkey where when I press "F3" it will send the message "Channel has X number of followers and only need Y more to hit 500k"
X value I can get from decapi. and Y value simply is 500,000 - X.

However, I can't figure out how to get the script to do that calculation.
I'm new to autohotkey but this is what I've made so far. So %OutputVar% gives me my X. But how do I subtract it from 500,000?
Any ideas/tips would be greatly appreciated.

Code: Select all

F3::
a := 500000
b := %OutputVar% 
c := Func1(a,b)

Func1(x, y)
		{
			return x - y
		}

urlsave=https decapi.me /twitch/followcount/twitchgaming  Broken Link for safety
URLDownloadToFile, %urlsave%, %A_Desktop%\ahkdownload1.txt 
Fileread, OutputVar, %A_Desktop%\ahkdownload1.txt
Send twitchgaming has %OutputVar% followers and only need %c% followers more to hit 500k
Return
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Help with grabbing a number from a website and subtracting it to a exciting value  Topic is solved

Post by mikeyww » 10 May 2021, 21:24

Code: Select all

urlsave := "https://decapi.me/twitch/followcount/twitchgaming", goal := 500000
done    := A_Desktop "\ahkdownload1.txt"
Gosub, F3
F3::
Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Downloading
URLDownloadToFile, %urlsave%, %done%
If ErrorLevel {
 Gui, Destroy
 MsgBox, 48, Error, An error occurred while downloading. Aborting.
 Return
} Else FileRead, followers, %done%
Gui, Destroy
MsgBox, 64, Status, % "Twitchgaming has " thousands(followers)" followers, and needs only " 
 . thousands(goal - followers) " more to reach " thousands(goal) "."
Return

thousands(x, s := ",") {
 ; https://autohotkey.com/board/topic/50019-add-thousands-separator/
 Return RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
}
burken
Posts: 5
Joined: 10 May 2021, 20:21

Re: Help with grabbing a number from a website and subtracting it to a exciting value

Post by burken » 10 May 2021, 21:50

mikeyww wrote:
10 May 2021, 21:24

Code: Select all

urlsave := "https decapi.me /twitch/followcount/twitchgaming",  Broken Link for safety goal := 500000
done    := A_Desktop "\ahkdownload1.txt"
Gosub, F3
F3::
Gui, New
Gui, Font, s12 w500
Gui, Color, F8DC75
Gui, Add, Text, w320 Center, Please wait....
Gui, Show,, Downloading
URLDownloadToFile, %urlsave%, %done%
If ErrorLevel {
 Gui, Destroy
 MsgBox, 48, Error, An error occurred while downloading. Aborting.
 Return
} Else FileRead, followers, %done%
Gui, Destroy
MsgBox, 64, Status, % "Twitchgaming has " thousands(followers)" followers, and needs only " 
 . thousands(goal - followers) " more to reach " thousands(goal) "."
Return

thousands(x, s := ",") {
 ; https://autohotkey.com/board/topic/50019-add-thousands-separator/
 Return RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
}
That was fast, and it works perfectly! Thank you so much for your help.
Do you know if it's possible to get the command to send "Twitchgaming has " thousands(followers)" followers, and needs only "
. thousands(goal - followers) " more to reach " thousands(goal)" in text, rather than a MsgBox?
burken
Posts: 5
Joined: 10 May 2021, 20:21

Re: Help with grabbing a number from a website and subtracting it to a exciting value

Post by burken » 10 May 2021, 22:32

Figured it out!! I just replaced Msgbox with Send. Thank you so much for your help
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Help with grabbing a number from a website and subtracting it to a exciting value

Post by mikeyww » 11 May 2021, 06:28

You are welcome. Another method is below.

Code: Select all

goal := 500000
Gosub, F3
F3::
followers := webGet("https://decapi.me/twitch/followcount/twitchgaming")
If (followers = "") {
 SoundBeep, 1000
 Return
}
If (followers < goal)
 SendInput, % "Twitchgaming has " thousands(followers)" followers, and needs only " 
  . thousands(goal - followers) " more to reach " thousands(goal) "."
Else SendInput, % "You have " thousands(followers) " followers{!}"
Return

thousands(x, s := ",") {
 ; https://autohotkey.com/board/topic/50019-add-thousands-separator/
 Return RegExReplace(x, "\G\d+?(?=(\d{3})+(?:\D|$))", "$0" s)
}

webGet(url) {
 ; https://jacks-autohotkey-blog.com/2019/05/06/autohotkey-web-data-extraction-regex-trick/
 Gui, New
 Gui, Font, s12 w500
 Gui, Color, F8DC75
 Gui, Add, Text, w320 Center, Please wait....
 Gui, Show,, Downloading
 whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
 whr.SetTimeouts(0, 30000, 30000, 600000), whr.Open("GET", url, False)
 Try {
  whr.Send()
  Sleep 100
  response := whr.ResponseText
 }
 Catch, err
  errMsg := "Problem:  " err.Extra
   . "`nFile:          " err.File
   . "`nLine:         " err.Line
   . "`n              " err.What
   . "`nError:                       " err.Message
 Gui, Destroy
 Return response
}
Post Reply

Return to “Ask for Help (v1)”