Author Topic: Setting Text from Website as a Variable?  (Read 1590 times)

robdh4

  • Newbie
  • *
  • Posts: 12
Setting Text from Website as a Variable?
« on: June 01, 2019, 10:30:36 AM »
How would I pull text off of a website and set it as a variable? I'm playing Fallout 76 and want to get the most recent nuclear launch codes from the website
https://nukacrypt.com/
I want to set the numbers listed for Alpha, Bravo, and Charlie as separate variables.
« Last Edit: June 01, 2019, 01:34:15 PM by robdh4 »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4758
  • RTFM
Re: Setting Text from Website as a Variable?
« Reply #1 on: June 01, 2019, 03:25:48 PM »
Here's one way to do it:

Code: [Select]
Set Text [~webData] to [https://nukacrypt.com/]
Inline C# Function: Parse web data for codes
Write [Blue] 'ALPHA = {TXT:ALPHA}, BRAVO = {TXT:BRAVO}, CHARLIE = {TXT:CHARLIE}' to log

Where "Parse web data for codes" contains:
Code: [Select]
using System;

public class VAInline
{
public void main()
{
string[] webData = VA.GetText("~webData").Split('\n');//Split on newline character only as text values from URLs does not use Environment.Newline

for (int i = 0; i < webData.Length; i++)
{
if (webData[i].Contains("ALPHA") && webData[i].Contains("BRAVO") && webData[i].Contains("CHARLIE"))//If the current line is the table header
{
string ALPHA = "";
string BRAVO = "";
string CHARLIE = "";

char[] codeLine = webData[i + 1].ToCharArray();//Get the next line of the table, which should be the codes

int currentCode = 0;
bool fromCode = false;
foreach(char c in codeLine)
{
if (Char.IsDigit(c))
{
if (currentCode == 0)
{
ALPHA += c;
fromCode = true;
}
else if (currentCode == 1)
{
BRAVO += c;
fromCode = true;
}
else if (currentCode == 2)
{
CHARLIE += c;
fromCode = true;
}
}
else if (fromCode == true)
{
currentCode++;
fromCode = false;
}
}

VA.SetText("ALPHA", ALPHA);
VA.SetText("BRAVO", BRAVO);
VA.SetText("CHARLIE", CHARLIE);

return;
}
}
}
}

robdh4

  • Newbie
  • *
  • Posts: 12
Re: Setting Text from Website as a Variable?
« Reply #2 on: June 02, 2019, 09:14:09 AM »
Thank you Pfeil. Worked as advertised. Also I set it up so the numbers get typed out in game and read aloud by TTS. For the TTS I wanted a pause between each digit so that's why there's lots of underscores and text subtraction.

[alpha; bravo; charlie] Code
Code: [Select]
Set Text [~webData] to [https://nukacrypt.com/]
Inline C# Function: Parse web data for codes., wait until execution finishes
Begin Text Compare : [{CMDSEGMENT:0}] Equals 'alpha'
    Say, '{TXTSUBSTR:ALPHA:0:1}_{TXTSUBSTR:ALPHA:1:1}_{TXTSUBSTR:ALPHA:2:1}_{TXTSUBSTR:ALPHA:3:1}. {TXTSUBSTR:ALPHA:4:1}_{TXTSUBSTR:ALPHA:5:1}_{TXTSUBSTR:ALPHA:6:1}_{TXTSUBSTR:ALPHA:7:1}'
    Quick Input, '{TXT:ALPHA}'
Else If Text Compare : [{CMDSEGMENT:0}] Equals 'bravo'
    Say, '{TXTSUBSTR:BRAVO:0:1}_{TXTSUBSTR:BRAVO:1:1}_{TXTSUBSTR:BRAVO:2:1}_{TXTSUBSTR:BRAVO:3:1}. {TXTSUBSTR:BRAVO:4:1}_{TXTSUBSTR:BRAVO:5:1}_{TXTSUBSTR:BRAVO:6:1}_{TXTSUBSTR:BRAVO:7:1}'
    Quick Input, '{TXT:BRAVO}'
Else If Text Compare : [{CMDSEGMENT:0}] Equals 'charlie'
    Say, '{TXTSUBSTR:CHARLIE:0:1}_{TXTSUBSTR:CHARLIE:1:1}_{TXTSUBSTR:CHARLIE:2:1}_{TXTSUBSTR:CHARLIE:3:1}. {TXTSUBSTR:CHARLIE:4:1}_{TXTSUBSTR:CHARLIE:5:1}_{TXTSUBSTR:CHARLIE:6:1}_{TXTSUBSTR:CHARLIE:7:1}'
    Quick Input, '{TXT:CHARLIE}'
End Condition
Write [Blue] 'ALPHA = {TXT:ALPHA}, BRAVO = {TXT:BRAVO}, CHARLIE = {TXT:CHARLIE}' to log

It works good, but when doing a Test Run of the C# function I am seeing an error that says "Exception has been thrown by the target of an invocation. Object reference not set to an instance of an object." I don't have experience with programming, so I have no idea what it means. Is this something I should worry about?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4758
  • RTFM
Re: Setting Text from Website as a Variable?
« Reply #3 on: June 02, 2019, 09:44:44 AM »
It works good, but when doing a Test Run of the C# function I am seeing an error that says "Exception has been thrown by the target of an invocation. Object reference not set to an instance of an object." I don't have experience with programming, so I have no idea what it means. Is this something I should worry about?

That's perfectly normal.


The "~" prefix for the "~webData" value means that value only exists while that particular command is actively running (it's good practice to have the command clean up after itself like this, if you don't need to use the data elsewhere).

When you test run the function, you're not executing the command, meaning the "Set Text [~webData] to [https://nukacrypt.com/]" action never runs to set that value, so "VA.GetText("~webData")" returns null (meaning nothing), so attempting to use the Split() method will throw that exception.

frankly

  • Newbie
  • *
  • Posts: 17
Re: Setting Text from Website as a Variable?
« Reply #4 on: June 22, 2021, 03:51:04 PM »
@Pfeil

do you have, by any chance, a script to pull the summary-text of a given wikipedia page?
That way alexa-style-what-is?-commands would work!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4758
  • RTFM
Re: Setting Text from Website as a Variable?
« Reply #5 on: June 22, 2021, 06:21:40 PM »
I don't, no.

There is an API that allows for that relatively easily, shown in the accepted answer here, which you could query using the "Value from file/URI" option of the "Set a Text Value" action, and then parse to get the relevant "extract" element, while replacing escape characters (like '\n') with their normal counterparts, and removing any semicolons (as those would be interpreted by the "Say Something with Text-To-Speech" action as a phrase variation)

However, retrieving the actual data is only part of the problem; if you want to be able to speak an arbitrary subject, and have a relevant article retrieved, you'd need a speech recognition system that's designed for that.
Wikipedia does have a search API that would allow you to find articles related to a given phrase, but especially for names or terminology that aren't in the speech recognition dictionary, you won't be able to get them recognized in the first place.
"Virtual assistants" like Alexa and Siri rely on gigantic datasets and complex algorithms to process their queries, which is not something you'll likely be able to emulate.