Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Virtual device retrieves data from YT


oldman

Question

Hello,

I need your help guys;)

 

I want to display the data from YT in vd: the amount of views on a particular channel and the total number of videos

 

I have a direct link that displays information in JSON:

Please login or register to see this link.

 

If I put it in web browser, I see the following information:

// 20170117085953
//

Please login or register to see this link.

{CHANNEL_ID}&key={MY_API_KEY}

{
  "kind": "youtube#channelListResponse",
  "etag": "\"gMxXHe-zin558lTnzKu8vjcmDI/2Qud556x4kgLe-969qH7-65gh7t\"",
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#channel",
      "etag": "\"gMxXHe-zinKdE9TTEzKu8vjcmDI/q3O415qUconyEsljnEa554j5mM\"",
      "id": "
{CHANNEL_ID}",
      "statistics": {
        "viewCount": "34315",
        "commentCount": "0",
        "subscriberCount": "76",
        "hiddenSubscriberCount": false,
        "videoCount": "74"
      }
    }
  ]
}

 

I want to import from the above data to VD following information:

viewCount":

videoCount":

 

I know that the connection to the data is encrypted (via https), so I created a php script and put it on my server:

<?php
$url = fopen( ‚https://www.googleapis.com/youtube/v3/channels?part=statistics&id={CHANNEL_ID}&key={MY_API_KEY}’, ‚r’ );
$wynik = ”;
while (!feof($url)) {
$wynik .= fread($url, 8192);
}
fclose( $url );
header(‚Content-Type: application/json’);
print_r($wynik);
?>

 

Then I created of virtual device on the Home Center:

HC2 = Net.FHttp(„ADDRESS_OF_MY_HOSTING”,80);
dane ,status, errorCode = HC2:GET(„/PHPFILE.php”);
ajson=json.decode(dane)
view=ajson.items[1].statistics.viewCount
fibaro:call(195,’setProperty’,’ui.Label1.value’,tostring(view))
video=ajson.items[1].statistics.videoCount
fibaro:call(195,’setProperty’,’ui.Label2.value’,tostring(video))

 

The problem is that it does not collect any data from YouTube.

In main look I see:

 

 line 2: unexpected symbol near '�'

 

If anyone has any idea where I made a mistake or how I can accomplish in a different way?

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

The topic has been moved from "

Please login or register to see this link.

" to "

Please login or register to see this link.

".

 

Temat został przeniesiony z "

Please login or register to see this link.

" do "

Please login or register to see this link.

".

Link to comment
Share on other sites

  • 0

Try using straight quotes, rather than curly ones - ie:

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0

Below my VD and scene:

 

MAIN LOOP:

--[[
%% autostart
%% properties
%% events
%% globals
--]]


local IDYT = fibaro:getSelfId() 

local viewCount_YT = fibaro:getGlobalValue("viewCount_YT") 
local subCount_YT = fibaro:getGlobalValue("subCount_YT")
local videoCount_YT = fibaro:getGlobalValue("videoCount_YT")


fibaro:call(IDYT, "setProperty", "ui.Label1.value", viewCount_YT) 
fibaro:log(viewCount_YT);
fibaro:sleep(1000)
fibaro:call(IDYT, "setProperty", "ui.Label2.value", subCount_YT)
fibaro:log(subCount_YT);
fibaro:sleep(1000)
fibaro:call(IDYT, "setProperty", "ui.Label3.value", videoCount_YT)
fibaro:log(videoCount_YT);
fibaro:sleep(1000)

fibaro:sleep(7200)


fibaro:call(195, "pressButton", "4"); --run the scene

 

SCENE:

--[[
%% autostart
%% properties
%% events
%% globals
--]]


if (fibaro:countScenes() > 2) then
  fibaro:abort();
end

 

local url = '

Please login or register to see this link.

 

function getEfergy(requestUrl, successCallback, errorCallback)
  local http = net.HTTPClient()
  http:request(requestUrl, {
      options = {
        method = 'GET',
        headers = {
        },
      },
      success = successCallback,
      error = errorCallback
  })
end

getEfergy(url,
          function(response)
            print ('Status: ' .. response.status)
            local decodedEfergy = json.decode(response.data)
            fibaro:setGlobal ('viewCount_YT', (decodedEfergy.items[1].statistics.viewCount))
            print (fibaro:getGlobalValue ('viewCount_YT'))    
               fibaro:setGlobal ('subCount_YT', (decodedEfergy.items[1].statistics.subscriberCount))
            print (fibaro:getGlobalValue ('subCount_YT'))    
            fibaro:setGlobal ('videoCount_YT', (decodedEfergy.items[1].statistics.videoCount))
            print (fibaro:getGlobalValue ('videoCount_YT'))    
            end,
                function(err)             
                  print ('error' .. err)
                end
                )

 

Cytuj

 


 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...