مقدمه
این مقاله نحوه واکشی داده های یک هشتگ خاص از توییتر را با استفاده از API TweetSharp در ASP.NET توضیح می دهد.
زمینه
- VS2013
- سی شارپ
قبل از حرکت به جلو، باید با استفاده از مراحل زیر یک برنامه در توییتر ایجاد کنیم:
مرحله 1: وارد توییتر شوید و به https://apps.twitter.com/ بروید و اگر برنامههای خود را دارید میتوانید ببینید، در غیر این صورت روی دکمه ایجاد برنامه جدید کلیک کنید . شما می توانید پنجره مدیریت برنامه را برای ایجاد یک برنامه جدید ببینید. در اینجا اسکرین شات است:
من طبق نیاز خود فرم را پر کرده ام. شما می توانید این را مطابق با سلیقه خود تغییر دهید.
توافق نامه را بپذیرید و روی Create Your Twitter Application کلیک کنید.
مرحله 2: به تب Keys and Access Tokens بروید و Consumer Key (API Key) و Consumer Secret (API Secret) را خواهید دید . در اینجا اسکرین شات است.
مرحله 3: حالا به پایین اسکرول کنید و روی Create my access token کلیک کنید. Access Token و Access Token Secret را خواهید دید .
مرحله 4: من داده ها را در برنامه کنسول نشان می دهم .
- به ویژوال استودیو بروید و یک برنامه کنسول ایجاد کنید.
- روی Manage NuGet Packages کلیک کنید.
- توییت شارپ را جستجو کرده و مطابق تصویر زیر نصب کنید:
مرحله 5: کد زیر را با متد main در کلاس خود اضافه کنید. برای درک بهتر به نظرات مراجعه کنید.
- public static string _consumerKey = "my key"; // Your key
- public static string _consumerSecret = "my key"; // Your key
- public static string _accessToken = "my key"; // Your key
- public static string _accessTokenSecret = "my key"; // Your key
- static void Main(string[] args)
- {
- TwitterService twitterService = new TwitterService(_consumerKey, _consumerSecret);
- twitterService.AuthenticateWith(_accessToken, _accessTokenSecret);
- int tweetcount = 1;
- var tweets_search = twitterService.Search(new SearchOptions { Q = "#ItCanWait", Resulttype = TwitterSearchResultType.Popular });
- //Resulttype can be TwitterSearchResultType.Popular or TwitterSearchResultType.Mixed or TwitterSearchResultType.Recent
- List<TwitterStatus> resultList = new List<TwitterStatus>(tweets_search.Statuses);
- foreach (var tweet in tweets_search.Statuses)
- {
- try
- {
- //tweet.User.ScreenName;
- //tweet.User.Name;
- //tweet.Text; // Tweet text
- //tweet.RetweetCount; //No of retweet on twitter
- //tweet.User.FavouritesCount; //No of Fav mark on twitter
- //tweet.User.ProfileImageUrl; //Profile Image of Tweet
- //tweet.CreatedDate; //For Tweet posted time
- //"https://twitter.com/intent/retweet?tweet_id=" + tweet.Id; //For Retweet
- //"https://twitter.com/intent/tweet?in_reply_to=" + tweet.Id; //For Reply
- //"https://twitter.com/intent/favorite?tweet_id=" + tweet.Id; //For Favorite
- //Above are the things we can also get using TweetSharp.
- Console.WriteLine("Sr.No: " + tweetcount + "\n" + tweet.User.Name + "\n" + tweet.User.ScreenName + "\n" + "https://twitter.com/intent/retweet?tweet_id=" + tweet.Id);
- tweetcount++;
- }
- catch { }
- }
- Console.ReadLine();
- }
حالا کد را اجرا کنید و فقط 15 نتیجه را مشاهده خواهید کرد.
می توانید کد خط 12 را مطابق کد زیر تغییر دهید تا 100 نتیجه بدست آورید.
- var tweets_search = twitterService.Search(new SearchOptions { Q = "#ItCanWait", Resulttype = TwitterSearchResultType.Popular, Count = 100 });