Click here to Skip to main content
15,892,537 members
Articles / Desktop Programming / Windows Forms

Manage YouTube using C# and Youtube API 1.6

Rate me:
Please Sign up or sign in to vote.
4.96/5 (30 votes)
7 Jan 2011CPOL8 min read 254.2K   18.9K   61  
I describe some of the things about Youtube video feeds
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;

namespace Manager
{
    public partial class UserAccount : Form
    {
        public UserAccount()
        {
            InitializeComponent();
        }

        //variables
        #region variables

        AddNewPlaylist newpl;

        YouTubeRequestSettings settings;
        YouTubeRequest request;

        Feed<Video> videofeed;
        Feed<Playlist> playlistfeed;

        Playlist p;

        //insert your dev key here
        string devkey = "";
        string username;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        string feedUrl;

        int lvcount = 0;
        int videofeedcount = 0;
        int videocount = 0;
        int selindex = 0;
        int totalpages = 0;
        int curpage = 1;
        int choice = 0;

        string[] medialocations;
        string[] thumblocations;
        string[] descriptions;
        string[] titles;
        string[] videoIDs;

        bool clicked = false;
        string wait = "";

        string playlisttitle = "";
        string playlistsummary = "";

        #endregion

        //functions
        #region function

        //initialize
        public void InitializeYouTube()
        {
            settings = new YouTubeRequestSettings("You Manager", devkey, username, password);
            request = new YouTubeRequest(settings);

            feedUrl = "";
            medialocations = new string[25];
            thumblocations = new string[25];
            descriptions = new string[25];
            titles = new string[25];
            videoIDs = new string[25];
        }

        //trim username
        public string GetUserName()
        {
            string newusername = "";
            int i = 0;
            if (username.Contains("@"))
            {
                foreach (char ch in username)
                {

                    if (ch == '@')
                    { break; }
                    i++;
                }
                newusername = username.Substring(0, i);
            }
            else
                newusername = username;
            return newusername;
        }

        //get playlist
        public void GetPlaylist()
        {
            string user = GetUserName();
            playlistfeed = request.GetPlaylistsFeed(user);
            if (playlistfeed.TotalResults == 0)
            {
                MessageBox.Show("You dont have any playlists");
                cmbVserviceType.SelectedIndex = 0;
            }
            else
            {
                cmbSubService.Items.Clear();
                foreach (Playlist pl in playlistfeed.Entries)
                {
                    cmbSubService.Items.Add(pl.Title);
                }
                cmbSubService.SelectedIndex = 0;
            }
        }

        //get videos of playlist feed
        public void DisplayPlaylist()
        {
            int i = 0;
            foreach (Playlist pl in playlistfeed.Entries)
            {
                p = pl;
                if (i == cmbSubService.SelectedIndex)
                {
                    p = pl;
                    break;
                }
                i++;
            }

            string[] temp = p.Id.Split(':');
            int ind = temp.GetUpperBound(0);
            feedUrl = "http://gdata.youtube.com/feeds/api/playlists/"+temp[ind]+"?v=2";
            Clipboard.SetText(feedUrl);
            GetVideoFeeds();
            /*Feed<PlayListMember> pm = request.GetPlaylist(p);
            lvcount = 0;
            videocount = 1;
            lvDisplay.Items.Clear();
            foreach (Video entry in pm.Entries)
            {
                ListDisplay(entry);
                videocount++;
            }*/

        }

        //Get video Feeds
        public void GetVideoFeeds()
        {
            Clipboard.SetText(feedUrl);
            videofeed = request.Get<Video>(new Uri(feedUrl));
            videofeedcount = videofeed.TotalResults;
            lvDisplay.Items.Clear();
            if (videofeedcount == 0)
            {
                pbThumnail.ImageLocation = "";
                lbTitle.Text = "";
                rtDesc.Text = "";
                MessageBox.Show("There is nothing to display");
            }
            else
            {
                totalpages = videofeedcount / 25;
                if (videofeedcount % 25 != 0)
                    totalpages = totalpages + 1;
                curpage = 1;
                for (int i = 1; i <= totalpages; i++)
                {
                    cmbJmp.Items.Add(i.ToString());
                }
                videocount = 1;
                DisplayFeed();
            }
        }

        //Display video feeds
        public void DisplayFeed()
        {
            lblPageNumber.Text = "Page: " + curpage + " of " + totalpages;
            lvcount = 0;
            lvDisplay.Items.Clear();
            foreach (Video entry in videofeed.Entries)
            {
                ListDisplay(entry);
                videocount++;
            }
        }

        //lv Display List of videos
        public void ListDisplay(Video video)
        {

            int slno = videocount, i = 0;
            lvDisplay.Items.Add(slno.ToString());
            lvDisplay.Items[lvcount].SubItems.Add(video.Title);
            lvDisplay.Items[lvcount].SubItems.Add(video.RatingAverage.ToString());
            lvDisplay.Items[lvcount].SubItems.Add(video.ViewCount.ToString());
            titles[lvcount] = video.Title;
            descriptions[lvcount] = video.Description;
            videoIDs[lvcount] = video.VideoId;
            foreach (Google.GData.YouTube.MediaContent mediaContent in video.Contents)
            {
                if (i == 1)
                {
                    break;
                }

                medialocations[lvcount] = mediaContent.Url;
                i++;
            }
            i = 0;
            foreach (MediaThumbnail thumbnail in video.Thumbnails)
            {
                if (i == 0)
                {
                    thumblocations[lvcount] = thumbnail.Url;
                    break;
                }
            }

            lvcount++;
        }

        #endregion



        //events
        #region events


        //form load
        private void UserAccount_Load(object sender, EventArgs e)
        {
            cmbVserviceType.SelectedIndex = 0;
            InitializeYouTube();
            feedUrl = "http://gdata.youtube.com/feeds/api/users/default/uploads";
            
        }

        //selecting service type
        private void cmbVserviceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (cmbVserviceType.SelectedIndex == 0)
            {
                rtDesc.ReadOnly = false;
                button2.Enabled = false;
                choice = 0;
                cmbSubService.Enabled = false;
                feedUrl = "http://gdata.youtube.com/feeds/api/users/default/uploads";
            }
            else if(cmbVserviceType.SelectedIndex==1)
            {
                rtDesc.ReadOnly = true;
                button2.Enabled = false;
                choice = 0;
                cmbSubService.Enabled = false;
                feedUrl = "http://gdata.youtube.com/feeds/api/users/default/favorites";
            }
            else if (cmbVserviceType.SelectedIndex == 2)
            {
                rtDesc.ReadOnly = true;
                button2.Enabled = true;
                choice = 1;
                cmbSubService.Enabled = true;
                //feedUrl = "http://gdata.youtube.com/feeds/api/users/default/playlists";
                GetPlaylist();
            }
            else
            {
                MessageBox.Show("Invalid choice");
            }
        }

        // get video feed
        private void btnGetVideos_Click(object sender, EventArgs e)
        {
            if (choice == 0)
            {
                GetVideoFeeds();
            }
            else if (choice == 1)
            {
                DisplayPlaylist();
                
            }
        }

        //lv display mouse click
        private void lvDisplay_MouseClick(object sender, MouseEventArgs e)
        {
            clicked = true;
            foreach (int i in lvDisplay.SelectedIndices)
            {
                selindex = i;
            }
            panel1.Enabled = true;
            lbTitle.Text = titles[selindex];
            pbThumnail.ImageLocation = thumblocations[selindex];
            rtDesc.Text = descriptions[selindex];
        }

        //play video
        private void pbThumnail_Click(object sender, EventArgs e)
        {
            YPlayer yp = new YPlayer();
            yp.Url = medialocations[selindex];
            yp.Show();
        }

        //page next
        private void btnNext_Click(object sender, EventArgs e)
        {
            clicked = false;
            int sindex = videofeed.StartIndex;
            string tempUrl = "";
            curpage++;
            if (curpage <= totalpages)
            {
                sindex += 25;
                videocount = sindex;
                if (feedUrl.Contains('?'))
                {
                    tempUrl = feedUrl + "&start-index=" + sindex.ToString();
                }
                else
                {
                    tempUrl = feedUrl + "?start-index=" + sindex.ToString();
                }
                Clipboard.SetText(tempUrl);
                videofeed = request.Get<Video>(new Uri(tempUrl));
                DisplayFeed();
            }
            else
            {
                MessageBox.Show("This is the last page");
                curpage--;
            }
        }

        //previous page
        private void btnPrev_Click(object sender, EventArgs e)
        {
            clicked = false;
            int sindex = videofeed.StartIndex;
            string tempUrl = "";
            curpage--;
            if (curpage >= 1)
            {
                sindex -= 25;
                videocount = sindex;
                if (feedUrl.Contains('?'))
                {
                    tempUrl = feedUrl + "&start-index=" + sindex.ToString();
                }
                else
                {
                    tempUrl = feedUrl + "?start-index=" + sindex.ToString();
                }
                Clipboard.SetText(tempUrl);
                videofeed = request.Get<Video>(new Uri(tempUrl));
                DisplayFeed();
            }
            else
            {
                MessageBox.Show("This is the first page");
                curpage++;
            }
        }

        //jump to page
        private void cmbJmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            clicked = false;
            int sindex = videofeed.StartIndex;
            string tempUrl = "";
            int i = cmbJmp.SelectedIndex;
            if (i < 0 && i > totalpages - 1)
            {
                MessageBox.Show("Select an appropriate page");
            }
            else
            {
                sindex = i * 25 + 1;
                curpage = i + 1;
                videocount = sindex;
                if (feedUrl.Contains('?'))
                {
                    tempUrl = feedUrl + "&start-index=" + sindex.ToString();
                }
                else
                {
                    tempUrl = feedUrl + "?start-index=" + sindex.ToString();
                }
                Clipboard.SetText(tempUrl);
                videofeed = request.Get<Video>(new Uri(tempUrl));
                DisplayFeed();
            }
        }

        //deleting video
        private void button1_Click(object sender, EventArgs e)
        {
            if (clicked == true)
            {
                int index = 0;
                Video video = new Video();
                foreach (Video entry in videofeed.Entries)
                {
                    if (index == selindex)
                    {
                        video = entry;
                        break;
                    }
                    index++;
                }
                request.Delete<Video>(video);
                MessageBox.Show("Video Successfully deleted");
                clicked = false;
                GetVideoFeeds();

            }
            else
                MessageBox.Show("Select a video first");
        }

        //deleting playlist
        private void button2_Click(object sender, EventArgs e)
        {
            if (cmbVserviceType.SelectedIndex == 2)
            {
                int i = cmbSubService.SelectedIndex;
                int index = 0;
                foreach (Playlist dpl in playlistfeed.Entries)
                {
                    if (index == i)
                    {
                        p = dpl;
                        break;
                    }
                    index++;
                }
                request.Delete<Playlist>(p);
                MessageBox.Show("Playlist successfully deleted");
                GetPlaylist();
                button2.Enabled = false;
            }
            else
                MessageBox.Show("Select a playlist");

        }

        //updating video
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int index = 0;
            Video video = new Video();
            foreach (Video entry in videofeed.Entries)
            {
                if (index == selindex)
                {
                    video = entry;
                    break;
                }
                index++;
            }
            video.Description = rtDesc.Text;
            try
            {
                request.Update<Video>(video);
                MessageBox.Show("Video successfully Updated");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error");
            }
        }

        //add playlist
        private void button3_Click(object sender, EventArgs e)
        {
            newpl = new AddNewPlaylist();
            newpl.Show();

            wait = "AddPlaylist";

            timer1.Enabled = true;
            timer1.Interval = 1000 * 10;
        }

        //btn upload
        private void btnUpload_Click(object sender, EventArgs e)
        {
            Upload upload = new Upload();
            upload.Username = username;
            upload.Password = password;
            upload.Devkey = devkey;
            upload.Show();
        }

#endregion


        //times
        #region timer
        //add playlist
        private void timer1_Tick(object sender, EventArgs e)
        {
            playlistsummary=newpl.Playlistsummary;
            playlisttitle = newpl.Playlisttitle;
            if (playlisttitle != null)
            {
                    Playlist pl1 = new Playlist();
                    pl1.Title = playlisttitle;
                    pl1.Summary = playlistsummary;
                    Playlist cpl = request.Insert<Playlist>(new Uri("http://gdata.youtube.com/feeds/api/users/default/playlists"), pl1);
                    PlayListMember pm = new PlayListMember();
                    MessageBox.Show("Playlist successfully created");
                    newpl.Done = true;
                    newpl.Close();
                    timer1.Enabled = false;
            }
        }
        #endregion

        

        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelancer
India India
I completed my engineering in 2010.. Then i am working as freelancer.

Comments and Discussions