Advertisement
Guest User

Untitled

a guest
Oct 27th, 2010
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Xml.Linq;
  7. using System.Xml.XPath;
  8.  
  9. namespace SharpDIC.Entities
  10. {
  11.     /// <summary>
  12.     /// Represents a user on the Dream.In.Code website.
  13.     /// </summary>
  14.     public class User
  15.     {
  16.         /********************************************************************************
  17.          * Some of these attributes aren't even used. The API doesn't provide them yet, *
  18.          * so I'll have to scrape the information from the HTML itself. Still thinking  *
  19.          * about how to tackle this.                                                    *
  20.          *                                                                              *
  21.          * Author: Sergio Tapia                                                         *
  22.          * Website: http://www.alphaot.com                                              *
  23.          * ******************************************************************************/
  24.  
  25.         #region "Attributes"        
  26.         public string ID { get; set; }
  27.         public string Name { get; set; }
  28.         public string Rating { get; set; }
  29.         public string Photo { get; set; }
  30.         public string LastActive { get; set; }
  31.         public string Location { get; set; }
  32.         public string Birthday { get; set; }
  33.         public string Age { get; set; }
  34.         public string Gender { get; set; }
  35.         public string Email { get; set; }
  36.  
  37.  
  38.         public string Title { get; set; }
  39.         public string Reputation { get; set; }
  40.         public string DreamKudos { get; set; }
  41.         public string Group { get; set; }
  42.         public string Posts { get; set; }
  43.         public string PostsPerDay { get; set; }
  44.         public string MostActiveIn { get; set; }
  45.         public string JoinDate { get; set; }
  46.         public string ProfileViews { get; set; }
  47.  
  48.         public string FavoriteOS { get; set; }
  49.         public string FavoriteBrowser { get; set; }
  50.         public string FavoriteProcessor { get; set; }
  51.         public string FavoriteConsole { get; set; }
  52.  
  53.         public List<Visitor> Visitors { get; set; }
  54.         public List<Friend> Friends { get; set; }
  55.         public List<Comment> Comments { get; set; }
  56.         public string ProgrammingLanguages { get; set; }
  57.  
  58.         public string AIM { get; set; }
  59.         public string MSN { get; set; }
  60.         public string Website { get; set; }
  61.         public string ICQ { get; set; }
  62.         public string Yahoo { get; set; }
  63.         public string Jabber { get; set; }
  64.         public string Skype { get; set; }
  65.         public string LinkedIn { get; set; }
  66.         public string Facebook { get; set; }
  67.         public string Twitter { get; set; }
  68.         public string XFire { get; set; }
  69.         #endregion
  70.  
  71.         /// <summary>
  72.         /// Load a user by providing an ID.
  73.         /// </summary>
  74.         /// <param name="ID">A user's individual ID number.</param>
  75.         public User(string ID)
  76.         {
  77.             XDocument xmlResponse = GetUserXMLResponse(ID);            
  78.                        
  79.             LoadGeneralInformation(xmlResponse.Element("ipb").Element("profile"));
  80.             LoadContactInformation(xmlResponse.Element("ipb").Element("profile").Element("contactinformation"));
  81.             LoadLatestVisitors(xmlResponse.Element("ipb").Element("profile").Element("latestvisitors"));
  82.             LoadFriends(xmlResponse.Element("ipb").Element("profile").Element("friends"));
  83.             LoadComments(xmlResponse.Element("ipb").Element("profile").Element("comments"));            
  84.         }
  85.  
  86.         #region "Loading Methods - give them XML and they'll do the job."        
  87.         private void LoadGeneralInformation(XElement profileXML)
  88.         {
  89.             this.ID = (string)profileXML.Element("id");
  90.             this.Name = (string)profileXML.Element("name");
  91.             this.Rating = (string)profileXML.Element("rating");
  92.             this.Photo = (string)profileXML.Element("photo");
  93.             this.Reputation = (string)profileXML.Element("reputation");
  94.             this.Group = (string)profileXML.Element("group").Element("span");
  95.             this.Posts = (string)profileXML.Element("posts");
  96.             this.PostsPerDay = (string)profileXML.Element("postsperday");
  97.             this.JoinDate = (string)profileXML.Element("joined");
  98.             this.ProfileViews = (string)profileXML.Element("views");
  99.             this.LastActive = (string)profileXML.Element("lastactive");
  100.             this.Location = (string)profileXML.Element("location");
  101.             this.Title = (string)profileXML.Element("title");
  102.             this.Age = (string)profileXML.Element("age");
  103.             this.Birthday = (string)profileXML.Element("birthday");
  104.             this.Gender = (string)profileXML.Element("gender").Element("gender").Element("value");
  105.         }
  106.  
  107.         private void LoadContactInformation(XElement contactXML)
  108.         {
  109.             this.AIM = (string)contactXML.XPathSelectElement("contact[title='AIM']/value");
  110.             this.MSN = (string)contactXML.XPathSelectElement("contact[title='MSN']/value");
  111.             this.Website = (string)contactXML.XPathSelectElement("contact[title='Website URL']/value");
  112.             this.ICQ = (string)contactXML.XPathSelectElement("contact[title='ICQ']/value");
  113.             this.Yahoo = (string)contactXML.XPathSelectElement("contact[title='Yahoo']/value");
  114.             this.Jabber = (string)contactXML.XPathSelectElement("contact[title='Jabber']/value");
  115.             this.Skype = (string)contactXML.XPathSelectElement("contact[title='Skype']/value");
  116.             this.LinkedIn = (string)contactXML.XPathSelectElement("contact[title='LinkedIn']/value");
  117.             this.Facebook = (string)contactXML.XPathSelectElement("contact[title='Facebook']/value");
  118.             this.Twitter = (string)contactXML.XPathSelectElement("contact[title='Twitter']/value");
  119.             this.XFire = (string)contactXML.XPathSelectElement("contact[title='Xfire']/value");
  120.         }
  121.  
  122.         private void LoadLatestVisitors(XElement visitorsXML)
  123.         {
  124.             this.Visitors = (from visitor in visitorsXML.Descendants("user")
  125.                              select new Visitor()
  126.                              {
  127.                                  ID = visitor.Element("id").Value,
  128.                                  Name = visitor.Element("name").Value,
  129.                                  Url = visitor.Element("url").Value,
  130.                                  Photo = visitor.Element("photo").Value,
  131.                                  Visited = visitor.Element("visited").Value,
  132.                              }).ToList();
  133.         }
  134.  
  135.         private void LoadFriends(XElement friendsXML)
  136.         {
  137.             this.Friends = (from friend in friendsXML.Descendants("user")
  138.                             select new Friend()
  139.                             {
  140.                                 ID = friend.Element("id").Value,
  141.                                 Name = friend.Element("name").Value,
  142.                                 Url = friend.Element("url").Value,
  143.                                 Photo = friend.Element("photo").Value
  144.                             }).ToList();
  145.         }
  146.  
  147.         private void LoadComments(XElement commentsXML)
  148.         {
  149.             this.Comments = (from comment in commentsXML.Descendants("comment")
  150.                              select new Comment()
  151.                              {
  152.                                  ID = comment.Element("id").Value,
  153.                                  Text = comment.Element("text").Value,
  154.                                  Date = comment.Element("date").Value,
  155.                                  UserWhoPosted = new Friend()
  156.                                  {
  157.                                      ID = comment.Element("user").Element("id").Value,
  158.                                      Name = comment.Element("user").Element("name").Value,
  159.                                      Url = comment.Element("user").Element("url").Value,
  160.                                      Photo = comment.Element("user").Element("photo").Value
  161.                                  }
  162.                              }).ToList();
  163.         }
  164.         #endregion        
  165.  
  166.         private XDocument GetUserXMLResponse(string ID)
  167.         {
  168.             WebClient webClient = new WebClient();
  169.             string htmlSource = webClient.DownloadString(new Uri(String.Format("http://www.dreamincode.net/forums/xml.php?showuser={0}", ID)));
  170.             return XDocument.Parse(htmlSource);
  171.         }        
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement