1: using System.Collections.Generic;
2: using System.Configuration;
3: using TweetSharp;
4:
5: namespace Ia.Cl.Model
6: {
7: ////////////////////////////////////////////////////////////////////////////
8:
9: /// <summary publish="true">
10: /// Twitter API support class.
11: /// </summary>
12: /// <remarks>
13: /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
14: ///
15: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
16: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
17: ///
18: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20: ///
21: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
22: ///
23: /// Copyright notice: This notice may not be removed or altered from any source distribution.
24: /// </remarks>
25: public class Twitter
26: {
27: private string consumerKey, consumerSecret, accessToken, accessTokenSecret;
28: private TwitterService service;
29:
30: ////////////////////////////////////////////////////////////////////////////
31:
32: /// <summary>
33: ///
34: /// </summary>
35: public Twitter()
36: {
37: /*
38: * <appSettings>
39: * <add key="twitterConsumerKey" value="*" />
40: * <add key="twitterConsumerSecret" value="*" />
41: * <add key="twitterAccessToken" value="*" />
42: * <add key="twitterAccessTokenSecret" value="*" />
43: */
44:
45: consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"].ToString();
46: consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"].ToString();
47: accessToken = ConfigurationManager.AppSettings["twitterAccessToken"].ToString();
48: accessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"].ToString();
49:
50: Initialize();
51: }
52:
53: ////////////////////////////////////////////////////////////////////////////
54:
55: /// <summary>
56: ///
57: /// </summary>
58: public Twitter(string _consumerKey, string _consumerSecret, string _accessToken, string _accessTokenSecret)
59: {
60: consumerKey = _consumerKey;
61: consumerSecret = _consumerSecret;
62: accessToken = _accessToken;
63: accessTokenSecret = _accessTokenSecret;
64:
65: Initialize();
66: }
67:
68: ////////////////////////////////////////////////////////////////////////////
69:
70: /// <summary>
71: ///
72: /// </summary>
73: private void Initialize()
74: {
75: // In v1.1, all API calls require authentication
76: service = new TwitterService(consumerKey, consumerSecret);
77: service.AuthenticateWith(accessToken, accessTokenSecret);
78: }
79:
80: ////////////////////////////////////////////////////////////////////////////
81:
82: /// <summary>
83: ///
84: /// </summary>
85: public List<TwitterStatus> TweetListAboveId(long aboveTweetId)
86: {
87: List<TwitterStatus> tweetList;
88: IEnumerable<TwitterStatus> iEnumerableTweetList;
89:
90: tweetList = new List<TwitterStatus>();
91: iEnumerableTweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
92:
93: foreach (TwitterStatus tweet in iEnumerableTweetList)
94: {
95: if (tweet.Id > aboveTweetId) tweetList.Add(tweet);
96: }
97:
98: return tweetList;
99: }
100:
101: ////////////////////////////////////////////////////////////////////////////
102:
103: /// <summary>
104: ///
105: /// </summary>
106: public List<TwitterDirectMessage> DirectMessagesReceivedListAboveId(long aboveDmId)
107: {
108: List<TwitterDirectMessage> dmList;
109: IEnumerable<TwitterDirectMessage> iEnumerableDmList;
110:
111: dmList = new List<TwitterDirectMessage>();
112: iEnumerableDmList = service.ListDirectMessagesReceived(new ListDirectMessagesReceivedOptions());
113:
114: foreach (TwitterDirectMessage dm in iEnumerableDmList)
115: {
116: if (dm.Id > aboveDmId) dmList.Add(dm);
117: }
118:
119: return dmList;
120: }
121:
122: ////////////////////////////////////////////////////////////////////////////
123:
124: /// <summary>
125: ///
126: /// </summary>
127: public List<TwitterStatus> TweetList()
128: {
129: //
130: List<TwitterStatus> tweetList;
131: IEnumerable<TwitterStatus> iEnumerableTweetList;
132:
133: tweetList = new List<TwitterStatus>();
134: iEnumerableTweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
135:
136: foreach (TwitterStatus tweet in tweetList)
137: {
138: tweetList.Add(tweet);
139: }
140:
141: return tweetList;
142: }
143:
144: ////////////////////////////////////////////////////////////////////////////
145:
146: /// <summary>
147: ///
148: /// </summary>
149: public List<TwitterDirectMessage> DirectMessageList()
150: {
151: //
152: List<TwitterDirectMessage> dmList;
153: IEnumerable<TwitterDirectMessage> iEnumerableTweetList;
154:
155: dmList = new List<TwitterDirectMessage>();
156: iEnumerableTweetList = service.ListDirectMessagesReceived(new ListDirectMessagesReceivedOptions());
157:
158: foreach (TwitterDirectMessage dm in dmList)
159: {
160: dmList.Add(dm);
161: }
162:
163: return dmList;
164: }
165:
166: ////////////////////////////////////////////////////////////////////////////
167:
168: /// <summary>
169: ///
170: /// </summary>
171: public TwitterStatus ReadLastTweet()
172: {
173: // send tweet
174: TwitterStatus ts;
175: IEnumerable<TwitterStatus> tweetList;
176:
177: ts = null;
178: tweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
179:
180: foreach (TwitterStatus tweet in tweetList)
181: {
182: ts = tweet;
183: break; // to read only the first;
184: }
185:
186: return ts;
187: }
188:
189: ////////////////////////////////////////////////////////////////////////////
190:
191: /// <summary>
192: /// Delete Tweet (this function is not working properly)
193: /// <param name="tweetId">Tweet Id to delete</param>
194: /// <see cref="http://stackoverflow.com/questions/4810076/tweetsharp-remove-undo-retweet"/>
195: /// </summary>
196: public TwitterStatus DeleteTweet(long tweetId)
197: {
198: // delete tweet
199: TwitterStatus ts;
200:
201: ts = service.DeleteTweet(new DeleteTweetOptions() { Id = tweetId });
202:
203: return ts;
204: }
205:
206: ////////////////////////////////////////////////////////////////////////////
207:
208: /// <summary>
209: /// Delete DM
210: /// </summary>
211: public TwitterDirectMessage DeleteDirectMessage(long dmId)
212: {
213: // delete tweet
214: TwitterDirectMessage dm;
215:
216: dm = service.DeleteDirectMessage(new DeleteDirectMessageOptions() { Id = dmId });
217:
218: return dm;
219: }
220:
221: ////////////////////////////////////////////////////////////////////////////
222:
223: /// <summary>
224: ///
225: /// </summary>
226: public void SendTweet(string status)
227: {
228: // send tweet
229:
230: SendTweetOptions sto;
231:
232: sto = new SendTweetOptions();
233:
234: sto.Status = status; // "Testing (2)...";
235:
236: service.SendTweet(sto);
237: }
238:
239: ////////////////////////////////////////////////////////////////////////////
240:
241: /// <summary>
242: ///
243: /// </summary>
244: public void SendDirectMessage(string screenName, string text)
245: {
246: // direct message
247:
248: SendDirectMessageOptions sdmo;
249:
250: sdmo = new SendDirectMessageOptions();
251:
252: sdmo.ScreenName = screenName; // "@abcd";
253: sdmo.Text = text; // "Testing (2)...";
254:
255: service.SendDirectMessage(sdmo);
256: }
257:
258: ////////////////////////////////////////////////////////////////////////////
259: ////////////////////////////////////////////////////////////////////////////
260: }
261: }