Last active
August 11, 2017 12:22
-
-
Save birksy89/d8dd7db1f437f8f2990975931ac71ad0 to your computer and use it in GitHub Desktop.
Example of adding User Tweets or Tweets from a query.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LinqToTwitter; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace Christoc.Modules.DNNSearchTest.Components | |
{ | |
public class TwitterManager | |
{ | |
public List<Status> functionA() | |
{ | |
//Define Empty List of Twitter Statuses | |
List<Status> statusList = new List<Status>(); | |
Task.Run(async () => | |
{ | |
//Authorize | |
var auth = new ApplicationOnlyAuthorizer | |
{ | |
CredentialStore = new InMemoryCredentialStore() | |
{ | |
ConsumerKey = "xxx", | |
ConsumerSecret = "yyy" | |
} | |
}; | |
await auth.AuthorizeAsync(); | |
var twitterCtx = new TwitterContext(auth); | |
//Perform Search - Using Query | |
var srch = await (from search in twitterCtx.Search | |
where search.Type == SearchType.Search && | |
search.Query == "LINQ to Twitter" | |
select search) | |
.SingleOrDefaultAsync(); | |
//statusList = srch.Statuses; | |
//Perform Search - Using Username | |
var tweets = await (from tweet in twitterCtx.Status | |
where tweet.Type == StatusType.User && | |
tweet.ScreenName == "purplecreative_" | |
select tweet) | |
.ToListAsync(); | |
statusList = tweets; | |
}).Wait(); | |
foreach (var tweet in statusList) | |
{ | |
tweet.Text = TextAsHtml(tweet); | |
} | |
return statusList; | |
} | |
public static string TextAsHtml(Status status) | |
{ | |
Regex _parseUrls = new Regex("\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^\\p{P}\\s]|/)))", RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
Regex _parseMentions = new Regex("(^|\\W)@([A-Za-z0-9_]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
Regex _parseHashtags = new Regex("[#]+[A-Za-z0-9-_]+", RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
string tweetText = status.Text; | |
if (!String.IsNullOrEmpty(tweetText)) | |
{ | |
// Replace URLs | |
foreach (var urlMatch in _parseUrls.Matches(tweetText)) | |
{ | |
Match match = (Match)urlMatch; | |
tweetText = tweetText.Replace(match.Value, String.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", match.Value)); | |
} | |
// Replace Mentions | |
foreach (var mentionMatch in _parseMentions.Matches(tweetText)) | |
{ | |
Match match = (Match)mentionMatch; | |
if (match.Groups.Count == 3) | |
{ | |
string value = match.Groups[2].Value; | |
string text = "@" + value; | |
tweetText = tweetText.Replace(text, String.Format("<a href=\"http://twitter.com/{0}\" target=\"_blank\">{1}</a>", value, text)); | |
} | |
} | |
// Replace Hash Tags | |
foreach (var hashMatch in _parseHashtags.Matches(tweetText)) | |
{ | |
Match match = (Match)hashMatch; | |
string query = Uri.EscapeDataString(match.Value.TrimStart('#')); | |
tweetText = tweetText.Replace(match.Value, String.Format("<a href=\"https://twitter.com/hashtag/{0}\" target=\"_blank\">{1}</a>", query, match.Value)); | |
} | |
} | |
return tweetText; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment