public string GetBlogImagesAlbum(string albumName, string blogId)
        {
            const string FEED_REL = "http://schemas.google.com/g/2005#feed";

            // TODO: HACK: The deprecation-extension flag keeps the deprecated Picasa API alive.
            Uri picasaUri = new Uri("https://picasaweb.google.com/data/feed/api/user/default?deprecation-extension=true");
            var picasaId  = string.Empty;

            try
            {
                Uri         reqUri       = picasaUri;
                XmlDocument albumListDoc = AtomClient.xmlRestRequestHelper.Get(ref reqUri, CreateAuthorizationFilter(), "kind", "album");
                var         idNode       = albumListDoc.SelectSingleNode(@"/atom:feed/gphoto:user", _nsMgr) as XmlElement;
                if (idNode != null)
                {
                    var id = AtomProtocolVersion.V10DraftBlogger.TextNodeToPlaintext(idNode);
                    picasaId = id;
                }

                foreach (XmlElement entryEl in albumListDoc.SelectNodes(@"/atom:feed/atom:entry", _nsMgr))
                {
                    XmlElement titleNode = entryEl.SelectSingleNode(@"atom:title", _nsMgr) as XmlElement;
                    if (titleNode != null)
                    {
                        string titleText = AtomProtocolVersion.V10DraftBlogger.TextNodeToPlaintext(titleNode);
                        if (titleText == albumName)
                        {
                            XmlNode numPhotosRemainingNode = entryEl.SelectSingleNode("gphoto:numphotosremaining/text()", _nsMgr);
                            if (numPhotosRemainingNode != null)
                            {
                                int numPhotosRemaining;
                                if (int.TryParse(numPhotosRemainingNode.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out numPhotosRemaining))
                                {
                                    if (numPhotosRemaining < 1)
                                    {
                                        continue;
                                    }
                                }
                            }
                            string selfHref = AtomEntry.GetLink(entryEl, _nsMgr, FEED_REL, "application/atom+xml", null, reqUri);
                            if (selfHref.Length > 1)
                            {
                                // TODO: HACK: This keeps the deprecated Picasa API alive.
                                selfHref += "&deprecation-extension=true";
                                return(selfHref);
                            }
                        }
                    }
                }
            }
            catch (WebException we)
            {
                HttpWebResponse httpWebResponse = we.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    HttpRequestHelper.DumpResponse(httpWebResponse);
                    if (httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new BlogClientOperationCancelledException();
                    }
                }
                throw;
            }

            try
            {
                XmlDocument newDoc     = new XmlDocument();
                XmlElement  newEntryEl = newDoc.CreateElement("atom", "entry", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);
                newDoc.AppendChild(newEntryEl);

                XmlElement newTitleEl = newDoc.CreateElement("atom", "title", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);
                newTitleEl.SetAttribute("type", "text");
                newTitleEl.InnerText = albumName;
                newEntryEl.AppendChild(newTitleEl);

                XmlElement newSummaryEl = newDoc.CreateElement("atom", "summary", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);
                newSummaryEl.SetAttribute("type", "text");
                newSummaryEl.InnerText = Res.Get(StringId.BloggerImageAlbumDescription);
                newEntryEl.AppendChild(newSummaryEl);

                XmlElement newAccessEl = newDoc.CreateElement("gphoto", "access", photoNS.Uri);
                newAccessEl.InnerText = "private";
                newEntryEl.AppendChild(newAccessEl);

                XmlElement newCategoryEl = newDoc.CreateElement("atom", "category", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);
                newCategoryEl.SetAttribute("scheme", "http://schemas.google.com/g/2005#kind");
                newCategoryEl.SetAttribute("term", "http://schemas.google.com/photos/2007#album");
                newEntryEl.AppendChild(newCategoryEl);

                Uri         postUri               = picasaUri;
                XmlDocument newAlbumResult        = AtomClient.xmlRestRequestHelper.Post(ref postUri, CreateAuthorizationFilter(), "application/atom+xml", newDoc, null);
                XmlElement  newAlbumResultEntryEl = newAlbumResult.SelectSingleNode("/atom:entry", _nsMgr) as XmlElement;
                Debug.Assert(newAlbumResultEntryEl != null);
                return(AtomEntry.GetLink(newAlbumResultEntryEl, _nsMgr, FEED_REL, "application/atom+xml", null, postUri));
            }
            catch (Exception)
            {
                // Ignore
            }

            // If we've got this far, it means creating the Open Live Writer album has failed.
            // We will now try and use the Blogger assigned folder.
            if (!string.IsNullOrEmpty(picasaId))
            {
                var service = GetService();

                var userInfo = service.BlogUserInfos.Get("self", blogId).Execute();

                // If the PhotosAlbumKey is "0", this means the user has never posted to Blogger from the
                // Blogger web interface, which means the album has never been created and so there's nothing
                // for us to use.
                if (userInfo.BlogUserInfoValue.PhotosAlbumKey != "0")
                {
                    // TODO: HACK: The deprecation-extension flag keeps the deprecated Picasa API alive.
                    var bloggerPicasaUrl = $"https://picasaweb.google.com/data/feed/api/user/{picasaId}/albumid/{userInfo.BlogUserInfoValue.PhotosAlbumKey}?deprecation-extension=true";
                    return(bloggerPicasaUrl);
                }
            }

            // If we've got this far, it means the user is going to have to manually create their own album.
            throw new BlogClientFileTransferException("Unable to upload to Blogger", "BloggerError", "We were unable to create a folder for your images, please go to http://openlivewriter.org/tutorials/googlePhotoFix.html to see how to do this");
        }
예제 #2
0
        public string GetBlogImagesAlbum(string albumName)
        {
            const string FEED_REL      = "http://schemas.google.com/g/2005#feed";
            const string GPHOTO_NS_URI = "http://schemas.google.com/photos/2007";

            Uri picasaUri = new Uri("https://picasaweb.google.com/data/feed/api/user/default");

            try
            {
                Uri         reqUri       = picasaUri;
                XmlDocument albumListDoc = AtomClient.xmlRestRequestHelper.Get(ref reqUri, CreateAuthorizationFilter(), "kind", "album");
                foreach (XmlElement entryEl in albumListDoc.SelectNodes(@"/atom:feed/atom:entry", _nsMgr))
                {
                    XmlElement titleNode = entryEl.SelectSingleNode(@"atom:title", _nsMgr) as XmlElement;
                    if (titleNode != null)
                    {
                        string titleText = AtomProtocolVersion.V10DraftBlogger.TextNodeToPlaintext(titleNode);
                        if (titleText == albumName)
                        {
                            XmlNamespaceManager nsMgr2 = new XmlNamespaceManager(new NameTable());
                            nsMgr2.AddNamespace("gphoto", "http://schemas.google.com/photos/2007");
                            XmlNode numPhotosRemainingNode = entryEl.SelectSingleNode("gphoto:numphotosremaining/text()", nsMgr2);
                            if (numPhotosRemainingNode != null)
                            {
                                int numPhotosRemaining;
                                if (int.TryParse(numPhotosRemainingNode.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out numPhotosRemaining))
                                {
                                    if (numPhotosRemaining < 1)
                                    {
                                        continue;
                                    }
                                }
                            }
                            string selfHref = AtomEntry.GetLink(entryEl, _nsMgr, FEED_REL, "application/atom+xml", null, reqUri);
                            if (selfHref.Length > 1)
                            {
                                return(selfHref);
                            }
                        }
                    }
                }
            }
            catch (WebException we)
            {
                HttpWebResponse httpWebResponse = we.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    HttpRequestHelper.DumpResponse(httpWebResponse);
                    if (httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new BlogClientOperationCancelledException();
                    }
                }
                throw;
            }

            XmlDocument newDoc     = new XmlDocument();
            XmlElement  newEntryEl = newDoc.CreateElement("atom", "entry", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);

            newDoc.AppendChild(newEntryEl);

            XmlElement newTitleEl = newDoc.CreateElement("atom", "title", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);

            newTitleEl.SetAttribute("type", "text");
            newTitleEl.InnerText = albumName;
            newEntryEl.AppendChild(newTitleEl);

            XmlElement newSummaryEl = newDoc.CreateElement("atom", "summary", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);

            newSummaryEl.SetAttribute("type", "text");
            newSummaryEl.InnerText = Res.Get(StringId.BloggerImageAlbumDescription);
            newEntryEl.AppendChild(newSummaryEl);

            XmlElement newAccessEl = newDoc.CreateElement("gphoto", "access", GPHOTO_NS_URI);

            newAccessEl.InnerText = "private";
            newEntryEl.AppendChild(newAccessEl);

            XmlElement newCategoryEl = newDoc.CreateElement("atom", "category", AtomProtocolVersion.V10DraftBlogger.NamespaceUri);

            newCategoryEl.SetAttribute("scheme", "http://schemas.google.com/g/2005#kind");
            newCategoryEl.SetAttribute("term", "http://schemas.google.com/photos/2007#album");
            newEntryEl.AppendChild(newCategoryEl);

            Uri         postUri               = picasaUri;
            XmlDocument newAlbumResult        = AtomClient.xmlRestRequestHelper.Post(ref postUri, CreateAuthorizationFilter(), "application/atom+xml", newDoc, null);
            XmlElement  newAlbumResultEntryEl = newAlbumResult.SelectSingleNode("/atom:entry", _nsMgr) as XmlElement;

            Debug.Assert(newAlbumResultEntryEl != null);
            return(AtomEntry.GetLink(newAlbumResultEntryEl, _nsMgr, FEED_REL, "application/atom+xml", null, postUri));
        }
        public async Task <string> GetBlogImagesAlbum(string albumName)
        {
            const string FEED_REL      = "http://schemas.google.com/g/2005#feed";
            const string GPHOTO_NS_URI = "http://schemas.google.com/photos/2007";

            Uri picasaUri = new Uri("https://picasaweb.google.com/data/feed/api/user/default");

            try
            {
                Uri reqUri       = picasaUri;
                var albumListDoc = await AtomClient.xmlRestRequestHelper.Get(await CreateAuthorizationFilter(), new XmlRestRequestHelper.XmlRequestResult()
                {
                    uri = reqUri
                }, "kind", "album");

                foreach (var entryEl in albumListDoc.SelectNodesNS(@"/atom:feed/atom:entry", _nsMgr.ToNSMethodFormat()))
                {
                    var titleNode = entryEl.SelectSingleNodeNS(@"atom:title", _nsMgr.ToNSMethodFormat());
                    if (titleNode != null)
                    {
                        string titleText = AtomProtocolVersion.V10DraftBlogger.TextNodeToPlaintext(titleNode);
                        if (titleText == albumName)
                        {
                            XmlNamespaceManager nsMgr2 = new XmlNamespaceManager(new NameTable());
                            nsMgr2.AddNamespace("gphoto", "http://schemas.google.com/photos/2007");
                            var numPhotosRemainingNode = entryEl.SelectSingleNodeNS("gphoto:numphotosremaining/text()", nsMgr2.ToNSMethodFormat());
                            if (numPhotosRemainingNode != null)
                            {
                                int numPhotosRemaining;
                                if (int.TryParse((string)numPhotosRemainingNode.NodeValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out numPhotosRemaining))
                                {
                                    if (numPhotosRemaining < 1)
                                    {
                                        continue;
                                    }
                                }
                            }
                            string selfHref = AtomEntry.GetLink(entryEl, _nsMgr, FEED_REL, "application/atom+xml", null, reqUri);
                            if (selfHref.Length > 1)
                            {
                                return(selfHref);
                            }
                        }
                    }
                }
            }
            catch (WebException we)
            {
                HttpWebResponse httpWebResponse = we.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    HttpRequestHelper.DumpResponse(httpWebResponse);
                    if (httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new BlogClientOperationCancelledException();
                    }
                }
                throw;
            }

            throw new Exception("Could not find a Google Photos album called 'NetWriter'. Create an Album in Google Photos called 'NetWriter' and try again.");

            //var newDoc = new Windows.Data.Xml.Dom.XmlDocument();
            //var newEntryEl = newDoc.CreateElementNS(AtomProtocolVersion.V10DraftBlogger.NamespaceUri, "atom:entry");
            //newDoc.AppendChild(newEntryEl);

            //var newTitleEl = newDoc.CreateElementNS(AtomProtocolVersion.V10DraftBlogger.NamespaceUri, "atom:title");
            //newTitleEl.SetAttribute("type", "text");
            //newTitleEl.InnerText = albumName;
            //newEntryEl.AppendChild(newTitleEl);

            //var newSummaryEl = newDoc.CreateElementNS(AtomProtocolVersion.V10DraftBlogger.NamespaceUri, "atom:summary");
            //newSummaryEl.SetAttribute("type", "text");
            //newSummaryEl.InnerText = "This album is used to store pictures from blog posts published by Net Writer.";
            //newEntryEl.AppendChild(newSummaryEl);

            //var newAccessEl = newDoc.CreateElementNS(GPHOTO_NS_URI, "gphoto:access");
            //newAccessEl.InnerText = "private";
            //newEntryEl.AppendChild(newAccessEl);

            //var newCategoryEl = newDoc.CreateElementNS(AtomProtocolVersion.V10DraftBlogger.NamespaceUri, "atom:category");
            //newCategoryEl.SetAttribute("scheme", "http://schemas.google.com/g/2005#kind");
            //newCategoryEl.SetAttribute("term", "http://schemas.google.com/photos/2007#album");
            //newEntryEl.AppendChild(newCategoryEl);


            //XmlRestRequestHelper.XmlRequestResult result = new XmlRestRequestHelper.XmlRequestResult() {uri = picasaUri};
            //var newAlbumResult = await AtomClient.xmlRestRequestHelper.Post(await CreateAuthorizationFilter(), "application/atom+xml", newDoc, null, result);
            //var newAlbumResultEntryEl = newAlbumResult.SelectSingleNodeNS("/atom:entry", _nsMgr.ToNSMethodFormat());
            //Debug.Assert(newAlbumResultEntryEl != null);
            //return AtomEntry.GetLink(newAlbumResultEntryEl, _nsMgr, FEED_REL, "application/atom+xml", null, result.uri);
        }