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: // I stopped using Twitter 2021-01
28:
29: private string consumerKey, consumerSecret, accessToken, accessTokenSecret;
30: private TwitterService service;
31:
32: ////////////////////////////////////////////////////////////////////////////
33:
34: /// <summary>
35: ///
36: /// </summary>
37: public Twitter()
38: {
39: /*
40: * <appSettings>
41: * <add key="twitterConsumerKey" value="*" />
42: * <add key="twitterConsumerSecret" value="*" />
43: * <add key="twitterAccessToken" value="*" />
44: * <add key="twitterAccessTokenSecret" value="*" />
45: */
46:
47: consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"].ToString();
48: consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"].ToString();
49: accessToken = ConfigurationManager.AppSettings["twitterAccessToken"].ToString();
50: accessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"].ToString();
51:
52: Initialize();
53: }
54:
55: ////////////////////////////////////////////////////////////////////////////
56:
57: /// <summary>
58: ///
59: /// </summary>
60: public Twitter(string _consumerKey, string _consumerSecret, string _accessToken, string _accessTokenSecret)
61: {
62: consumerKey = _consumerKey;
63: consumerSecret = _consumerSecret;
64: accessToken = _accessToken;
65: accessTokenSecret = _accessTokenSecret;
66:
67: Initialize();
68: }
69:
70: ////////////////////////////////////////////////////////////////////////////
71:
72: /// <summary>
73: ///
74: /// </summary>
75: private void Initialize()
76: {
77: // In v1.1, all API calls require authentication
78: service = new TwitterService(consumerKey, consumerSecret);
79: service.AuthenticateWith(accessToken, accessTokenSecret);
80: }
81:
82: ////////////////////////////////////////////////////////////////////////////
83:
84: /// <summary>
85: ///
86: /// </summary>
87: public List<TwitterStatus> TweetListAboveId(long aboveTweetId)
88: {
89: List<TwitterStatus> tweetList;
90: IEnumerable<TwitterStatus> iEnumerableTweetList;
91:
92: tweetList = new List<TwitterStatus>();
93: iEnumerableTweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
94:
95: foreach (TwitterStatus tweet in iEnumerableTweetList)
96: {
97: if (tweet.Id > aboveTweetId) tweetList.Add(tweet);
98: }
99:
100: return tweetList;
101: }
102:
103: ////////////////////////////////////////////////////////////////////////////
104:
105: /// <summary>
106: ///
107: /// </summary>
108: public List<TwitterDirectMessage> DirectMessagesReceivedListAboveId(long aboveDmId)
109: {
110: List<TwitterDirectMessage> dmList;
111: IEnumerable<TwitterDirectMessage> iEnumerableDmList;
112:
113: dmList = new List<TwitterDirectMessage>();
114: iEnumerableDmList = service.ListDirectMessagesReceived(new ListDirectMessagesReceivedOptions());
115:
116: foreach (TwitterDirectMessage dm in iEnumerableDmList)
117: {
118: if (dm.Id > aboveDmId) dmList.Add(dm);
119: }
120:
121: return dmList;
122: }
123:
124: ////////////////////////////////////////////////////////////////////////////
125:
126: /// <summary>
127: ///
128: /// </summary>
129: public List<TwitterStatus> TweetList()
130: {
131: //
132: List<TwitterStatus> tweetList;
133: IEnumerable<TwitterStatus> iEnumerableTweetList;
134:
135: tweetList = new List<TwitterStatus>();
136: iEnumerableTweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
137:
138: foreach (TwitterStatus tweet in tweetList)
139: {
140: tweetList.Add(tweet);
141: }
142:
143: return tweetList;
144: }
145:
146: ////////////////////////////////////////////////////////////////////////////
147:
148: /// <summary>
149: ///
150: /// </summary>
151: public List<TwitterDirectMessage> DirectMessageList()
152: {
153: //
154: List<TwitterDirectMessage> dmList;
155: IEnumerable<TwitterDirectMessage> iEnumerableTweetList;
156:
157: dmList = new List<TwitterDirectMessage>();
158: iEnumerableTweetList = service.ListDirectMessagesReceived(new ListDirectMessagesReceivedOptions());
159:
160: foreach (TwitterDirectMessage dm in dmList)
161: {
162: dmList.Add(dm);
163: }
164:
165: return dmList;
166: }
167:
168: ////////////////////////////////////////////////////////////////////////////
169:
170: /// <summary>
171: ///
172: /// </summary>
173: public TwitterStatus ReadLastTweet()
174: {
175: // send tweet
176: TwitterStatus ts;
177: IEnumerable<TwitterStatus> tweetList;
178:
179: ts = null;
180: tweetList = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
181:
182: foreach (TwitterStatus tweet in tweetList)
183: {
184: ts = tweet;
185: break; // to read only the first;
186: }
187:
188: return ts;
189: }
190:
191: ////////////////////////////////////////////////////////////////////////////
192:
193: /// <summary>
194: /// Delete Tweet (this function is not working properly)
195: /// <param name="tweetId">Tweet Id to delete</param>
196: /// <see cref="http://stackoverflow.com/questions/4810076/tweetsharp-remove-undo-retweet"/>
197: /// </summary>
198: public TwitterStatus DeleteTweet(long tweetId)
199: {
200: // delete tweet
201: TwitterStatus ts;
202:
203: ts = service.DeleteTweet(new DeleteTweetOptions() { Id = tweetId });
204:
205: return ts;
206: }
207:
208: ////////////////////////////////////////////////////////////////////////////
209:
210: /// <summary>
211: /// Delete DM
212: /// </summary>
213: public TwitterDirectMessage DeleteDirectMessage(long dmId)
214: {
215: // delete tweet
216: TwitterDirectMessage dm;
217:
218: dm = service.DeleteDirectMessage(new DeleteDirectMessageOptions() { Id = dmId });
219:
220: return dm;
221: }
222:
223: ////////////////////////////////////////////////////////////////////////////
224:
225: /// <summary>
226: ///
227: /// </summary>
228: public void SendTweet(string status)
229: {
230: // send tweet
231:
232: SendTweetOptions sto;
233:
234: sto = new SendTweetOptions();
235:
236: sto.Status = status; // "Testing (2)...";
237:
238: service.SendTweet(sto);
239: }
240:
241: ////////////////////////////////////////////////////////////////////////////
242:
243: /// <summary>
244: ///
245: /// </summary>
246: public void SendDirectMessage(string screenName, string text)
247: {
248: // direct message
249:
250: SendDirectMessageOptions sdmo;
251:
252: sdmo = new SendDirectMessageOptions();
253:
254: sdmo.ScreenName = screenName; // "@abcd";
255: sdmo.Text = text; // "Testing (2)...";
256:
257: service.SendDirectMessage(sdmo);
258: }
259:
260: ////////////////////////////////////////////////////////////////////////////
261: ////////////////////////////////////////////////////////////////////////////
262: }
263: }