Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

Getting YouTube statistics with GetAsync (String patterns)?

Asked by
LuaQuest 450 Moderation Voter
8 years ago

This is actually more of a question of string patterns, but i'll go more in depth about my question after this code:

-- YouTube specific
local Site = "https://www.youtube.com/watch?v=9bZkp7q19f0"

local Http = game:GetService'HttpService'
local SiteHtml = GetAsync(Site)

-- Get views from video
local ViewsPattern= html:match('<div class="watch%-view%-count">%d+')

-- Prints it
print(ViewsPattern:match("%d+"))

Now, this code does work, but only for videos with less than a thousand views. Other than that, it will only return the number up to the comma.

How can i make a string pattern where the script will return the views regardless of the comma? I can't say something like "%d+%p" because not all videos will be above a thousand views. Same goes for if i'm getting a video with a million views, then it has multiple commas.

Please help.

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Lua's string patterns are based largely around "character classes" -- a "kind" of character.

%d is the class of digits, but what you want is something like "number-like" characters, which would be 0 to 9 as well as ,.

You can write character classes by listing the characters in square braces: [0123456789,]. To shorten this, you can use ranges of ASCII characters with a -: [0-9,].

You can also list the % based character classes inside: [%d,].

The final pattern would look something like like

'<div class="watch%-view%-count">[0-9,]+'

Another nice feature of pattern matching is that you can match on particular groups in the result. Instead of having to do the second match, you can just surround the [0-9,]+ in parens:

local count = html:match(`'<div class="watch%-view%-count">([0-9,]+)')
print(count) -- 1,023
-- (careful that it will include the commas)

I recommend including the closing </div> or whatever follows it, to make sure that the + actually includes the final digit!

0
Thank you. LuaQuest 450 — 8y
Ad

Answer this question