예제 #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        IOAuthCredentials creds = new SessionStateCredentials();
        creds.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
        creds.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
        creds.AccessToken = ConfigurationManager.AppSettings["twitterAccessToken"];
        creds.OAuthToken = ConfigurationManager.AppSettings["twitterOauthToken"];
        //Auth Object With Credentials
        var auth = new WebAuthorizer
        {
            Credentials = creds
        };     
        var twitterCtx = new TwitterContext(auth);

        var users =
            (from user in twitterCtx.Status
             where user.Type == StatusType.User &&
                   user.ScreenName == "JoeMayo" &&
                   user.Count == 200
             select user)
            .ToList();

        UserListView.DataSource = users;
        UserListView.DataBind();
    }
예제 #2
0
        protected string GetImageInfo(string twitterName, string defaultURL)
        {
            if (twitterName == null)
                return defaultURL;
            string twitterURL = defaultURL;
            var auth = new MvcAuthorizer();
            SessionStateCredentials s = new SessionStateCredentials();
            s.ConsumerKey = "fUpi8KuU3hMWsCHueZIww";
            s.ConsumerSecret = "4gCFXwi5zW5CYIoGSNgydL9dmqVM9T9BUS9ElrMI";
            auth.Credentials = s;
            var twitterCtx = new TwitterContext(auth);
            try
            {
                var userResponse =
                     (from user in twitterCtx.User
                      where user.Type == UserType.Lookup &&
                            user.ScreenName == twitterName.Replace("@", "")
                      select user).ToList();

                if (userResponse == null)
                {
                    return defaultURL;
                }
                twitterURL = userResponse.FirstOrDefault().ProfileImageUrl;
            }
            catch
            {
                return defaultURL;
            }
            return twitterURL;
        }
        public ActionResult PostToTwitter(string url, string text)
        {
            if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(text))
                return Json(new { Success = false, Message = "You must enter some text to tweet" });

            IOAuthCredentials credentials = new SessionStateCredentials();

            credentials.ConsumerKey = _config.TwitterConsumerKey;
            credentials.ConsumerSecret = _config.TwitterConsumerSecret;
            credentials.AccessToken = _config.TwitterAccessTokenSecret;
            credentials.OAuthToken = _config.TwitterAccessToken;

            MvcAuthorizer auth = new MvcAuthorizer { Credentials = credentials };

            TwitterContext twitter = new TwitterContext(auth);

            try
            {
                twitter.UpdateStatus(text + " " + url);

                return Json(new { Success = true });
            }
            catch (Exception e)
            {
                return Json(new { Success = false, Message = e.Message });
            }
        }
예제 #4
0
    protected void Page_Load(object sender, EventArgs e)
    {
        IOAuthCredentials credentials = new SessionStateCredentials();

        if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
        {
            credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
        }

        auth = new WebAuthorizer
        {
            Credentials = credentials,
            PerformRedirect = authUrl => Response.Redirect(authUrl)
        };

        if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
        {
            auth.CompleteAuthorization(Request.Url);
        }

        if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||
            string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
        {
            // The user needs to set up the web.config file to include Twitter consumer key and secret.
            PrivateDataMultiView.SetActiveView(SetupTwitterConsumer);
        }
        else if (auth.IsAuthorized)
        {
            screenNameLabel.Text = auth.ScreenName;
            PrivateDataMultiView.SetActiveView(ViewPrivateUpdates);
            updateBox.Focus();
        }
        else
        {
            PrivateDataMultiView.SetActiveView(AuthorizeTwitter);
        }

        if (auth.IsAuthorized)
        {
            twitterCtx = new TwitterContext(auth);

            var search =
                (from srch in twitterCtx.Search
                 where srch.Type == SearchType.Search &&
                       srch.Query == "LINQ to Twitter"
                 select srch)
                .SingleOrDefault();

            TwitterListView.DataSource = search.Statuses;
            TwitterListView.DataBind(); 
        }
    }
예제 #5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //var twitterCtx = new TwitterContext();

            IOAuthCredentials credentials = new SessionStateCredentials();

            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new WebAuthorizer
            {
                Credentials = credentials,
                PerformRedirect = authUrl => Response.Redirect(authUrl)
            };

            if (!Page.IsPostBack)
            {
                auth.CompleteAuthorization(Request.Url);
            }

            else if (auth.IsAuthorized)
            {
                Label1.Text = "Congratulations, you're authorized";

            }

            var twitterCtx = new TwitterContext(auth);
            //twitterCtx = auth.IsAuthorized ? new TwitterContext(auth) : new TwitterContext();

            UserId = credentials.UserId;
            string token = credentials.OAuthToken;
            string sec = credentials.AccessToken;

            ////// show token and secret in index page after Authorization
            Label2.Text = token;
            Label3.Text = sec;
            Label4.Text = credentials.UserId;

            ////// creat twtUser object to store the info about the user
            TwitterUser twt = new TwitterUser(UserId, token, sec);

            ////// move user's info to another page throw Session
            Session["objTwt"] = twt;
        }
예제 #6
0
        public ActionResult Index()
        {
            IOAuthCredentials credentials = new SessionStateCredentials();
            MvcAuthorizer auth;
            TwitterContext twitterCtx;
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings[CommonConstants.ConsumerKey];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings[CommonConstants.ConsumerSecret];
                credentials.AccessToken = ConfigurationManager.AppSettings[CommonConstants.AccessToken];
                credentials.OAuthToken = ConfigurationManager.AppSettings[CommonConstants.OAuthToken];
                credentials.ScreenName = ConfigurationManager.AppSettings[CommonConstants.ScreenName];
            }

            auth = new MvcAuthorizer
            {
                Credentials = credentials
            };
            auth.CompleteAuthorization(Request.Url);
            if (!auth.IsAuthorized)
            {
                Uri specialUri = new Uri(Request.Url.ToString());
                return auth.BeginAuthorization(specialUri);
            }
             twitterCtx = new TwitterContext(auth);

            var friendTweets =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.Home
                 select new TwitterEntity
                 {

                     ScreenName = tweet.User.Name,
                     Tweet = tweet.Text

                 })
                .ToList();

            return View(friendTweets);
        }
예제 #7
0
        private void UpdateStatus()
        {
            IOAuthCredentials credentials = new SessionStateCredentials();

            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"];
            }

            Authorizer.Auth = new WebAuthorizer
                {
                    Credentials = credentials,
                    PerformRedirect = authUrl => Response.Redirect(authUrl)
                };

            Authorizer.Auth.CompleteAuthorization(Request.Url);
            Authorizer.Auth.BeginAuthorization(Request.Url);
        }
예제 #8
0
    private List<SessionsResult> SessionsToTweetBeforeBigEvent(out StringBuilder sbText)
    {
        var codeCampYearResult = CodeCampYearManager.I.Get(new CodeCampYearQuery {Id = Utils.CurrentCodeCampYear}).FirstOrDefault();
        if (codeCampYearResult != null)
        {
            DateTime startTweetsAfter =
                codeCampYearResult.
                    CampStartDate.Subtract(new TimeSpan(12, 0, 0, 0));

            if (startTweetsAfter.CompareTo(DateTime.Now) > 0)
            {
                sbText = new StringBuilder("No Tweeting because too soon for SessionsToTweetBeforeBigEvent()");
                return new List<SessionsResult>();
            }
        }
        else
        {
            sbText = new StringBuilder("No Tweeting because no code camp year found");
            return new List<SessionsResult>();
        }

        // if got here, then we should start tweeting all sessions again

        var sessionsToTweet =
            SessionsManager.I.Get(new SessionsQuery
                                      {
                                          TweetLineTweetedPreCamp = false,
                                          WithSchedule = true,
                                          WithSpeakers = true,
                                          CodeCampYearId = Utils.CurrentCodeCampYear
                                         // Id = 953 // test for just scottgu and email comes to me
                                      }).Where(a => !a.SessionTime.Equals("Agenda Not Set Yet")).
                                      OrderBy(a => a.SessionTimesResult.StartTime.HasValue ? a.SessionTimesResult.StartTime.Value : DateTime.Now).Take(_tweetsToProcessAtOnce).
                ToList();

        sbText = new StringBuilder();
        foreach (var session in sessionsToTweet)
        {

            DateTime sessionTime;
            string sessionTimeFriendly;
            string speakerNames;
            string speakerHandles;
            string hashTags;
            Utils.GetSessionHandlesHashTagsTime(session, out speakerNames, out speakerHandles,out hashTags, out sessionTime, out sessionTimeFriendly);
            string sessionTitle = Utils.ClearSpecialCharacters(session.Title);

            string sessionUrl = String.Format("http://siliconvalley-codecamp.com/Sessions.aspx?sessionid={0}",
                                              session.Id);
            string sessionUrlShort = API.Bit(_bitLyApiUsername, _bitLyApiKey,
                                             HttpContext.Current.Server.HtmlEncode(sessionUrl), "Shorten");

            // "at 9:45AM 10/6 Speaker Douglas Crockford @dougc Title: Gonads and Ponads  #svcc #json http://dkfjdkf
            string template = "{0}  {1} {2} \"{{0}}\" #svcc {3} {4} ReTweet!";

            string tweetFirstPart =
                String.Format(template, sessionTimeFriendly, speakerNames, speakerHandles, hashTags,
                              sessionUrlShort).Trim();
            int spaceLeftForTitle = 130 - tweetFirstPart.Length;
            int titleLen = Math.Min(sessionTitle.Length, spaceLeftForTitle);

            string tweet = String.Format(tweetFirstPart, sessionTitle.Substring(0, titleLen)).Trim();

            // finally, do the tweet
            string credentialString = _twitterCredential;

            var credentials = new SessionStateCredentials();
            credentials.Load(credentialString);

            var auth = new WebAuthorizer
            {
                Credentials = credentials
            };

            var twitterCtx = new TwitterContext(auth);
            const decimal lattitude = (decimal)37.362056;
            const decimal longitude = (decimal)122.131056;
            if (!testMode)
            {
                Status tweetInfo = twitterCtx.UpdateStatus(tweet, lattitude, longitude);
                Label1.Text = tweetInfo.Text; // not sure if this helps
            }
            sbText.AppendLine(tweet);

            // update
            session.TweetLinePreCamp = tweet;
            session.TweetLineTweetedDatePreCamp = DateTime.Now;
            session.TweetLineTweetedPreCamp = true;

            if (!testMode)
            {
                SessionsManager.I.Update(session);
            }

            // finally, send the emails.
            foreach (var rec in session.SpeakersList)
            {
                SendEmailToSpeakerJustBeforeEvent(rec.UserFirstName, rec.UserLastName, rec.Email, session.Title, tweet);
            }
        }
        return sessionsToTweet;
    }
예제 #9
0
    private List<SessionsResult> SessionsToTweetAfterSignup(out StringBuilder sbText)
    {
        //DateTime compareDateTime = DateTime.Now.Subtract(new TimeSpan(0, 1, 0, 0));
        // do a max of 2 at a time.  these go every 30 minutes
        var sessionsToTweet = SessionsManager.I.Get(new SessionsQuery
                                                        {
                                                            TweetLineTweeted = false,
                                                            WithSpeakers = true,
                                                            WithSchedule = true,
                                                            CodeCampYearId = Utils.CurrentCodeCampYear
                                                        }).Where(
                                                            a =>
                                                            a.Createdate != null &&
                                                            a.Createdate.Value.CompareTo(
                                                                DateTime.Now.Subtract(new TimeSpan(0, 48, 0, 0))) <
                                                            0).
            OrderByDescending(a => a.Id).Take(_tweetsToProcessAtOnce).ToList();

        sbText = new StringBuilder();
        foreach (var session in sessionsToTweet)
        {
            var speakers = Utils.GetSpeakersBySessionId(session.Id);
            var sb = new StringBuilder();
            foreach (var speaker in speakers)
            {
                string speakerName = string.Format("{0} {1},", speaker.UserFirstName, speaker.UserLastName);
                sb.Append(speakerName);
            }
            string speakerList = sb.ToString();
            if (speakerList.Length > 0 && speakerList.EndsWith(","))
            {
                speakerList = speakerList.Substring(0, speakerList.Length - 1);
            }

            string sessionUrl = String.Format("http://siliconvalley-codecamp.com/Sessions.aspx?sessionid={0}",
                                              session.Id);
            string sessionUrlShort = API.Bit(_bitLyApiUsername, _bitLyApiKey,
                                             HttpContext.Current.Server.HtmlEncode(sessionUrl), "Shorten");

            string template = "NEW SESSION: {0}  Speaker: {1} #svcc Title: \"{2}\"";
            if (speakerList.Length > 0 && speakers.Count > 1)
            {
                template = "NEW SESSION: {0}  Speakers: {1} #svcc Title: \"{2}\"";
            }

            string tweet = String.Format(template, sessionUrlShort, speakerList, session.Title);
            if (tweet.Length > 139)
            {
                tweet = tweet.Substring(0, 136) + "...";
            }

            // finally, do the tweet
            string credentialString = _twitterCredential;

            var credentials = new SessionStateCredentials();
            credentials.Load(credentialString);

            var auth = new WebAuthorizer
                           {
                               Credentials = credentials
                           };

            var twitterCtx = new TwitterContext(auth);
            const decimal lattitude = (decimal) 37.362056;
            const decimal longitude = (decimal) 122.131056;
            if (!testMode)
            {
                Status tweetInfo = twitterCtx.UpdateStatus(tweet, lattitude, longitude);
                Label1.Text = tweetInfo.Text; // not sure if this helps
            }

            sbText.AppendLine(tweet);

            // update
            session.TweetLine = tweet;
            session.TweetLineTweetedDate = DateTime.Now;
            session.TweetLineTweeted = true;
            if (!testMode)
            {
                SessionsManager.I.Update(session);
            }

            // finally, send the emails.
            foreach (var rec in speakers)
            {
                SendEmailToSpeaker(rec.UserFirstName, rec.UserLastName, rec.Email, session.Title, tweet);
            }
        }
        return sessionsToTweet;
    }