SyncTask.cs :  » Network-Clients » iReaper » IReaper » IndexBuilder » WWESource » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » iReaper 
iReaper » IReaper » IndexBuilder » WWESource » SyncTask.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iReaper.IndexBuilder.Properties;
using System.Net;
using System.Xml;

namespace iReaper.IndexBuilder.WWESource{
    public class SyncTask : ITask
    {
        const string SEARCH_URL = "https://msevents.microsoft.com/cui/Handlers/DataCollector.ashx";
        const string SEARCH_PARM_BASE = "culture={0}&eventType=3&searchcontrol=no&s=1&pageNumber={1}";
        Dictionary<string, EventItem> eventTbl = new Dictionary<string, EventItem>();
        #region ITask Members

        public void Initialize()
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(EventItem[]));
            // Prepare event table.
            String folderPath = System.Configuration.ConfigurationSettings.AppSettings["TempFolder"];
            var dir = new System.IO.DirectoryInfo(folderPath);
            using (System.IO.FileStream fs = System.IO.File.Open(System.IO.Path.Combine(dir.FullName, "searchResult.xml"), System.IO.FileMode.OpenOrCreate))
            {
                try
                {
                    var items = (EventItem[])serializer.Deserialize(fs);
                    foreach (var item in items)
                    {
                        eventTbl[item.EventID] = item;
                    }
                }
                catch { }
                finally
                {
                    fs.Close();
                }
            }
        }

        public void Invoke()
        {
            List<EventItem> lists = new List<EventItem>();
            EventItem[] buffer = null;
            int pageCount = 0;
            string[] cultures = new string[] { "en-us","zh-cn" };
            foreach (var culture in cultures)
            {
                // First time invoke, get data and parameters
                QueryEvents(out buffer, out pageCount, culture, 1);
                lists.AddRange(buffer);
                System.Threading.ThreadPool.SetMaxThreads(10, 20);
                int count = pageCount - 1;
                System.Threading.ManualResetEvent waitHandle = new System.Threading.ManualResetEvent(false);
                for (int i = 2; i <= pageCount; i++)
                {
                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object index)
                        {
                            EventItem[] localBuffer = null;
                            int localPageCount = 0;
                            QueryEvents(out localBuffer, out localPageCount,culture, (int)index);
                            lock (lists)
                            {
                                lists.AddRange(localBuffer);
                            }
                            int left = System.Threading.Interlocked.Decrement(ref count);
                            Console.Write("\rLeft page{0}", left);
                            if (left == 0)
                            {
                                waitHandle.Set();
                            }
                        }), i);
                }
                waitHandle.WaitOne();
            }
            // Write to xml
            String folderPath = System.Configuration.ConfigurationSettings.AppSettings["TempFolder"];
            var dir = new System.IO.DirectoryInfo(folderPath);

            if (!dir.Exists)
            {
                dir.Create();
            }

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(EventItem[]));
            using(System.IO.FileStream fs = System.IO.File.Create(System.IO.Path.Combine(dir.FullName, "searchResult.xml")))
            {
                serializer.Serialize(fs, lists.ToArray());
                fs.Close();
            }
        }

        private void QueryEvents(out EventItem[] events, out int pageCount,string culture, int pageNum)
        {
            string search_param = string.Format(SEARCH_PARM_BASE,culture, pageNum);
            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            var result = client.UploadValues(SEARCH_URL, "POST", System.Web.HttpUtility.ParseQueryString(search_param));
            System.IO.MemoryStream strm = new System.IO.MemoryStream(result);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strm);
            // Check page count
            var strPageCount = xmldoc.SelectSingleNode("/root").Attributes["pageCount"].Value;
            pageCount = int.Parse(strPageCount);
            events = ParseResultXml(xmldoc);
        }

        private EventItem[] ParseResultXml(XmlDocument xmldoc)
        {
            var nodes = xmldoc.DocumentElement.SelectNodes("//eventData");
            List<EventItem> items = new List<EventItem>();
            for (int i = 0; i < nodes.Count; i++)
            {
                var item = ParseOneNode(nodes[i]);
                if (!eventTbl.ContainsKey(item.EventID))
                {
                    eventTbl.Add(item.EventID, item);
                    items.Add(item);
                }
            }

            return items.ToArray();
        }
    //    <title>Microsoft Office System Webcast: Advanced Tips and Tricks for Getting Data and Other Content into and out of Your Documents (Level 300)</title>
    //<categoryId>3</categoryId>
    //<eventId>1032369392</eventId>
    //<description>
    //  Do you often find yourself retyping or copying data and other content throughout a document or between documents or programs? If so, stop typing and join this webcast for a look at tools across the 2007 Microsoft Office system that manage information for you and enable you to do more with your documents than you might imagine. In this session, take a look at new or improved options for repeating content throughout a document, reusing slides in other presentations, importing and analyzing data, changing content and formatting without even opening documents, creating diagrams from data, and sharing information with others. If you ever feel as though you spend too much time on tasks in Microsoft Office programs, or wonder if there are faster and easier ways to get your work done and create better content, this webcast is for you.Presenters: Stephanie Krieger, Microsoft Press Author and Office System MVP, Arouet.netStephanie Krieger is a Microsoft Office Most Valuable Professional (MVP) and author of two books, Advanced Microsoft Office Documents 2007 Edition Inside Out and Microsoft Office Document Designer (Microsoft Press, 2007 and 2004, respectively). She has helped many global companies develop enterprise solutions for the Microsoft Office System and taught numerous professionals how to build great documents by understanding how Microsoft Office programs "think." Stephanie writes for several Microsoft Web pages and frequently delivers Microsoft Office webcasts. Visit her blog, arouet.net, for a regularly updated collection of tips. If you have questions or feedback, contact us.
    //</description>
    //<startdate>2/11/2008 12:00 AM  Pacific Time (US &amp; Canada)</startdate>
    //<enddate>- 2/7/2012 11:59 PM</enddate>
    //<detailUrl>https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032369392&amp;culture=en-US</detailUrl>
    //<duration> | Duration:60 Minutes</duration>
    //<primaryLanguage>English</primaryLanguage>
    //<targetAudience>Additional Information Worker</targetAudience>
    //<starRatingImage>resources/images/5Star.jpg</starRatingImage>
    //<starRatingVisibility>true</starRatingVisibility>
    //<userbookmarked>false</userbookmarked>
    //<userregistered>false</userregistered>
    //<quickactionimg>register</quickactionimg>
    //<quickactionurl>https://msevents.microsoft.com/cui/Register.aspx?EventID=1032369392&amp;culture=en-US&amp;countryCode=US</quickactionurl>
    //<quickaction>1</quickaction>
        private EventItem ParseOneNode(XmlNode xmlNode)
        {
            EventItem item = new EventItem();
            item.Title = xmlNode.SelectSingleNode("./title").InnerText;
            item.EventID = xmlNode.SelectSingleNode("./eventId").InnerText;
            item.Description = xmlNode.SelectSingleNode("./description").InnerText.Trim();
            item.Language = xmlNode.SelectSingleNode("./primaryLanguage").InnerText;
            item.StartDataString = xmlNode.SelectSingleNode("./startdate").InnerText;
            DateTime dt;
            if (item.StartDataString.Contains(" Pacific Time (US &amp; Canada)"))
            {
                item.StartDataString = item.StartDataString.Replace(" Pacific Time (US &amp; Canada)", "");
            }
            else if (item.StartDataString.Contains(" "))
            {
                item.StartDataString = item.StartDataString.Replace(" ", "");
            }
            if (DateTime.TryParse(item.StartDataString, out dt))
            {
                item.StartData = dt;
            }
            item.DetailUrl = xmlNode.SelectSingleNode("./detailUrl").InnerText;
            item.QuickActionUrl = xmlNode.SelectSingleNode("./quickactionurl").InnerText;
            item.QuickActionId = int.Parse(xmlNode.SelectSingleNode("./quickaction").InnerText);

            return item;
        }

        public event EventHandler<ProgressEventArgs> OnProgress;

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.