Skip to content

Instantly share code, notes, and snippets.

@ustreamer-01647
Last active August 29, 2015 14:07
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 ustreamer-01647/269cb1d32513afd292c8 to your computer and use it in GitHub Desktop.
Save ustreamer-01647/269cb1d32513afd292c8 to your computer and use it in GitHub Desktop.
実習「CoreTweetでストリーミングを処理するためのRx入門」
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="testCoreTweet2.MainWindow"
Title="MainWindow" Width="525" SizeToContent="Height" d:DesignHeight="345">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Account:"/>
<Label x:Name="screennameLabel" Content="unregister" />
<Button x:Name="registButton" Content="regist" Width="75" Click="registButton_Click"/>
</StackPanel>
<TextBox x:Name="viewTextBox" Text="TextBox" Height="225" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" />
<StackPanel Orientation="Horizontal">
<Button x:Name="getTl" Content="getTl" Click="getTl_Click"/>
<Button x:Name="startUserStream" Content="startUserStream" Click="startUserStream_Click" />
<Button x:Name="stopUserStream" Content="stopUserStream" Click="stopUserStream_Click" />
</StackPanel>
</StackPanel>
</Window>
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;
using CoreTweet;
using CoreTweet.Streaming;
using CoreTweet.Streaming.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
namespace testCoreTweet2
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
internal Tokens tokens;
private IDisposable disposable;
public MainWindow()
{
InitializeComponent();
// トークン組立
if (!string.IsNullOrEmpty(Properties.Settings.Default.AccessToken)
&& !string.IsNullOrEmpty(Properties.Settings.Default.AccessTokenSecret))
{
tokens = Tokens.Create(
Properties.Settings.Default.ApiKey
, Properties.Settings.Default.ApiSecret
, Properties.Settings.Default.AccessToken
, Properties.Settings.Default.AccessTokenSecret);
updatescreennameLabel();
/*
* http://01647.hateblo.jp/entry/2014/10/12/132505
* CoreTweet.Tokens.Account.VerifyCredentials()とTwitter OAuth2それぞれの調査記録 2014-10-12
*
* 動的にScreenNameを得る場合のテストコード
*/
// トークン有効性確認
//try
//{
// var userResponse = tokens.Account.VerifyCredentials();
// updatescreennameLabel(userResponse.ScreenName);
// Properties.Settings.Default.ScreenName = userResponse.ScreenName;
// Properties.Settings.Default.Save();
//}
//catch (Exception ex)
//{
// // MessageBox.Show(ex.Message);
// tokens = null;
//}
}
}
/// <summary>
/// スクリーンネーム表示を更新する
/// </summary>
/// <param name="screenName">Twitter Screen Name</param>
/// パラメータ省略時は設定ファイルを読み出す
internal void updatescreennameLabel(string screenName = null)
{
string _screenName;
if (string.IsNullOrEmpty(screenName))
{
_screenName = Properties.Settings.Default.ScreenName;
// 未認証時
if (string.IsNullOrEmpty(_screenName))
{
screennameLabel.Content = "unregister";
return;
}
}
else
{
_screenName = screenName;
}
// http://msdn.microsoft.com/ja-jp/library/system.windows.controls.label(v=vs.110).aspx
// WPF Labelにおける文字 _ の仕様について対策する
screennameLabel.Content = _screenName.Replace("_", "__");
}
// Twitter アカウント認証
private void registButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new RegistAccountWindow();
dialog.Owner = this;
// http://msdn.microsoft.com/ja-jp/library/system.windows.window.showdialog(v=vs.110).aspx
dialog.ShowDialog();
}
private void getTl_Click(object sender, RoutedEventArgs e)
{
if (tokens != null)
{
viewTextBox.Clear();
try
{
foreach (var status in tokens.Statuses.HomeTimeline())
{
// http://qiita.com/lambdalice/items/55b1a3d8403ecc603b47#2-4 例1: REST API
// {ユーザー名}: {投稿内容}{改行}
viewTextBox.AppendText(string.Format("{0}: {1}{2}"
, status.User.ScreenName
, status.Text
, Environment.NewLine));
}
}
catch (Exception ex)
{
viewTextBox.AppendText(ex.Message);
}
}
}
private void startUserStream_Click(object sender, RoutedEventArgs e)
{
viewTextBox.Clear();
try
{
// 1. UserStream を受信してみよう
/*
tokens.Streaming.StartObservableStream(StreamingType.User)
.ObserveOn(SynchronizationContext.Current)
.Subscribe((StreamingMessage m) => viewTextBox.AppendText(string.Format("{0}{1}"
, m, Environment.NewLine)));
*/
// 2. 切断してみよう
/*
disposable = tokens.Streaming.StartObservableStream(StreamingType.User)
.ObserveOn(SynchronizationContext.Current)
.Subscribe((StreamingMessage m) => viewTextBox.AppendText(string.Format("{0}{1}"
, m, Environment.NewLine)));
*/
// 3. エラー処理をする
/*
disposable = tokens.Streaming.StartObservableStream(StreamingType.User)
.ObserveOn(SynchronizationContext.Current)
.Subscribe(
(StreamingMessage m) => viewTextBox.AppendText(string.Format("{0}{1}"
, m, Environment.NewLine)),
(Exception ex) => viewTextBox.AppendText(string.Format("{0}{1}"
, ex, Environment.NewLine)),
() => viewTextBox.AppendText("\"ストリーミングでは 3 つ目の引数が呼ばれてしまったら逆に問題なのですが、 Twitter が何しでかすかわからないので一応処理しておくことをお勧めします。\"")
);
*/
// 4. さぁ LINQ だ
disposable = tokens.Streaming.StartObservableStream(StreamingType.User)
.Where((StreamingMessage m) => m.Type == MessageType.Create)
.Cast<StatusMessage>()
.Select((StatusMessage m) => m.Status.Text)
.ObserveOn(SynchronizationContext.Current)
.Subscribe((string s) => viewTextBox.AppendText(string.Format("{0}{1}"
, s, Environment.NewLine)));
}
catch (Exception ex)
{
viewTextBox.AppendText(ex.Message);
}
}
private void stopUserStream_Click(object sender, RoutedEventArgs e)
{
try
{
// 2. 切断してみよう
disposable.Dispose();
viewTextBox.AppendText("Stopped UserStream." + Environment.NewLine);
}
catch (Exception ex)
{
viewTextBox.AppendText(ex.Message + Environment.NewLine);
}
}
}
}
@ustreamer-01647
Copy link
Author

http://azyobuzin.hatenablog.com/entry/2014/08/26/211529
CoreTweetでストリーミングを処理するためのRx入門 - アジョブジ星通信 2014-08-26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment