Author Topic: Generate insults with evilinsult.com API!  (Read 5432 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Generate insults with evilinsult.com API!
« on: July 23, 2020, 09:50:53 am »
Trust me; they get far raunchier.

 
insultapi.png


Have fun with this:

Code: QB64: [Select]
  1.  
  2. PRINT GetInsult
  3.  
  4. FUNCTION GetInsult$
  5.     DIM URL AS STRING
  6.     DIM URLFile AS STRING
  7.     DIM insultresponse AS STRING
  8.     DIM a%
  9.     URLFile = "insultresponse"
  10.     URL = "https://evilinsult.com/generate_insult.php?lang=en"
  11.     a% = API_request(URL, URLFile)
  12.     DIM U AS INTEGER
  13.     U = FREEFILE
  14.     OPEN URLFile FOR BINARY AS #U
  15.     IF LOF(U) <> 0 THEN
  16.         insultresponse = SPACE$(LOF(U))
  17.         GET #U, , insultresponse
  18.     ELSE
  19.         CLOSE #U
  20.         KILL URLFile
  21.         GetInsult = ""
  22.         EXIT FUNCTION
  23.     END IF
  24.     CLOSE #U
  25.     KILL URLFile
  26.     insultresponse = String.Replace(insultresponse, "&quot;", CHR$(34))
  27.     insultresponse = String.Replace(insultresponse, "&gt;", ">")
  28.     GetInsult = insultresponse
  29.  
  30. FUNCTION String.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  31.     DIM j
  32.     DIM r$
  33.     j = INSTR(a, b)
  34.     IF j > 0 THEN
  35.         r$ = LEFT$(a, j - 1) + c + String.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  36.     ELSE
  37.         r$ = a
  38.     END IF
  39.     String.Replace = r$
  40.  
  41.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  42.  
  43. FUNCTION API_request (URL AS STRING, File AS STRING)
  44.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 23, 2020, 10:37:05 am by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #1 on: July 23, 2020, 09:57:08 am »
Or perhaps you'd like it spoken aloud to you, you dang fool:

Code: QB64: [Select]
  1.  
  2. DIM insult AS STRING
  3. insult = GetInsult
  4. PRINT insult
  5. TTS insult
  6.  
  7.  
  8. FUNCTION GetInsult$
  9.     DIM URL AS STRING
  10.     DIM URLFile AS STRING
  11.     DIM insultresponse AS STRING
  12.     DIM a%
  13.     URLFile = "insultresponse"
  14.     URL = "https://evilinsult.com/generate_insult.php?lang=en"
  15.     a% = API_request(URL, URLFile)
  16.     DIM U AS INTEGER
  17.     U = FREEFILE
  18.     OPEN URLFile FOR BINARY AS #U
  19.     IF LOF(U) <> 0 THEN
  20.         insultresponse = SPACE$(LOF(U))
  21.         GET #U, , insultresponse
  22.     ELSE
  23.         CLOSE #U
  24.         KILL URLFile
  25.         GetInsult = ""
  26.         EXIT FUNCTION
  27.     END IF
  28.     CLOSE #U
  29.     KILL URLFile
  30.     insultresponse = String.Replace(insultresponse, "&quot;", CHR$(34))
  31.     insultresponse = String.Replace(insultresponse, "&gt;", ">")
  32.     GetInsult = insultresponse
  33.  
  34. FUNCTION String.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  35.     DIM j
  36.     DIM r$
  37.     j = INSTR(a, b)
  38.     IF j > 0 THEN
  39.         r$ = LEFT$(a, j - 1) + c + String.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  40.     ELSE
  41.         r$ = a
  42.     END IF
  43.     String.Replace = r$
  44.  
  45.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  46.  
  47. FUNCTION API_request (URL AS STRING, File AS STRING)
  48.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
  49.  
  50.     FUNCTION WinExec (lpCmdLine AS STRING, BYVAL nCmdShow AS LONG)
  51.  
  52. SUB TTS (lines AS STRING)
  53.     DIM x
  54.     x = WinExec("voice -d " + lines + CHR$(0), 0)
* voice.exe (Filesize: 14.5 KB, Downloads: 185)
« Last Edit: July 23, 2020, 10:09:54 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #2 on: July 23, 2020, 10:08:29 am »
Well you peaked my interest when you said audio but I think you are using a built-in Windows thing not an On-Line service. Or are you using an On-line service?

Anyway it's not working for me with Windows 10 laptop.

BTW the spirit of Don Rickles lives on I hear! ;)

« Last Edit: July 23, 2020, 10:17:00 am by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #3 on: July 23, 2020, 10:09:33 am »
@bplus shoot, I forgot to attach the exe. It's attached now.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #4 on: July 23, 2020, 10:39:16 am »
LOL while testing the voice box I wanted to put the insult thing in a loop, I made a little blunder but it had cool effect!

Here it is under control:
Code: QB64: [Select]
  1. _TITLE "Audio Insult needs Voice.exe"
  2. ' ref Spriggsy 2020-07-23  https://www.qb64.org/forum/index.php?topic=2846.msg121080#msg121080
  3.  
  4. DIM insult AS STRING
  5. WHILE _KEYDOWN(27) = 0  ' <<<  esc never gets caught!!!!
  6.     loopcnt = loopcnt + 1
  7.     IF loopcnt > 30 THEN SYSTEM '  alternate ending
  8.     insult = GetInsult
  9.     PRINT insult
  10.     TTS insult
  11. SLEEP  '< should of put this inside loop?
  12.  
  13. FUNCTION GetInsult$
  14.     DIM URL AS STRING
  15.     DIM URLFile AS STRING
  16.     DIM insultresponse AS STRING
  17.     DIM a%
  18.     URLFile = "insultresponse"
  19.     URL = "https://evilinsult.com/generate_insult.php?lang=en"
  20.     a% = API_request(URL, URLFile)
  21.     DIM U AS INTEGER
  22.     U = FREEFILE
  23.     OPEN URLFile FOR BINARY AS #U
  24.     IF LOF(U) <> 0 THEN
  25.         insultresponse = SPACE$(LOF(U))
  26.         GET #U, , insultresponse
  27.     ELSE
  28.         CLOSE #U
  29.         KILL URLFile
  30.         GetInsult = ""
  31.         EXIT FUNCTION
  32.     END IF
  33.     CLOSE #U
  34.     KILL URLFile
  35.     insultresponse = STRING.Replace(insultresponse, "&quot;", CHR$(34))
  36.     insultresponse = STRING.Replace(insultresponse, "&gt;", ">")
  37.     GetInsult = insultresponse
  38.  
  39. FUNCTION STRING.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  40.     DIM j
  41.     DIM r$
  42.     j = INSTR(a, b)
  43.     IF j > 0 THEN
  44.         r$ = LEFT$(a, j - 1) + c + STRING.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  45.     ELSE
  46.         r$ = a
  47.     END IF
  48.     STRING.Replace = r$
  49.  
  50.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  51.  
  52. FUNCTION API_request (URL AS STRING, File AS STRING)
  53.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
  54.  
  55.     FUNCTION WinExec (lpCmdLine AS STRING, BYVAL nCmdShow AS LONG)
  56.  
  57. SUB TTS (lines AS STRING)
  58.     DIM x
  59.     x = WinExec("voice -d " + lines + CHR$(0), 0)
  60.  
  61.  

After you think it's done press spacebar.
« Last Edit: July 23, 2020, 10:42:10 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #5 on: July 23, 2020, 10:46:26 am »
Ohhh CONSOLE doesn't do normal stuff with keys!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #6 on: July 23, 2020, 10:48:05 am »
Yeah CONSOLE has a totally different keystroke detection. A little annoying at times but I like using $CONSOLE:ONLY for my simple programs. Check out the help page for _CINP
« Last Edit: July 23, 2020, 10:52:10 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #7 on: July 23, 2020, 10:54:45 am »
@SpriggsySpriggs

I am testing this in a normal window and the thing in a loop keeps repeating the same insult. It doesn't seem to be getting fresh ones.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #8 on: July 23, 2020, 11:04:18 am »
@bplus Add a delay to the loop. I think you are running the loop so fast it doesn't have time to GetInsult and the voice doesn't have time to finish talking. Winexec works differently from SHELL. If I used SHELL it would stop and make the program wait until VOICE was complete. That, and the API might have a refresh time of so many requests in a certain span of time.
« Last Edit: July 23, 2020, 11:07:05 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #9 on: July 23, 2020, 11:08:40 am »
Right I never used WinExec before.

I tried this mod and STILL! no 2nd insult:
Code: QB64: [Select]
  1. _TITLE "Audio Insult needs Voice.exe"
  2. ' ref Spriggsy 2020-07-23  https://www.qb64.org/forum/index.php?topic=2846.msg121080#msg121080
  3. '$CONSOLE:ONLY
  4. '_DEST _CONSOLE
  5.  
  6. DIM insult AS STRING, insult2 AS STRING
  7. WHILE _KEYDOWN(27) = 0
  8.     loopcnt = loopcnt + 1
  9.     IF loopcnt > 30 THEN SLEEP: END
  10.     IF _FILEEXISTS("insultresponse") THEN KILL "insultresponse"
  11.     insult = GetInsult
  12.     PRINT insult
  13.     TTS insult
  14.     SLEEP
  15.  
  16.     IF _FILEEXISTS("insultresponse") THEN KILL "insultresponse"
  17.     insult2 = GetInsult
  18.     PRINT insult2
  19.     TTS insult2
  20.     SLEEP
  21.  
  22.  

It definitely seems like a timing thing!
« Last Edit: July 23, 2020, 11:09:46 am by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #10 on: July 23, 2020, 11:09:08 am »
@bplus Yep, it's an API thing. If I just go to the URL in a browser and hit refresh I get the same insult 3 - 4 times back to back before getting a new one. Chances are we need a heavy delay.
« Last Edit: July 23, 2020, 11:10:49 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #11 on: July 23, 2020, 11:13:31 am »
When I was testing original without loop it seems to me I was restarting exe in about the same amount of time, maybe not? and getting different insults although they started recycling quickly enough.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #12 on: July 23, 2020, 11:20:52 am »
@bplus I'm not sure then.
Shuwatch!

Marked as best answer by SpriggsySpriggs on July 23, 2020, 07:36:11 am

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Generate insults with evilinsult.com API!
« Reply #13 on: July 23, 2020, 11:35:38 am »
@bplus I've modified this. The issue is with URLMON. The first argument in URLDownloadToFileA is the ID of the caller. The webserver reads that and I guess it just keeps sending me the same data because of that. I've modified my code to use CURL instead:

Code: QB64: [Select]
  1. _CONSOLETITLE "Insult Generator 2.0"
  2.  
  3.  
  4. DIM insult AS STRING
  5.     loopcnt = loopcnt + 1
  6.     IF loopcnt > 30 THEN SYSTEM
  7.     insult = ""
  8.     insult = GetInsult
  9.     PRINT insult
  10.     TTS insult
  11.  
  12. FUNCTION GetInsult$
  13.     DIM URL AS STRING
  14.     DIM URLFile AS STRING
  15.     DIM insultresponse AS STRING
  16.     DIM a%
  17.     URLFile = "insultresponse"
  18.     SHELL _HIDE "curl " + CHR$(34) + "https://evilinsult.com/generate_insult.php?lang=en" + CHR$(34) + " -o " + URLFile
  19.  
  20.     DIM U AS INTEGER
  21.     U = FREEFILE
  22.     OPEN URLFile FOR BINARY AS #U
  23.     IF LOF(U) <> 0 THEN
  24.         insultresponse = SPACE$(LOF(U))
  25.         GET #U, , insultresponse
  26.     ELSE
  27.         CLOSE #U
  28.         KILL URLFile
  29.         GetInsult = ""
  30.         EXIT FUNCTION
  31.     END IF
  32.     CLOSE #U
  33.     KILL URLFile
  34.     insultresponse = STRING.Replace(insultresponse, "&quot;", CHR$(34))
  35.     insultresponse = STRING.Replace(insultresponse, "&gt;", ">")
  36.     insultresponse = STRING.Replace(insultresponse, "&amp;", "&")
  37.     insultresponse = STRING.Replace(insultresponse, "-->", "")
  38.     GetInsult = insultresponse
  39.  
  40. FUNCTION STRING.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  41.     DIM j
  42.     DIM r$
  43.     j = INSTR(a, b)
  44.     IF j > 0 THEN
  45.         r$ = LEFT$(a, j - 1) + c + STRING.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  46.     ELSE
  47.         r$ = a
  48.     END IF
  49.     STRING.Replace = r$
  50.  
  51. SUB TTS (lines AS STRING)
  52.     DIM x
  53.     x = _SHELLHIDE("voice -d " + lines + CHR$(0))
* voice.exe (Filesize: 14.5 KB, Downloads: 168)
« Last Edit: July 23, 2020, 11:46:41 am by SpriggsySpriggs »
Shuwatch!

Offline Lucky_Strikez

  • Newbie
  • Posts: 7
    • View Profile
Re: Generate insults with evilinsult.com API!
« Reply #14 on: August 01, 2020, 12:00:01 pm »
"Do you have to leave so soon? I was just about to poison the tea."        haha!