Hi, I’m new to AppleScript and am looking for some help.
Basically I want to have a script that will go to a specified YouTube video and retrieve the number of views it has had.
From there I will send that information to an Arduino (I have found code for sending stuff to Arduino, it’s the YouTube bit that I am stuck on).
Anybody interested in helping me out?
Much appreciated.
Phil
Try this:
tell application "Safari"
set URL of document 1 to "http://www.youtube.com/watch?v=N01vThrQ40Q&feature=results_main&playnext=1&list=PL21722A438B9EC13C"
delay 3
set viewCount to (do JavaScript "document.getElementById('watch-actions-right').childNodes[1].childNodes[1].innerText" in document 1)
end tell
Thanks for the quick reply, when I run the script it lunches Safari and goes to YouTube but returns : missing value
Also, I really need the script to get the view count without launching a browser.
In python, the method is this:
Make a HTTP GET request to the YouTube API -
for example, http://gdata.youtube.com/feeds/api/videos?q=DwJvUMfel7E
– this gets a bunch of data about the video (and should be viewable in a standard web browser too)
Then Extract the ‘media:title’ and ‘yt:statistics viewCount’ contents from the XML
Any ideas?
Thanks.
Hi,
try this, the script assumes that the xml structure is always the same
property ytURL : "http://gdata.youtube.com/feeds/api/videos?q=DwJvUMfel7E"
set tempFile to (path to temporary items as text) & (do shell script "/bin/date +%Y%m%d%H%M%S")
do shell script "curl " & quoted form of ytURL & " > " & quoted form of POSIX path of tempFile
tell application "System Events"
set xmlData to contents of XML file tempFile
set entryTag to XML element "entry" of XML element 1 of xmlData
set theTitle to value of XML element "media:title" of XML element "media:group" of entryTag
set viewCount to value of XML attribute "viewcount" of XML element "yt:statistics" of entryTag
end tell
do shell script "rm " & quoted form of POSIX path of tempFile
display dialog "title: " & theTitle & return & "view count: " & viewCount
This works perfectly thanks for your help.
I’m gonna be digging into the code to send it to the Arduino soon and maybe asking for more help.
Thanks.
Phil
Here is another approach :
set viewCount to do shell script "curl 'http://www.youtube.com/watch?v=N01vThrQ40Q&feature=results_main&playnext=1&list=PL21722A438B9EC13C' | grep -Eo '<strong>[0-9].*</strong>' | sed s'/[</strong>]//g'"
Thanks guys, both of these work great.
Any idea how I would have the number updating ever 5 seconds?
I’m guessing it is some kind of timing loop but am not familiar enough with AppleScript to figure that out.
Cheers
Phil
repeat 10 times
set viewCount to do shell script "curl 'http://www.youtube.com/watch?v=N01vThrQ40Q&feature=results_main&playnext=1&list=PL21722A438B9EC13C' | grep -Eo '<strong>[0-9].*</strong>' | sed s'/[</strong>]//g'"
delay 5
end repeat
Actually, it seems like the count isn’t getting updated.
I tried both versions of the script and it never seems to give me the updated count.
Do we need to delete the temp file or clear some kind of cache?
If I look at the video in YouTube then I see the count has changed.
any ideas?
Phil
the youtube API reads the statistics, it doesn’t “view” the site.
I looked at the statistics and it is different to the view count on the website but oddly enough, this AppleScript still shows an old number.
property ytURL : "http://gdata.youtube.com/feeds/api/videos?q=v=ae_DKNwK_ms"
set tempFile to (path to temporary items as text) & (do shell script "/bin/date +%Y%m%d%H%M%S")
set a to 0
repeat while a = 0
do shell script "curl " & quoted form of ytURL & " > " & quoted form of POSIX path of tempFile
tell application "System Events"
set xmlData to contents of XML file tempFile
set entryTag to XML element "entry" of XML element 1 of xmlData
set theTitle to value of XML element "media:title" of XML element "media:group" of entryTag
set viewCount to value of XML attribute "viewcount" of XML element "yt:statistics" of entryTag
end tell
do shell script "rm " & quoted form of POSIX path of tempFile
display dialog "title: " & theTitle & return & "view count: " & viewCount giving up after 10
delay 1
end repeat
Hey guys!
Just wondering if there is a way to use this on multiple URLs and have it paste the title and view count to a document like Numbers or Excel?
I’m still learning but this site and everyone on it has been super helpful!
Thanks so much!
Jenn