Esempio n. 1
0
        public static List<User> UserList( XDocument xdoc, List<User> users)
        {
            foreach (var status in xdoc.Root.Elements())
            {
                String tweetId = status.Element("id").Value;
                String text = status.Element("text").Value;
                String createdAt = status.Element("created_at").Value;
                Tweet tweet = new Tweet(tweetId, text, createdAt);

                XElement xuser = status.Element("user");
                String userId = xuser.Element("id").Value;
                String lastStatusUpdate = status.Element("created_at").Value; // Thu Dec 10 04:49:37 +0000 2009
                User user = new User( userId, null, null, null, lastStatusUpdate);
                if( users.Contains( user))
                {
                    User u = users[users.IndexOf(user)];
                    u.lastStatusUpdate = user.lastStatusUpdate;
                    u.LatestTweetDispFlag = false;

                    if (u.LatestTweet != null)
                    {
                        u.Tweets.Add(u.LatestTweet);
                    }
                    u.LatestTweet = tweet;
                }
                else
                {
                    user.name = xuser.Element("name").Value;
                    user.screenName = xuser.Element("screen_name").Value;
                    user.profileImageUrl = xuser.Element("profile_image_url").Value;

                    String[] _temp = user.ProfileImageUrl.Split(new char[] { '/' });
                    String ext = (_temp[_temp.Length - 1].Split(new char[] { '.' }))[1];
                    String imgCachePath = Game1.IMG_CACHE_DIR + userId + "." + ext;

                    if (File.Exists(imgCachePath) && ( DateTime.Now - File.GetCreationTime(imgCachePath)).TotalDays < 1)
                    {
                        user.ProfileImageCachePath = imgCachePath;
                    }
                    else
                    {
                        try
                        {
                            if (File.Exists(imgCachePath)) { File.Delete(imgCachePath); }
                            System.Net.WebClient wc = new System.Net.WebClient();
                            String tempImgCachePath = imgCachePath + "_tmp";
                            user.ProfileImageCachePath = imgCachePath;
                            wc.DownloadFile(user.ProfileImageUrl, tempImgCachePath);
                            wc.Dispose();

                            Image src = Image.FromFile(tempImgCachePath);
                            Image dest = ImageUtils.Resize.resizeImage((Bitmap)src, 48, 48);

                            dest.Save(imgCachePath);
                            File.Delete(tempImgCachePath);
                        }
                        catch( Exception ee)
                        {
                            Console.WriteLine( "[{0}] {1}", user.ToString(), ee.StackTrace);
                        }
                    }

                    user.LatestTweet = tweet;

                    users.Add(user);
                }
            }

            return users;
        }