예제 #1
0
        static void Main()
        {
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");

            ContentManager cm = new ContentManager(client);
            List<Source> sources = cm.GetSources();
            foreach (var source in sources)
            {
                List<Article> articles = cm.GetArticlesForSource(source);
                foreach (Article article in articles)
                {
                    string id = article.Id.ToString().Replace("tcm:4", "tcm:5");
                    if(!client.IsPublished(id, "tcm:0-2-65537", true))
                        cm.Publish(new []{id}, "tcm:0-2-65537");
                }
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");

            const string primaryAccountName = "Chris Summers";
            List<string> alternateNames = new List<string> { "[email protected] (chris)" };

            ContentManager cm = new ContentManager(client);
            List<Person> persons = cm.GetPersons();
            Person primary = null;

            foreach (Person p in persons)
            {
                if (p.Name == primaryAccountName)
                {
                    primary = p;
                    break;
                }
            }
            if (primary == null)
            {
                Console.WriteLine("Could not find primary account with name " + primaryAccountName);
                return;
            }

            List<Person> duplicates = new List<Person>();
            foreach (string name in alternateNames)
            {
                foreach (Person p in persons)
                {
                    if(p.Name == name)
                        duplicates.Add(p);
                }
            }

            // 1. Add alternate names to primary account
            // 2. Move author of alternate content to primary account
            // 3. Republish content
            // 4. Delete alternate person accounts

            List<string> currentNames = primary.AlternateNames;
            bool changedAuthorName = false;
            foreach (string name in alternateNames)
            {
                if (!currentNames.Contains(name))
                {
                    currentNames.Add(name);
                    changedAuthorName = true;
                }
            }

            if (changedAuthorName)
            {
                primary.AlternateNames = currentNames;
                primary.Save(true);
            }
            List<string> articles = new List<string>();
            foreach (Person p in duplicates)
            {
                foreach (Article a in cm.GetArticlesForPerson(p))
                {
                    // BUG in a.Authors!!
                    // It is not replacing the values, just adding.
                    a.Authors = new List<Person>{primary};
                    a.Save(true);
                    articles.Add(a.Id.GetVersionlessUri().ToString().Replace("tcm:4-", "tcm:5-"));
                }
            }

            cm.Publish(articles.ToArray(), Constants.TargetUri);

            foreach (Person p in duplicates)
            {
                try
                {
                    client.Delete(p.Id);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not delete component with id " + p.Id + ". Exception: " + ex);
                }
            }
        }
예제 #3
0
        static void Main()
        {
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");

            ContentManager cm = new ContentManager(client);

            List<Source> sources = cm.GetSources();
            int countSources = sources.Count;
            Console.WriteLine("Loaded " + countSources + " sources. Starting to process.");
            XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable());
            nm.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

            Dictionary<Source, List<Article>> addedContent = new Dictionary<Source, List<Article>>();

            foreach (var source in sources)
            {
                Console.WriteLine("Loading content for source " + source.Title);
                XmlDocument feedXml = null;

                WebRequest wrq = WebRequest.Create(source.RssFeedUrl);
                wrq.Proxy = WebRequest.DefaultWebProxy;
                wrq.Proxy.Credentials = CredentialCache.DefaultCredentials;

                XmlTextReader reader = null;
                SyndicationFeed feed = null;
                try
                {
                    reader = new XmlTextReader(wrq.GetResponse().GetResponseStream());
                    feed = SyndicationFeed.Load(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not read response from source" + ex.Message);
                }
                if (reader == null || feed == null) continue;

                int countItems = feed.Items.Count();
                Console.WriteLine("Loaded " + countItems + " items from source. Processing");
                int count = 0;
                List<Article> newArticles = new List<Article>();
                foreach (var item in feed.Items)
                {
                    count++;
                    Person author = null;
                    if (item.Authors.Count == 0)
                    {
                        //Console.WriteLine("Could not find an author in feed source, checking for default");
                        if (source.DefaultAuthor != null)
                        {
                            author = source.DefaultAuthor;
                            //Console.WriteLine("Using default author " + author.Name);
                        }
                        else
                        {
                            //Console.WriteLine("Could not find default author, being creative");
                            if (feedXml == null)
                            {
                                try
                                {
                                    feedXml = new XmlDocument();
                                    feedXml.Load(source.RssFeedUrl);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Something went wrong loading " + source.RssFeedUrl);
                                    Console.WriteLine(ex.ToString());
                                }

                            }
                            if (feedXml != null)
                            {
                                string xpath = "/rss/channel/item[" + count + "]/dc:creator";
                                if (feedXml.SelectSingleNode(xpath, nm) != null)
                                {
                                    author =
                                        cm.FindPersonByNameOrAlternate(feedXml.SelectSingleNode(xpath, nm).InnerText);
                                    if (author == null)
                                    {
                                        author = new Person(client) { Name = feedXml.SelectSingleNode(xpath, nm).InnerText };
                                        author.Save();
                                        author =
                                            cm.FindPersonByNameOrAlternate(
                                                feedXml.SelectSingleNode(xpath, nm).InnerText, true);
                                    }
                                }
                            }
                        }

                    }
                    else
                    {
                        string nameOrAlternate;
                        SyndicationPerson syndicationPerson = item.Authors.First();
                        if (string.IsNullOrEmpty(syndicationPerson.Name))
                            nameOrAlternate = syndicationPerson.Email;
                        else
                            nameOrAlternate = syndicationPerson.Name;

                        author = cm.FindPersonByNameOrAlternate(nameOrAlternate);
                    }
                    if (author == null)
                    {
                        string name = string.Empty;
                        if (item.Authors.Count > 0)
                        {
                            if (item.Authors[0].Name != null)
                                name = item.Authors[0].Name;
                            else
                                name = item.Authors[0].Email;
                        }
                        author = new Person(client) { Name = name };
                        if (source.IsStackOverflow)
                        {
                            author.StackOverflowId = item.Authors[0].Uri;
                        }
                        author.Save();
                        author = cm.FindPersonByNameOrAlternate(name, true);
                    }

                    List<Person> authors = new List<Person> { author };
                    //Console.WriteLine("Using author: " + author.Name);
                    string stackOverflowId = null;
                    if (source.IsStackOverflow)
                    {
                        if (string.IsNullOrEmpty(author.StackOverflowId))
                        {
                            author.StackOverflowId = item.Authors[0].Uri;
                            author.Save(true);
                        }
                    }

                    if (source.IsTridionStackExchange)
                    {
                        stackOverflowId = item.Id;
                    }

                    if (item.PublishDate.DateTime > DateTime.MinValue)
                    {
                        // Organize content by Date
                        // Year
                        // Month
                        // Day
                        string store = cm.GetFolderForDate(item.PublishDate.DateTime);
                        string articleTitle;
                        if (string.IsNullOrEmpty(item.Title.Text))
                        {
                            articleTitle = "No title specified";
                        }
                        else
                        {
                            articleTitle = item.Title.Text;
                        }
                        articleTitle = articleTitle.Trim();

                        OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData();
                        bool alreadyExists = false;
                        XElement items = client.GetListXml(store, filter);
                        foreach (XElement node in items.Nodes())
                        {
                            if (source.IsTridionStackExchange)
                            {
                                Article a = new Article(new TcmUri(node.Attribute("ID").Value), client);
                                if (a.StackOverFlowQuestionId == stackOverflowId)
                                {
                                    alreadyExists = true;
                                    if (a.Title != node.Attribute("Title").Value)
                                    {
                                        a.Title = node.Attribute("Title").Value;
                                        a.Save();
                                        Console.WriteLine("Modified title of article with TrEx ID: " + stackOverflowId);
                                    }
                                    continue;
                                }

                            }

                            if (!node.Attribute("Title").Value.Equals(articleTitle)) continue;
                            alreadyExists = true;
                            break;
                        }

                        // Loop differently if this is Tridion on StackExchange (titles tend to change as people fix the content)

                        if (!alreadyExists)
                        {

                            //Console.WriteLine(articleTitle + " is a new article. Saving");
                            Console.Write(".");
                            Article article = new Article(client, new TcmUri(store));

                            string content = "";
                            string summary = "";
                            try
                            {
                                if (item.Content != null)
                                {
                                    content = ((TextSyndicationContent)item.Content).Text;
                                }
                                if (item.Summary != null)
                                {
                                    summary = item.Summary.Text;
                                }
                                if (!string.IsNullOrEmpty(content))
                                {
                                    try
                                    {
                                        content = Utilities.ConvertHtmlToXhtml(content);
                                    }
                                    catch (Exception)
                                    {
                                        content = null;
                                    }
                                }
                                if (!string.IsNullOrEmpty(summary))
                                {
                                    try
                                    {
                                        summary = Utilities.ConvertHtmlToXhtml(summary);
                                    }
                                    catch (Exception)
                                    {
                                        summary = null;
                                    }

                                }

                                if (string.IsNullOrEmpty(summary))
                                {
                                    summary = !string.IsNullOrEmpty(content) ? content : "Could not find summary";
                                }
                                if (string.IsNullOrEmpty(content))
                                {
                                    content = !string.IsNullOrEmpty(summary) ? summary : "Could not find content";
                                }

                            }
                            catch (Exception ex)
                            {
                                content = "Could not convert source description to XHtml. " + ex.Message;
                                content += ((TextSyndicationContent)item.Content).Text;
                            }
                            article.Authors = authors;
                            article.Body = content;
                            article.Date = item.PublishDate.DateTime;
                            article.DisplayTitle = item.Title.Text;
                            article.Title = articleTitle;
                            article.Summary = summary;
                            article.Url = item.Links.First().Uri.AbsoluteUri;
                            //if (stackOverflowId != null) article.StackOverFlowQuestionId = stackOverflowId;
                            List<string> categories = new List<string>();
                            foreach (var category in item.Categories)
                            {
                                categories.Add(category.Name);
                            }
                            article.Categories = categories;
                            article.Source = source;
                            article.Save();

                            Console.Write("#");
                            newArticles.Add(article);
                        }
                        else
                        {
                            Console.Write(".");
                        }
                    }

                }
                if (newArticles.Count > 0)
                {
                    addedContent.Add(source, newArticles);
                }
            }
            List<string> idsToPublish = new List<string>();
            if (addedContent.Count > 0)
            {
                Console.WriteLine("============================================================");
                Console.WriteLine("Added content");
                foreach (Source source in addedContent.Keys)
                {
                    string sg = cm.GetStructureGroup(source.Title, cm.ResolveUrl(Constants.RootStructureGroup));
                    Console.WriteLine("Source: " + source.Content.Title + "(" + addedContent[source].Count + ")");
                    foreach (Article article in addedContent[source])
                    {
                        string yearSg = cm.GetStructureGroup(article.Date.Year.ToString(CultureInfo.InvariantCulture), sg);
                        string pageId = cm.AddToPage(yearSg, article);
                        if (!idsToPublish.Contains(pageId)) idsToPublish.Add(pageId);
                        Console.WriteLine(article.Title + ", " + article.Authors[0].Name);
                    }
                    Console.WriteLine("-------");
                }
                Console.WriteLine("============================================================");
            }

            //Publishing
            cm.Publish(idsToPublish.ToArray(), "tcm:0-2-65537");

            Console.WriteLine("Finished, press any key to exit");
            Console.Read();
        }