private PhotoInstance?GetPhotoInstance(AdferoVideoDotNet.AdferoArticles.Articles.AdferoArticle article, AdferoVideoDotNet.AdferoArticles.ArticlePhotos.AdferoArticlePhotosClient photos, AdferoVideoDotNet.AdferoPhotos.AdferoPhotoClient photoClient, string scaleAxis, int scale)
        {
            PhotoInstance?inst = null;

            AdferoVideoDotNet.AdferoArticles.ArticlePhotos.AdferoArticlePhotoList photoList = photos.ListForArticle(article.Id, 0, 100);
            if (photoList.TotalCount > 0)
            {
                AdferoVideoDotNet.AdferoArticles.ArticlePhotos.AdferoArticlePhoto apho = photos.Get(photoList.Items[0].Id);
                int photoId = apho.SourcePhotoId;
                AdferoVideoDotNet.AdferoPhotos.Photos.AdferoPhoto pho = photoClient.Photos().GetScaleLocationUrl(photoId, scaleAxis, scale);
                string photoUrl     = pho.LocationUri;
                string photoCaption = photos.Get(photoList.Items[0].Id).Fields["caption"];

                enumeratedTypes.enumPhotoOrientation ori = enumeratedTypes.enumPhotoOrientation.Landscape;
                if (scaleAxis == AdferoVideoDotNet.AdferoPhotos.Photos.AdferoScaleAxis.Y)
                {
                    ori = enumeratedTypes.enumPhotoOrientation.Portrait;
                }

                string cleanedUrl = photoUrl;
                if (cleanedUrl.IndexOf('?') >= 0)
                {
                    cleanedUrl = cleanedUrl.Substring(0, cleanedUrl.IndexOf('?'));
                }

                inst = new PhotoInstance()
                {
                    AltText             = photoCaption,
                    Caption             = photoCaption,
                    DestinationFileName = Slugify(article.Fields["title"]) + "-" + scale + Path.GetExtension(cleanedUrl),
                    Height      = 0,
                    Id          = apho.Id,
                    Orientation = ori,
                    Type        = enumeratedTypes.enumPhotoInstanceType.Custom,
                    Url         = photoUrl,
                    Width       = 0
                };
            }

            return(inst);
        }
        private void ImportVideos()
        {
            string publicKey = _settings.GetSingleValue("VideoPublicKey");
            string secretKey = _settings.GetSingleValue("VideoSecretKey");
            int feedNumber = -1;

            if (!int.TryParse(_settings.GetSingleValue("VideoFeedNumber"), out feedNumber))
            {
                Log("Invalid video feed number. Stopping.", LogLevel.Error);
                return;
            }

            if (!ValidateVideoPublicKey(publicKey))
            {
                Log("Invalid video public key. Stopping.", LogLevel.Error);
                return;
            }

            if (!ValidateGuid(secretKey))
            {
                Log("Invalid video secret key. Stopping.", LogLevel.Error);
                return;
            }

            Log("Starting video import.", LogLevel.Debug);

            string baseUrl = "http://livevideo.api.brafton.com/v2/";
            string basePhotoUrl = "http://pictures.brafton.com/v2/";
            AdferoVideoClient videoClient = new AdferoVideoClient(baseUrl, publicKey, secretKey);
            AdferoClient client = new AdferoClient(baseUrl, publicKey, secretKey);
            AdferoPhotoClient photoClient = new AdferoPhotoClient(basePhotoUrl);

            AdferoVideoDotNet.AdferoArticles.ArticlePhotos.AdferoArticlePhotosClient photos = client.ArticlePhotos();
            string scaleAxis = AdferoVideoDotNet.AdferoPhotos.Photos.AdferoScaleAxis.X;

            AdferoVideoDotNet.AdferoArticles.Feeds.AdferoFeedsClient feeds = client.Feeds();
            AdferoVideoDotNet.AdferoArticles.Feeds.AdferoFeedList feedList = feeds.ListFeeds(0, 10);

            AdferoVideoDotNet.AdferoArticles.Articles.AdferoArticlesClient articles = client.Articles();
            AdferoVideoDotNet.AdferoArticles.Articles.AdferoArticleList articleList = articles.ListForFeed(feedList.Items[feedNumber].Id, "live", 0, 100);

            int articleCount = articleList.Items.Count;
            AdferoVideoDotNet.AdferoArticles.Categories.AdferoCategoriesClient categories = client.Categories();

            foreach (AdferoVideoDotNet.AdferoArticles.Articles.AdferoArticleListItem item in articleList.Items)
            {
                int brafId = item.Id;
                AdferoVideoDotNet.AdferoArticles.Articles.AdferoArticle article = articles.Get(brafId);

                Post p = FindPost(article);

                if (p == null)
                {
                    Log(string.Format("Importing new post '{0}' (ID {1}).", article.Fields["title"].Trim(), article.Id), LogLevel.Info);
                    p = ConvertToPost(article, categories, videoClient);

                    ImportCategories(p);

                    PhotoInstance? thumbnail = GetPhotoInstance(article, photos, photoClient, scaleAxis, 180);
                    PhotoInstance? fullSizePhoto = GetPhotoInstance(article, photos, photoClient, scaleAxis, 500);
                    ImportPhotos(p, thumbnail, fullSizePhoto);

                    if (!p.Valid)
                    {
                        Log(string.Format("Error: post '{0}' invalid: '{1}'", p.ValidationMessage), LogLevel.Error);
                        continue;
                    }
                    else
                        p.Save();
                }
            }
        }