Exemplo n.º 1
0
        public void RevokeAccount(IYoutubeAccountContainer container, IYoutubeAccount account)
        {
            LOGGER.Info($"Revoking connecgtion to account with id: {account.Id} and title: '{account.Title}'");

            YoutubeAccountService.RevokeAccessOfAccount(account);
            container.UnregisterAccount(account);
        }
Exemplo n.º 2
0
 public void UnregisterAccount(IYoutubeAccount account)
 {
     if (Accounts.Contains(account))
     {
         LOGGER.Debug($"Removing account, title: '{account.Title}'");
         Accounts.Remove(account);
     }
 }
Exemplo n.º 3
0
 public void RegisterAccount(IYoutubeAccount account)
 {
     if (!RegisteredAccounts.Any(r => r.Id == account.Id))
     {
         LOGGER.Debug($"Adding a new account, title: '{account.Title}'");
         Accounts.Add(account);
     }
 }
Exemplo n.º 4
0
        public static void Send(IYoutubeAccount from, string to, string title, string message)
        {
            LOGGER.Info($"Sending mail from youtube account {from.Title} to recipient {to} with title {title}");
            LOGGER.Debug($"Message content: {message}");

            var token = YoutubeAccountService.GetAccessToken(from.Access, ac => ac.HasSendMailPrivilegue);

            if (!string.IsNullOrWhiteSpace(token) && !string.IsNullOrWhiteSpace(to))
            {
                LOGGER.Debug($"Access token found. Sending mail");

                // Wir können senden!
                HttpWebRequest request = HttpWebRequestCreator.CreateWithAuthHeader(
                    $"https://www.googleapis.com/gmail/v1/users/me/messages/send?key={from.Access.First(a => a.AccessToken == token).Client.Secret}",
                    "POST",
                    token
                    );
                request.ContentType = "application/json";

                string content = GenerateMail(to, title, message, true);

                RawMail mail = new RawMail()
                {
                    raw = Encode(content)
                };

                var rfcMail = JsonConvert.SerializeObject(mail);

                LOGGER.Debug($"rfc mail json: {rfcMail}");

                string     result     = null;
                SendResult sendResult = null;

                try
                {
                    result = WebService.Communicate(request, Encoding.UTF8.GetBytes(rfcMail));
                    LOGGER.Debug($"response from mail service: {result}");

                    sendResult = JsonConvert.DeserializeObject <SendResult>(result);
                }
                catch (Exception ex)
                {
                    LOGGER.Error($"Exception occured while sending the mail", ex);
                }

                if (sendResult == null || !sendResult.labelIds.Any(label => label.ToUpper() == "SENT"))
                {
                    LOGGER.Error($"Could not send the mail. Result: {result}");
                }
                else
                {
                    LOGGER.Error($"Mail was sent successfully");
                }
            }
        }
Exemplo n.º 5
0
        internal static void RevokeAccessOfAccount(IYoutubeAccount account)
        {
            LOGGER.Info($"Revoking all {account.Access.Count} accesses of account with id: {account.Id} and title: '{account.Title}'");

            while (account.Access.Count > 0)
            {
                RevokeSingleAccess(account, account.Access[0]);
            }

            LOGGER.Info($"All accesses revoked");
        }
Exemplo n.º 6
0
        public RefreshPlaylistsForm(PlaylistPersistor playlistPersistor, IYoutubeAccount account)
        {
            LOGGER.Info($"Initializing new instance of RefreshPlaylistsForm");

            InitializeComponent();

            PlaylistPersistor = playlistPersistor;
            Account           = account;

            RefreshListView();
        }
Exemplo n.º 7
0
        public YoutubeJob(IYoutubeVideo video, IYoutubeAccount account, UploadStatus uploadStatus)
        {
            LOGGER.Info($"Creating new job for video '{video?.Title ?? "null"}' and account '{account?.Title ?? "null"}'");

            Video        = video;
            Account      = account;
            UploadStatus = uploadStatus;

            JobUploader = new JobUploader(this);
            JobUploader.StateChanged += JobUploaderStateChanged;
        }
Exemplo n.º 8
0
        public AutomationUploader(IYoutubeUploader uploader, IYoutubeJobContainer archiveContainer, IPlaylistServiceConnectionContainer pscContainer, IYoutubeAccount account, IEnumerable <IObservationConfiguration> configurationsToAdd)
            : this(uploader, archiveContainer, pscContainer)
        {
            LOGGER.Debug($"Creating new instance of AutomationUploader with account with title {account?.Title ?? null}");
            Account = account;

            foreach (var config in configurationsToAdd)
            {
                LOGGER.Debug($"Adding configuration for template '{config.Template.Name}' and path '{config.PathInfo.Fullname}'");
                Configuration.Add(config);
            }
        }
Exemplo n.º 9
0
        internal static void RevokeSingleAccess(IYoutubeAccount account, IYoutubeAccountAccess access)
        {
            LOGGER.Info($"Revoking single access of account with id: {account.Id} and title: '{account.Title}'");

            string address = $"https://accounts.google.com/o/oauth2/revoke?token={access.RefreshToken}";

            WebRequest request = WebRequest.Create(address);

            request.ContentType = "application/x-www-form-urlencoded";

            WebService.Communicate(request);

            account.Access.Remove(access);
        }
Exemplo n.º 10
0
        public AddVideosForm(ITemplate[] templates, IPath[] pathInfos, IYoutubeCategoryContainer categoryContainer, IYoutubeLanguageContainer languageContainer, IYoutubePlaylistContainer playlistContainer, IPlaylistServiceConnectionContainer pscContainer, IYoutubeAccount account)
        {
            InitializeComponent();
            editVideoInformationGrid.IsNewUpload = true;

            DialogResult = DialogResult.Cancel;

            CategoryContainer = categoryContainer;
            LanguageContainer = languageContainer;
            PlaylistContainer = playlistContainer;
            PscContainer      = pscContainer;
            Account           = account;
            Templates         = templates;
            PathInfos         = pathInfos;
        }
Exemplo n.º 11
0
        public IYoutubeAccount ConnectToAccount(string code, bool mailsAllowed, IYoutubeClient client, YoutubeRedirectUri redirectUri)
        {
            LOGGER.Info($"Connecting to account, mails allowed: {mailsAllowed}, redirect uri: {redirectUri}");

            var    uri     = redirectUri.GetAttribute <EnumMemberAttribute>().Value;
            string content = $"code={code}&client_id={client.Id}&client_secret={client.Secret}&redirect_uri={uri}&grant_type=authorization_code";
            var    bytes   = Encoding.UTF8.GetBytes(content);

            // Request erstellen
            WebRequest request = WebRequest.Create("https://www.googleapis.com/oauth2/v4/token");

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            string result = WebService.Communicate(request, bytes);

            QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

            IYoutubeAccount account      = null;
            var             authResponse = JsonConvert.DeserializeObject <YoutubeAuthResponse>(result);

            IYoutubeAccountAccess access = new YoutubeAccountAccess();

            access.Client = client;
            access.HasSendMailPrivilegue = mailsAllowed;

            if (authResponse != null && !string.IsNullOrWhiteSpace(authResponse.access_token))
            {
                access.AccessToken    = authResponse.access_token;
                access.RefreshToken   = authResponse.refresh_token;
                access.TokenType      = authResponse.token_type;
                access.ExpirationDate = DateTime.Now.AddSeconds(authResponse.expires_in);
                access.ClientId       = client.Id;

                LOGGER.Info($"Connection successful, loading account details");

                var accountDetails = GetAccountDetails(access);
                var acc            = accountDetails.items.First();
                account = YoutubeAccount.Create(acc.id, acc.snippet.country, acc.snippet.title);

                account.Access.Add(access);
            }

            LOGGER.Info($"Connected to account with id: {account.Id} and title: '{account.Title}'");

            return(account);
        }
Exemplo n.º 12
0
        public IList <IYoutubePlaylist> LoadPlaylists(IYoutubeAccount account)
        {
            LOGGER.Info($"Loading playlists of account with id: '{account.Id}' and title: '{account.Title}' from youtube");

            var list = new List <IYoutubePlaylist>();

            string nextPageToken = null;

            do
            {
                string         pagetoken = !string.IsNullOrWhiteSpace(nextPageToken) ? $"&pageToken={nextPageToken}" : string.Empty;
                string         url       = $"https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&maxResults=50&mine=true{pagetoken}&key={YoutubeClientData.YoutubeApiKey}";
                HttpWebRequest request   = (HttpWebRequest)WebRequest.Create(url);
                request.Method          = "GET";
                request.Credentials     = CredentialCache.DefaultCredentials;
                request.ProtocolVersion = HttpVersion.Version11;
                request.Accept          = "application/json";

                // Header schreiben
                request.Headers.Add($"Authorization: Bearer {account.Access.GetActiveToken()}");

                var result = WebService.Communicate(request);
                QuotaProblemHandler.ThrowOnQuotaLimitReached(result);

                Response response = JsonConvert.DeserializeObject <Response>(result);

                nextPageToken = response.nextPageToken;
                foreach (var serializedPlaylist in response.items)
                {
                    list.Add(new YoutubePlaylist()
                    {
                        Title       = serializedPlaylist.snippet.title,
                        Id          = serializedPlaylist.id,
                        PublishedAt = serializedPlaylist.snippet.publishedAt
                    });
                }
            } while (!string.IsNullOrWhiteSpace(nextPageToken));

            foreach (var playlist in list)
            {
                LOGGER.Info($"Loaded playlist with id: {playlist.Id} and title: '{playlist.Title}'. Playlist has been published at: '{playlist.PublishedAt}'");
            }

            return(list);
        }
Exemplo n.º 13
0
 public static string GetActiveToken(this IYoutubeAccount account)
 {
     LOGGER.Debug($"Returning active access token for account with id: {account.Id} and title: '{account.Title}'");
     return(YoutubeAccountService.GetAccessToken(account));
 }
Exemplo n.º 14
0
 public static string GetAccessToken(IYoutubeAccount account)
 {
     return(GetAccessToken(account.Access));
 }
Exemplo n.º 15
0
        public static HttpWebRequest CreateForResumeUpload(Uri uri, IYoutubeVideo video, IYoutubeAccount account, long lastbyte)
        {
            LOGGER.Info($"Creating web request for resumable upload of video: '{video.Path}' and account with id: {account.Id} and title: '{account.Title}' from last byte: {lastbyte}");

            var request = CreateWithAuthHeader(uri.AbsoluteUri, "PUT", account.GetActiveToken());

            request.Headers.Add($"Content-Range: bytes {lastbyte + 1}-{video.File.Length - 1}/{video.File.Length}");
            request.ContentLength = video.File.Length - (lastbyte + 1);

            LOGGER.Info($"Content-Range header: 'bytes {lastbyte + 1}-{video.File.Length - 1}/{video.File.Length}'");
            LOGGER.Info($"Content-Length header: {video.File.Length - (lastbyte + 1)}");

            // Am Leben halten (wichtig bei großen Dateien)!
            request.ServicePoint.SetTcpKeepAlive(true, 10000, 1000);
            request.AllowWriteStreamBuffering = false;
            request.Timeout = int.MaxValue;

            return(request);
        }
Exemplo n.º 16
0
        public static HttpWebRequest CreateForNewUpload(Uri uri, IYoutubeVideo video, IYoutubeAccount account)
        {
            LOGGER.Info($"Creating web request for video: '{video.Path}' and account with id: {account.Id} and title: '{account.Title}'");

            var request = CreateWithAuthHeader(uri.AbsoluteUri, "PUT", account.GetActiveToken());

            request.ContentLength = video.File.Length;
            request.ContentType   = MimeMapping.GetMimeMapping(video.File.Name);

            // Am Leben halten (wichtig bei großen Dateien)!
            request.ServicePoint.SetTcpKeepAlive(true, 10000, 1000);
            request.AllowWriteStreamBuffering = false;
            request.Timeout = int.MaxValue;

            return(request);
        }