public void UploadAllAudios(List<AudioUoW> audios, OauthTokenModel otm, AllAudiosUploadedEventHandler allAudiosUploaded = null,
            AudioUploadedEventHandler audioUploaded = null, Action<string> feedbackMethod = null)
        {
            try
            {
                var sessionId = Guid.NewGuid().ToString();
                _sessions.Add(sessionId, new AudioUploadSession() { Count = 0, AllAudiosUploaded = allAudiosUploaded });
                foreach (var audio in audios)
                {
                    audio.SessionId = sessionId;

                    var list = new List<AudioUploadedEventHandler>();
                    if (audioUploaded != null)
                        list.Add(audioUploaded);
                    list.Add(AudioUploaded);

                    if (feedbackMethod != null)
                        feedbackMethod("Upload started... " + audio.Title);

                    _sessions[sessionId].Count++;
                    _ans.UploadAudio(audio, otm, list);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
            }
        }
Beispiel #2
0
        public OauthTokenModel ToOauthTokenModel()
        {
            var ytm = new OauthTokenModel
            {
                AccessToken       = access_token,
                TokenType         = scope,
                AccessTokenExpiry = DateTime.MaxValue
            };

            return(ytm);
        }
        public OauthTokenModel ToOauthTokenModel()
        {
            var ytm = new OauthTokenModel
            {
                AccessToken = access_token,
                TokenType = scope,
                AccessTokenExpiry = DateTime.MaxValue
            };

            return ytm;
        }
        public OauthTokenModel ToOauthTokenModel()
        {
            var ytm = new OauthTokenModel
            {
                AccessToken = access_token,
                TokenType = token_type,
                //RefreshToken = refresh_token,
                AccessTokenExpiry = DateTime.UtcNow.AddSeconds(expires_in)
            };

            return ytm;
        }
Beispiel #5
0
        public OauthTokenModel ToOauthTokenModel()
        {
            var ytm = new OauthTokenModel
            {
                AccessToken = access_token,
                TokenType   = token_type,
                //RefreshToken = refresh_token,
                AccessTokenExpiry = DateTime.UtcNow.AddSeconds(expires_in)
            };

            return(ytm);
        }
 public OauthTokenModel GetOauthTokenModel()
 {
     try
     {
         return _otm ?? (_otm = _ss.Load().ToYouTubeOauthTokenModel());
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message, ex);
         return null;
     }
 }
        public OauthTokenModel RefreshRequestTokens(OauthTokenModel ytm)
        {
            try
            {
                using (var wb = new WebClient())
                {
                    var data = new NameValueCollection();
                    data["client_id"] = _aks.GetAPIKeys().YouTubeClientId;
                    data["client_secret"] = _aks.GetAPIKeys().YouTubeClientSecret;
                    data["refresh_token"] = ytm.RefreshToken;
                    data["grant_type"] = "refresh_token";

                    var response = wb.UploadValues(OAUTH_URL, "POST", data);
                    var responseStr = Encoding.ASCII.GetString(response);
                    var oauthResponse = JsonConvert.DeserializeObject<youtube_oauth_response>(responseStr);
                    oauthResponse.refresh_token = ytm.RefreshToken;

                    return oauthResponse.ToOauthTokenModel();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
                return null;
            }
        }
        public async void UploadVideo(AudioUoW audio, OauthTokenModel otm, List<VideoUploadedEventHandler> videoUploaded = null)
        {
            try
            {
                Video returnedVideo = null;
                using (var stream = File.OpenRead(audio.VideoPath))
                {

                    var video = new Video
                    {
                        Snippet = new VideoSnippet
                        {
                            Title = audio.Title,
                            Description = audio.Description,
                            Tags = audio.Tags,
                            //TODO get category list info
                            CategoryId = "22"
                        },
                        Status = new VideoStatus
                        {
                            PrivacyStatus = "unlisted",
                            Embeddable = true,
                            License = "youtube"
                        }
                    };

                    var headers = new Dictionary<string, string>();

                    headers["Authorization"] = otm.TokenType + " " + otm.AccessToken;
                    headers["X-Upload-Content-Length"] = stream.Length.ToString();
                    headers["x-upload-content-type"] = "application/octet-stream";

                    IJsonSerializer js = new NewtonsoftJsonSerializer();
                    var videoData = js.Serialize(video);

                    var response =
                        await
                            WebHelper.GetRawResponsePost(
                                "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails",
                                videoData, headers);

                    var uploadUrl = response.Headers.Location;

                    returnedVideo = await WebHelper.Post<Video>(
                        uploadUrl.AbsoluteUri,
                        stream, headers);
                    audio.YouTubeUrl = "https://www.youtube.com/watch?v=" + returnedVideo.Id;
                }

                if (videoUploaded == null) return;

                foreach (var vu in videoUploaded)
                {
                    vu(this, new VideoUploadedEventArgs() {Audio = audio});
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
            }
        }
 public void SaveOauthResponse(OauthTokenModel otm)
 {
     try
     {
         var sm = _ss.Load();
         otm.UpdateYouTubeStorage(sm);
         _ss.Save(sm);
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message, ex);
     }
 }
 public void UploadVideo(AudioUoW audio, OauthTokenModel otm, VideoUploadedEventArgs videoUploaded = null)
 {
     try
     {
         _vns.UploadVideo(audio, otm, new List<VideoUploadedEventHandler>());
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message, ex);
     }
 }
        public void UploadAllVideos(List<AudioUoW> audios, OauthTokenModel otm, AllVideosUploadedEventHandler allVideosUploaded = null,
            VideoUploadedEventHandler videoUploaded = null, Action<string> feedbackMethod = null)
        {
            try
            {
                var sessionId = audios.First().SessionId;
                var session = _sessions[sessionId];
                session.AllVideosUploaded = allVideosUploaded;
                session.Count = 0;
                foreach (var audio in audios)
                {
                    var list = new List<VideoUploadedEventHandler>();
                    if (videoUploaded != null)
                        list.Add(videoUploaded);
                    list.Add(VideoUploaded);

                    if (feedbackMethod != null)
                        feedbackMethod("Upload started... " + audio.Title);

                    _sessions[sessionId].Count++;
                    _vns.UploadVideo(audio, otm, list);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
            }
        }
 public void MakePost(OauthTokenModel otm, AudioUoW audio)
 {
     try
     {
         var fc = new FacebookClient(otm.AccessToken);
         dynamic response = fc.Post("/me/feed", new { message = audio.SocialMediaMessage, link = audio.SoundCloudUrl });
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message, ex);
     }
 }
        public async void UploadAudio(AudioUoW audio, OauthTokenModel otm, List<AudioUploadedEventHandler> audioUploaded = null)
        {
            try
            {
                var buffer = new byte[16*1024];
                using (var stream = File.OpenRead(audio.AudioPath))
                {
                    using (var ms = new MemoryStream())
                    {
                        int read;
                        while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        buffer = ms.ToArray();
                    }
                }

                var dict = new Dictionary<string, object>
                {
                    {"track[title]", audio.Title},
                    {"track[description]", audio.Description},
                    {"track[tag_list]", _ms.GenerateTagsString(audio.Tags, ' ')}
                };

                var fileData = new Dictionary<string, byte[]> {{"track[asset_data]", buffer}};

                var track = await WebHelper.PostFile<soundcloud_track>(API_URL + "/tracks.json?oauth_token=" + otm.AccessToken, dict, fileData);
                audio.SoundCloudUrl = track.permalink_url;

                if (audioUploaded == null) return;

                foreach (var au in audioUploaded)
                {
                    au(this, new AudioUploadedEventArgs() { Audio = audio });
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
            }
        }