Parsing data is one of my favourite topic in Automagic. Here I will just assume you want to retrieve some data from weather info. And then using HTTP request, will store the result in {response}. It should be longer, but let assume the response you get only like this.
Code: Select all
response = '<span class="pretty-sum">Berlin, Germany | 21 C; | Clear</span>';
You want the text inside the span tag :
Berlin, Germany | 21 C; | Clear
Parsing using split()
You can simulate the response just in script. So we assign the html we want to parse to {response}. Then do the splitting as you have done in tasker.
Code: Select all
response = '<span class="pretty-sum">Berlin, Germany | 21 C; | Clear</span>';
a = split(response, '<span class="pretty-sum">');
b = split(a[1], '</span>');
text = b[0];
When first split, {a} will contain 2 elements,
'' (blank) and
Berlin, Germany | 21 C; | Clear</span>. We only need to access second element, and automagic list start the index from 0. So second element is index 1. Hence to access the next split, we use a[1]. (2nd element in list {a}). After splitting the b, it will also result in 2 elements, and we access only the first one, so b[0]. {text} will contain
Berlin, Germany | 21 C; | Clear.
Put a condition debug dialog after the script to see the value of each variables.
Accessing variable
To access any available variable, tap the
variable button in script or expression. Or at any element editing, tap 3 dot menu >
select variable. For list/map type variable, you only can access the parent (list/map), not the child (list/map element). In this case, you can select {a}, but not the {a[0]}. You have to type the [0] by yourself. a and {a} is the same. Accessing variable usually require the braces {}. But when using it in script, expression, Control UI, extras, {} is not needed. I purposely put {a} in above example to make it clear that it is variable {a}.
Problem with split()
However, when parsing data like this, split() is not recommended, you will ended splitting so many times and the structure becomes uglier each time you need extra split. From the example above, if I want to split again, the city, country, temperature and forecast, I need to do another 2 split(). Continue from our script above.
Code: Select all
response = '<span class="pretty-sum">Berlin, Germany | 21 C; | Clear</span>';
a = split(response, '<span class="pretty-sum">');
b = split(a[1], '</span>'); // b[0] = Berlin, Germany | 21 C; | Clear
c = split(b[0], ",");
city = c[0]; // Berlin
d = split(c[1], " \\| "); // | is special char, need to double escaped using \\
country = d[0]; // Germany
temp = d[1]; // 21 C;
forecast = d[2]; // Clear
As you can see, we have to split so many times and each additional split make it more confusing. As you need more and more temporary variable to hold the data.
Better function, findAll()
Here comes the better function, use findAll(), which utilize regex parsing. The same data parsing can be done just in one-hit KO.
Code: Select all
response = '<span class="pretty-sum">Berlin, Germany | 21 C; | Clear</span>';
find = findAll(response, '<span class="pretty-sum">(.*), (.*) \\| (.*); \\| (.*)</span>', true);
city = find[0][1]; // Berlin
country = find[0][2]; // Germany
temp = find[0][3]; // 21 C
forecast = find[0][4]; // Clear
A single findAll() will automagically group all needed data in a list. The findAll() with group matching (true) will result in nested list. That's why we need extra [0] to access the first match, then another [1] to access the first group capture, city = find[0][1]. use [2], [3], [4] to access the subsequent variable. Regex parsing also much more precise. You can see the {temp} value by splitting result in
21 C; (there is additional semicolon). While using findAll(), we can exclude the ; directly, result in more precise matching,
21 C
Regex
To learn about regex, how to use the syntax or the grouping, I suggest to visit regexr.com. Automagic also has built-in regex tester that you can access from action script or condition expression. You can see more example of parsing data at my index, section HTTP request, Parsing data and Regex pattern.
Tasker also can do regex parsing, but require additional plugin in AutoTools (need to install the app). And the parsing also have to be done in each action, can't be done just in single script action with several lines. In short, sometimes you will need 3-8 actions in tasker, but you only need single script with several lines of code in Automagic. You got better overview of your variables in Automagic.