Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created April 25, 2014 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkAmi/11303226 to your computer and use it in GitHub Desktop.
Save thinkAmi/11303226 to your computer and use it in GitHub Desktop.
LINQToTwitterライブラリで、ツイートしたり、ツイートを取得するサンプル (WPFでTextBoxとButtonを使ってます)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// ToListAsync() メソッド使うのに必要
using LinqToTwitter;
namespace linqappwpf
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var auth = new LinqToTwitter.SingleUserAuthorizer
{
CredentialStore = new LinqToTwitter.SingleUserInMemoryCredentialStore
{
ConsumerKey = "hoge",
ConsumerSecret = "fuga",
AccessToken = "piyo",
AccessTokenSecret = "hogera"
}
};
Tweet(auth);
Timeline(auth);
}
private async void Tweet(LinqToTwitter.SingleUserAuthorizer auth)
{
var context = new LinqToTwitter.TwitterContext(auth);
var results = await context.TweetAsync(TextBox1.Text);
}
private async void Timeline(LinqToTwitter.SingleUserAuthorizer auth)
{
var reg = new System.Text.RegularExpressions.Regex(@"^\[リンゴ\]");
ulong maxID;
var context = new LinqToTwitter.TwitterContext(auth);
var results = new List<LinqToTwitter.Status>();
var firstResponse = await context.Status
.Where(s => s.Type == LinqToTwitter.StatusType.User &&
s.ScreenName == "thinkAmi" &&
s.Count == 200)
.Where(s => reg.IsMatch(s.Text))
.ToListAsync();
maxID = firstResponse.Min(r => r.StatusID) - 1;
results.AddRange(firstResponse);
for (int i = 0; i < 15; i++)
{
var response = await context.Status
.Where(s => s.Type == LinqToTwitter.StatusType.User &&
s.ScreenName == "thinkAmi" &&
s.Count == 200 &&
s.MaxID == maxID)
.Where(s => reg.IsMatch(s.Text))
.ToListAsync();
if (response.Any())
{
maxID = response.Min(r => r.StatusID) - 1;
results.AddRange(response);
}
else
{
break;
}
}
MessageBox.Show(results.Count.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment