private static void convertEntries(Tweets model, List<Twitter.entry> entries)
        {
            bool first = true;

            foreach (var tweet in entries)
            {
                if (first)
                {
                    model.name = tweet.user.name;
                    model.profile_image_url = tweet.user.profile_image_url;
                    model.screen_name = tweet.user.screen_name;
                    first = false;
                }
                model.Items.Add(new Twitter.entryModel
                {
                    created_at_display = convertDateString(tweet.created_at),
                    text = tweet.text
                });
            }
        }
        public static Tweets GetTweets(string screenName)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            Tweets ret = new Tweets();
            TwitterTweets tweets = new TwitterTweets();
            ret.screen_name = screenName;

            try
            {
                bool requestSucceeded = false;

                try
                {                    
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=" + screenName + "&count=25&include_rts=1");
                    request.Timeout = 4000;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Twitter.entry>));
                    var outStream = response.GetResponseStream();
                    tweets.Items = serializer.ReadObject(outStream) as List<Twitter.entry>;
                    outStream.Close();
                    convertEntries(ret, tweets.Items);

                    try
                    {
                        if (ret.Items.Count > 0) //overwrite file cache only when some items are returned
                        {
                            LocalResource ls = RoleEnvironment.GetLocalResource("localStore");
                            using (FileStream stream = File.OpenWrite(ls.RootPath + "WindowsAzure-" + screenName + ".json"))
                            {
                                serializer.WriteObject(stream, tweets.Items);
                            }
                        }
                    }
                    catch (Exception exp)
                    {
                        Trace.WriteLine(exp.ToString());
                    }

                    requestSucceeded = true;
                }
                catch (Exception exp)
                {
                    Trace.WriteLine(exp.ToString());
                    requestSucceeded = false;
                }

                if (!requestSucceeded)
                {
                    try
                    {
                        LocalResource ls = RoleEnvironment.GetLocalResource("localStore");
                        string newCachedFile = ls.RootPath + "WindowsAzure-" + screenName + ".json";
                        if (File.Exists(newCachedFile)) //has previously cached file
                        {
                            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Twitter.entry>));
                            using (FileStream stream = File.OpenRead(newCachedFile))
                            {
                                tweets.Items = serializer.ReadObject(stream) as List<Twitter.entry>;
                            }
                        }
                        else //fall back to default demo data
                        {
                            MemoryStream stream = new MemoryStream(Resources.WindowsAzure);
                            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Twitter.entry>));
                            tweets.Items = serializer.ReadObject(stream) as List<Twitter.entry>;
                            stream.Close();
                            convertEntries(ret, tweets.Items);
                        }
                    }
                    catch (Exception exp)
                    {
                        Trace.WriteLine(exp.ToString());
                    }
                }
            }
            catch (Exception exp)
            {
                Trace.WriteLine(exp.ToString());
            }

            watch.Stop();

            if (watch.ElapsedMilliseconds < 1500)
                Thread.Sleep((int)(1500 + rand.Next(0,200) - watch.ElapsedMilliseconds));

            return ret;
        }
 public static Tweets GetTweets(string screenName)
 {
     Stopwatch watch = new Stopwatch();
     watch.Start();
     Tweets ret = new Tweets();
     TwitterTweets tweets = new TwitterTweets();
     ret.screen_name = screenName;
     try
     {
         bool requestSucceeded = false;
         try
         {
             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=" + screenName + "&count=25&include_rts=1");
             request.Timeout = 4000;
             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Twitter.entry>));
             var outStream = response.GetResponseStream();
             tweets.Items = serializer.ReadObject(outStream) as List<Twitter.entry>;
             outStream.Close();
             convertEntries(ret, tweets.Items);
             requestSucceeded = true;
         }
         catch (Exception exp)
         {
             Trace.WriteLine(exp.ToString());
             requestSucceeded = false;
         }
         if (!requestSucceeded)
         {
             try
             {
                     MemoryStream stream = new MemoryStream(Resources.WindowsAzure);
                     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<Twitter.entry>));
                     tweets.Items = serializer.ReadObject(stream) as List<Twitter.entry>;
                     stream.Close();
                     convertEntries(ret, tweets.Items);
             }
             catch (Exception exp)
             {
                 Trace.WriteLine(exp.ToString());
             }
         }
     }
     catch (Exception exp)
     {
         Trace.WriteLine(exp.ToString());
     }
     watch.Stop();
     if (watch.ElapsedMilliseconds < 1500)
         Thread.Sleep((int)(1500 + rand.Next(0,200) - watch.ElapsedMilliseconds));
     return ret;
 }