Exemplo n.º 1
0
        public TwitterAccount(Account account)
            : base()
        {
            if (account.Type.Equals(Account.TWITTERTYPE))
            {
                // the local part
                this.CloneFrom(account);

                // the remote part

                BackgroundWorker bgWorker = new BackgroundWorker();

                bgWorker.DoWork += new DoWorkEventHandler(delegate(object wsender, DoWorkEventArgs args) {
                    try {
                        args.Result = null;
                        Twitterizer.TwitterResponse <Twitterizer.TwitterUser> showUserResponse = Twitterizer.TwitterUser.Show(TwitterTimeline.getTwitterAuthToken(account), account.getOption("username"));
                        if (showUserResponse.Result == Twitterizer.RequestResult.Success)
                        {
                            args.Result = showUserResponse.ResponseObject.ProfileImageLocation;
                        }
                    } catch (Exception ex) {
                        args.Result = "Unable to get the Twitter account: " + ex.Message;
                        args.Cancel = true;
                    }
                });

                bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object wsender, RunWorkerCompletedEventArgs args) {
                    if (!args.Cancelled && args.Error == null)
                    {
                        String url = args.Result as String;

                        if (url != null && !url.Equals(""))
                        {
                            this.ImageUrl = new Uri(url);
                        }
                    }
                });

                bgWorker.RunWorkerAsync();
            }
            else
            {
                throw new InvalidCastException("Unable to create a TwitterAccount from something other than a Twitter account");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// The recent modifications for a course
        /// </summary>
        /// <param name="account">With the account data to use on the query</param>
        private void GetRecentTweetsForAccount(Account account)
        {
            TimelineManager.ReportProgress(new Status(account.ID, "Loading account..."));

            // Load the cached data
            LinkedList <Frame> frames = TimelineManager.GetCachedFramesFromDB(account);

            foreach (Frame frame in frames)
            {
                this.AddFrame(frame);
            }

            frames.Clear();

            BackgroundWorker bgWorker = new BackgroundWorker();

            this.workers.AddLast(bgWorker);

            bgWorker.WorkerReportsProgress      = true;
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.DoWork += new DoWorkEventHandler(delegate(object wsender, DoWorkEventArgs args)  {
                // Load the remote data
                try {
                    Twitterizer.OAuthTokens authToken = TwitterTimeline.getTwitterAuthToken(account);

                    Twitterizer.TimelineOptions options = new Twitterizer.TimelineOptions();
                    String sinceStatusId = account.getOption("SinceStatusId");
                    if (sinceStatusId != null)
                    {
                        options.SinceStatusId = Convert.ToDecimal(sinceStatusId);
                    }
                    else
                    {
                        options.Count = 10;
                    }

                    Twitterizer.TwitterResponse <TwitterStatusCollection> timeline = Twitterizer.TwitterTimeline.HomeTimeline(authToken, options);

                    if (sinceStatusId != null && timeline.Result == Twitterizer.RequestResult.Success && timeline.ResponseObject.Count < 10)
                    {
                        options       = new Twitterizer.TimelineOptions();
                        options.Count = 10;
                        timeline      = Twitterizer.TwitterTimeline.HomeTimeline(authToken, options);
                    }

                    if (timeline.Result == Twitterizer.RequestResult.Success)
                    {
                        frames.Clear();
                        Frame frame;
                        foreach (Twitterizer.TwitterStatus status in timeline.ResponseObject)
                        {
                            frame          = new Frame(account, "[" + account.Name + "] @" + status.User.ScreenName, status.Text, status.CreatedDate, ((status.User.Website != null) ? new Uri(status.User.Website) : null), Convert.ToInt64(status.Id));
                            frame.ImageUrl = new Uri(status.User.ProfileImageLocation);
                            frames.AddLast(frame);
                        }
                        if (timeline.ResponseObject[0] != null)
                        {
                            sinceStatusId = Convert.ToString(timeline.ResponseObject[0].Id);
                        }
                        args.Result = Convert.ToString(sinceStatusId);
                    }
                } catch {
                    //MessageBox.Show(ex.Message+ ex.StackTrace);
                }
            });

            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object wsender, RunWorkerCompletedEventArgs args)  {
                TimelineManager.CacheFramesOnDB(account, frames);
                int totalFramesAdded = 0;
                foreach (Frame frame in frames)
                {
                    if (TimelineManager.AddFrame(frame))
                    {
                        totalFramesAdded++;
                    }
                }
                TimelineManager.RegisterEvent(account);

                if (!args.Cancelled && args.Error == null)
                {
                    String sinceStatusId = args.Result as String;
                    account.setOption("SinceStatusId", sinceStatusId);
                }
                account.LastUpdate = DateTime.Now;
                ConfigurationManager.UpdateUserAccount(account);

                TimelineManager.ReportProgress(new Status(account.ID, totalFramesAdded + " new twitts loaded."));
                this.workers.Remove(bgWorker);
            });
            bgWorker.RunWorkerAsync(account);
        }