forked from umbraco/UmbracoDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterFeed.cshtml
217 lines (174 loc) · 7.65 KB
/
TwitterFeed.cshtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
@*
Macro Parameters To Create
Show:True Alias:twitterUsername Name:Twitter Username Type:Textstring Defaults to: umbraco
Show:True Alias:includeRTs Name:Include Retweets Type:Textstring Defaults to: false
Show:True Alias:excludeReplies Name:Exclude Replies Type:Textstring Defaults to: false
Show:True Alias:noTweets Name:Number of Tweets Type:Integer Defaults to: 1
*@
var twitterUsername = String.IsNullOrEmpty(Parameter.twitterUsername) ? "umbraco" : Parameter.twitterUsername;
var includeRTs = String.IsNullOrEmpty(Parameter.includeRTs) ? false : Parameter.includeRTs;
var excludeReplies = String.IsNullOrEmpty(Parameter.excludeReplies) ? false : Parameter.excludeReplies;
var noTweets = String.IsNullOrEmpty(Parameter.noTweets) ? 1 : Parameter.noTweets;
@* Twitter JSON URL *@
var twitterURL = string.Format(
"https://api.twitter.com/1/statuses/user_timeline.json?screen_name={0}&include_rts={1}&exclude_replies={2}&include_entities=1&count={3}",
twitterUsername,
includeRTs,
excludeReplies,
noTweets);
}
@* Fetch the JSON from Twitters API *@
@using (var client = new System.Net.WebClient())
{
@* Fetch the JSON from Twitter *@
var response = client.DownloadString(new Uri(twitterURL));
@* Decode the JSON so we can interate over it *@
var tweets = Json.Decode(response);
<ul>
@foreach (var tweet in tweets)
{
<li>
@* Tweet with formatted links *@
<h3>@formatLinks(tweet.text, tweet.entities)</h3>
<p>
@* Format Tweet Date and ouput as 24/03/12 @14:05 *@
<em>@formatDate(tweet.created_at).ToString("dd/MM/yy @ HH:mm")</em>
</p>
<p>
@* Profile Image *@
<img src="@tweet.user.profile_image_url" alt="@tweet.user.Name" />
@* Your real name (not profile name) *@
<strong>@tweet.user.name</strong>
</p>
@* Google Map (if tweet has geo info) *@
@if (tweet.geo != null)
{
@Html.Raw(displayMap(tweet.geo));
}
@* Dislay Image (if tweet has image attached *@
@if (tweet.entities.media != null)
{
<a href="@tweet.entities.media[0].media_url" target="_blank">
<img src="@tweet.entities.media[0].media_url:thumb" />
</a>
}
</li>
}
</ul>
}
@functions
{
DateTime formatDate(string twitterDate)
{
//Example tweet date
//Fri Mar 02 16:09:35 +0000 2012
//ddd MMM dd HH:mm:ss zz00 yyyy
DateTime tweetDate = DateTime.ParseExact(twitterDate, "ddd MMM dd HH:mm:ss zz00 yyyy", null);
return tweetDate;
}
String displayMap(dynamic geo)
{
//Get the lat & long values
var tweetLat = geo.coordinates[0];
var tweetLong = geo.coordinates[1];
//Format the string to return the image
var googleMap = string.Format("http://maps.googleapis.com/maps/api/staticmap?center={0},{1}&zoom=14&size=250x250&maptype=roadmap&sensor=false&markers={0}, {1}", tweetLat, tweetLong);
var mapImage = string.Format("<img src='{0}' alt='map' />", googleMap);
return mapImage;
}
IHtmlString formatLinks(string tweet, dynamic entities)
{
//A List of tweet entities so we can sort all of them
IList<tweetEntity> tweetEntities = new List<tweetEntity>();
//Get URLs
var links = entities.urls;
//Check we have links to loop over
if (links != null)
{
//For each link in the collection of links
foreach (var link in links)
{
var startPosition = link.indices[0];
var endPosition = link.indices[1];
var length = endPosition - startPosition;
var url = link.url; //The short t.co link
var displayURL = link.display_url; //A friendly version of the full link (may be truncated)
var newText = string.Format("<a href='{0}' target='_blank'>{1}</a>", url, displayURL);
var oldText = tweet.Substring(startPosition, length);
//Create a new entity
tweetEntity entity = new tweetEntity();
entity.startPosition = startPosition;
entity.endPosition = endPosition;
entity.newText = newText;
entity.oldText = oldText;
//Add it to the collection
tweetEntities.Add(entity);
}
}
//Get user mentions (@umbraco)
var mentions = entities.user_mentions;
//Check we have mentions to loop over
if (mentions != null)
{
//For each mention in the collection of mentions
foreach (var mention in mentions)
{
var startPosition = mention.indices[0];
var endPosition = mention.indices[1];
var length = endPosition - startPosition;
var username = mention.screen_name;
var newText = string.Format("<a href='http://twitter.com/{0}' target='_blank'>@{0}</a>", username);
var oldText = tweet.Substring(startPosition, length);
//Create a new entity
tweetEntity entity = new tweetEntity();
entity.startPosition = startPosition;
entity.endPosition = endPosition;
entity.newText = newText;
entity.oldText = oldText;
//Add to collection
tweetEntities.Add(entity);
}
}
//Get hashtags
var hashtags = entities.hashtags;
//Check we have hash to loop over
if (hashtags != null)
{
foreach (var hash in hashtags)
{
var startPosition = hash.indices[0];
var endPosition = hash.indices[1];
var length = endPosition - startPosition;
var hashtag = hash.text;
var newText = string.Format("<a href='http://twitter.com/search/%23{0}' target='_blank'>#{0}</a>", hashtag);
var oldText = tweet.Substring(startPosition, length);
//Create a new entity
tweetEntity entity = new tweetEntity();
entity.startPosition = startPosition;
entity.endPosition = endPosition;
entity.newText = newText;
entity.oldText = oldText;
//Add to collection
tweetEntities.Add(entity);
}
}
//For each item in the tweet entities in reverse order
//If we update the string in reverse order the remaining start/end indexs will still be correct
foreach (var item in tweetEntities.OrderByDescending(x => x.startPosition))
{
//Lets update the tweet text
tweet = tweet.Replace(item.oldText, item.newText);
}
//Return the new tweet with all the links added in
return Html.Raw(tweet);
}
public class tweetEntity
{
public int startPosition { get; set; }
public int endPosition { get; set; }
public string oldText { get; set; }
public string newText { get; set; }
}
}