Monday, March 12, 2012

[YouTube-API] Using C# to retrieve YouTube Insights

Does any one have working code for retrieving Insights data for each video in a channel? 

I'm attempting to retrieve YouTube insights using C#.  I have been able to retrieve video IDs
with the code below, but I cannot retrieve the insights.  I am hoping that by using the video ID
I can do a get on the following URL:


https://insight.youtube.com/video-analytics/csvreports?query=VIDEO_ID&type=v&starttime=1330727299&endtime=1331677699&region=world&hl=en_US&userName=USERNAME&password=PASSWORD&token=DEV_TOKEN

Though this approach (and URL) doesn't seem to work.


Any ideas? A specific C# code example (that works) would be invaluable.

-------------my code sample ----------------

public class UnixTime 

    public static string ToUnixTime(DateTime dateTime) 

    { 

        return (Math.Round((dateTime - new DateTime(1970, 1, 1, 0, 0, 0).ToLocalTime()).TotalSeconds, 0)).ToString() ; 

    } 

}


public void PrepareAndDownloadFile(string username, string password, string token, string channel, string destination) 

{

YouTubeRequestSettings settings = new YouTubeRequestSettings(channel, token, username, password);
YouTubeRequest request = new YouTubeRequest(settings);
string feedUrl = String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads?v=2", channel);
Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));

foreach (Video video in videoFeed.Entries)
{
   
StringBuilder url = new StringBuilder();

    url
.Append("https://insight.youtube.com/video-analytics/csvreports");
    url
.Append(String.Format("?query={0}", video.Id));
    url
.Append("&type=v");
    url
.Append(String.Format("&starttime={0}", UnixTime.ToUnixTime(DateTime.Now.AddDays(SINCE_OFFSET))));
    url
.Append(String.Format("&endtime={0}", UnixTime.ToUnixTime(DateTime.Now.AddDays(UNTIL_OFFSET))));
    url
.Append("&region=world");
    url
.Append("&hl=en_US");
    url
.Append(String.Format("&userName={0}", username));
    url
.Append(String.Format("&password={0}", password));
    url
.Append(string.Format("&token={0}", token));

   
// make Http Request here given the new url.

   
dynamic rows = GetPath(url.ToString());
    processor
.WriteRows(destination, rows);
 
}

}

-------------end sample ----------------

--
You received this message because you are subscribed to the Google Groups "YouTube APIs Developer Forum" group.
To view this discussion on the web visit https://groups.google.com/d/msg/youtube-api-gdata/-/PxMDez7j9nQJ.
To post to this group, send email to youtube-api-gdata@googlegroups.com.
To unsubscribe from this group, send email to youtube-api-gdata+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/youtube-api-gdata?hl=en.

No comments:

Post a Comment