public static string GetItemUrlFromPost(this string summary) { const string regex = @"<a.*?href=(""|')(?<href>.*?)(""|').*?>(?<value>.*?)</a>"; var matches = Regex.Matches(summary, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline); var links = new List<string>(); foreach (Match item in matches) { var link = item.Groups[3].Value; links.Add(link); } var linkToReturn = links.First(l => l.Contains("comments")); var shortenUrls = Convert.ToBoolean(ConfigurationHelper.GetValue("ShortenURLs")); if(shortenUrls) { var bitlyLogin = ConfigurationHelper.GetValue("BitlyLogin"); var bitlyApiKey = ConfigurationHelper.GetValue("BitlyAPIKey"); var bitly = new BitlyService(bitlyLogin, bitlyApiKey); return bitly.Shorten(linkToReturn); } return linkToReturn; }
protected void Page_Load(object sender, EventArgs e) { BitlyService s = new BitlyService("bitlyapidemo", "R_0da49e0a9118ff35f52f629d2d71bf07"); string shortened; StatusCode status = s.Shorten("http://cnn.com", out shortened); IBitlyResponse[] shortUrls = s.Shorten(new string[] { "http://cnn.com", "http://google.com" }, out status); }
public ActionResult GetLinkForSummary(long id) { PointDataSummary summary = PointDataSummary.Find(c => c.Id == id).SingleOrDefault(); if (summary == null) { HttpContext.Response.StatusCode = 404; return Json(new { SummaryId = id }, JsonRequestBehavior.AllowGet); } IBitlyService service = new BitlyService(BitlyLogin, BitlyAPIKey); string long_url = this.BuildUrlFromExpression<SummariesController>(c => c.ShowById(id, "html")); string short_url = null; service.Shorten(long_url, out short_url); return Json(new { SummaryId = id, long_url = long_url, short_url = short_url }, JsonRequestBehavior.AllowGet); }
private static string GetBitlyUrl(ISettings settingsRepository, string url, string urlFormat) { var bitlyUsername = ApplicationConfiguration.BitlyUserName; var bitlyApiKey = ApplicationConfiguration.BitlyApiKey; if (string.IsNullOrEmpty(bitlyUsername) || string.IsNullOrEmpty(bitlyApiKey)) { return null; } try { var bitlyService = new BitlyService(bitlyUsername, bitlyApiKey); string shortUrl; bitlyService.Shorten(string.Format(urlFormat, settingsRepository.BlogAkismetUrl, url), out shortUrl); return shortUrl; } catch { return null; } }
public static void SendTweet(string tweet, string appendUrl, SessionProperties sessionProperties) { if (TwitterService == null) { InitiateTwitterAuthentication(sessionProperties); } if (TwitterService == null) { WebControlManager.SendAndLogErrorMessage(new Exception("TwitterService not initialized"), Parameters.Instance.MailSender, Parameters.Instance.SupportMail); //clear authentication, guess we need to authenticate again? Parameters.Instance.TwitterAccessToken = null; Parameters.Instance.TwitterAccessTokenSecret = null; return; } try { //format the string, replace if (tweet.Contains("&")) { tweet = HttpUtility.HtmlDecode(tweet); } string shortUrl = String.Empty; //parse url to shorturl if (!String.IsNullOrEmpty(appendUrl)) { IBitlyService s = new BitlyService("o_3cpfcndji4", "R_8e203358cb5ca0f68d809419b056b192"); string shortened = s.Shorten(appendUrl); if (shortened != null) { shortUrl = " " + shortened; } } var maxLength = 140 - shortUrl.Length; if (tweet.Length > maxLength) tweet = tweet.Substring(0, maxLength); tweet += shortUrl; TwitterService.SendTweet(new SendTweetOptions(){Status = tweet}); // Likely this is an error; we don't have to go fishing for it TwitterError error = TwitterService.Response.Error; TwitterResponse response = TwitterService.Response; if (error != null || response.StatusCode != HttpStatusCode.OK) { // You now know you have a real error from Twitter, and can handle it string message; if (error != null) { message = String.Format("Twitter error: {0} ({1})", error.Message, error.Code); } else { message = String.Format("Twitter response status not ok: {0} ({1})\n{2}", response.StatusDescription, response.StatusCode, response.Response); } WebControlManager.SendAndLogErrorMessage(new Exception(message), Parameters.Instance.MailSender, Parameters.Instance.SupportMail); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }