CoreTweetの検索をインジェクションで
.NETのTwitterライブラリにCoreTweetってのがあります。TwitterAPIのラッパーです。
最近、コンテンツ追加にCoreTweetを使ったので簡単な検索方法を説明します。以下のような感じで「続きを読む」を押すと過去5件でツイートが表示されます。
.NET Coreで実装したのでSeviceに登録してインジェクションするやり方です。
ダウンロード
dotnet add package CoreTweet --version 1.0.0.483
大枠を作成
// class public class Twitter : ITwitter { private Tokens _tokens; public Twitter (string consumerKey, string consumerSecret, string accessToken, string accessSecret) { _tokens = Tokens.Create (consumerKey, consumerSecret, accessToken, accessSecret); } } // interface public interface ITwitter { }
consumerKey、consumerSecret、accessToken、accessSecretはhttps://apps.twitter.com/から取得してください。interfaceは後から追加するので今は枠組みだけです。
Startup.csに登録
services.AddSingleton<ITwitter> (_ => new Twitter (consumerKey, consumerSecret, accessToken, accessSecret)); }
これでインジェクションできる準備ができました。今回はシングルトンで問題ないのでシングルトンにしました。
検索メソッドを追加
public class Twitter : ITwitter { private Tokens _tokens; public Twitter (string consumerKey, string consumerSecret, string accessToken, string accessSecret) { _tokens = Tokens.Create (consumerKey, consumerSecret, accessToken, accessSecret); } // これ追加 public async Task<SearchResult> SearchTweets (params Expression<Func<string, object>>[] parameters) { return await _tokens.Search.TweetsAsync (parameters); } } // interface public interface ITwitter { Task<SearchResult> SearchTweets (params Expression<Func<string, object>>[] parameters); }
Twitterは結構いろんなところで使いそうなのでinterfaceだけ定義してインジェクションして使えるようにしました。引数はExpression
呼び出し側
public class TwitterRepository : ITwitterRepository { private ITwitter _twitter; public TwitterRepository (Twitter twitter) { _twitter = twitter; } public async Task SearchTweets (string keyword, long? maxId = null) { var tweets = await _twitter.SearchTweets (count => 100, q => keyword, max_id => maxId, lang => "ja", exclude => "retweets", tweet_mode => "extended"); } }
コンストラクタでインジェクションしてSearchTweetsで使ってます。オプションがいくつかあるので以下で説明します。以下のURLに全部乗ってます。
API reference index — Twitter Developers
max_id:twitterIdを指定するとtwitterIdより過去の記事を取得します。
lang:言語を指定します。jaは日本語です。
exclude:リツイートを除外します。僕は不要なんで必要なかったです。
tweet_mode:デフォルトだと140文字を取得できないので"extended"を指定して全部取れるようにします。
そのほか
これらはオプションであるかもしれません。
でかい画像に変換
ser.ProfileImageUrlで画像を取れるのですが、「normal.jpg」だと画像が小さいので「bigger.jpg」に置換しています。
User.ProfileImageUrl.Replace("normal.jpg", "bigger.jpg")
メタを置換
&は微妙なので
FullText.Replace("&", "&")
本文にキーワードが含まれているものを抽出
Where(x => x.FullText.ToUpper().Contains(keyword.ToUpper())).
結構簡単に使えるので良いですね。次は.NET Coreでの認証を書きます。
ASP.NET Coreのinclude属性とexclude属性
ASP.NET Coreのタグヘルパーでinclude、exclude属性ってのがあります。これは、実行環境毎に定義するタグを切り替えられるもので開発、ステージング、本番など環境に応じて変更出来ます。僕の場合、BuildBundlerMinifierを使っているのでcssがミニファイされて開発時にいちいちビルドしなければいけなかったのですが、これのおかげで開発用のcss、本番用のcssと切り替えられるようになりました。今回は、本番でミニファイされたCSS、開発で生CSSを使うように説明します。
include属性
開発の場合
<environment include="Development"> <link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/> </environment>
ステージング、本番の場合
<environment include="Staging,Production"> <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true"> </environment>
ちなみにasp-append-version="true"は「v=Q55BkiaTp6uRl9...」をつけてくれるのでキャッシュ対策です。
BuildBundlerMinifier
BuildBundlerMinifierはこれです。Microsoft サポートが絡んでいるので信頼があります。BundlerMinifier.Coreってのがあるのですがこれは公式サポートがないので使う人は自己責任って感じですかね。
www.nuget.org
こんな感じでミニファイするファイルを指定するのですが、これがいちいちビルドしなければいけなかったのがめんどかったのでよかった。
マイクロソフトの公式はいまいち読む気がしないのですが、ちゃんと読まなきゃダメですね。
IEnumerable<T>をLINQでマッピング
小ネタ。
IEnumerable
超絶厳しいTwitter APIが承認された
Twitter APIを使いたいので申請したらいろいろとめんどくさかったんで備忘録として
最近の審査は結構厳しくなったみたいなので簡単な気持ちで申請したら思ったよりめんどくさかったんで一連の流れを記載します。ちなみに僕は英語が全く出来ませんが承認されたのでそんなに心配はいらないかと思います。
2018年9月7日 申請
申請したら返信がきました。
Thanks for applying for access!
In order to complete our review of your application, we need additional information about your use case. The most common types of information that can help expedite our review include:
The core use case, intent, or business purpose for your use of the Twitter APIsIf you intend to analyze Tweets, Twitter users, or their content, share details about the analyses you plan to conduct and the methods or techniques
If your use involves Tweeting, Retweeting, or liking content, share how you will interact with Twitter users or their content
If you’ll display Twitter content off of Twitter, explain how and where Tweets and Twitter content will be displayed to users of your product or service, including whether Tweets and Twitter content will be displayed at row level or aggregated
To provide this information to Twitter, reply to this email.
Thank you for your interest in building on Twitter.
ふむふむ(わからん)。
放置。。。
2018年12月20日 返信
とりあえず利用用途を答えろって言ってるっぽいんでそれなりの答えを返信した。
thank you for replying to my email.
On this site, Twitter's comments are posted using keywords such as song title and artist name.
For example, Twitter search with the keyword "Girls Like You" on the following URL, and place comments in real time.
https://mygkrnk.com/music/732
このURLでキーワードを使ってツイート検索するよ!って内容。
ものの30分ぐらいで返信が!
Thank you for your response.
At this time, we still do not have enough specific information about your intended use case to complete review of your application. As a reminder, we previously requested additional information about:
以下略
Twitter「マジで意味わかんねぇからもうちょっと詳しく説明してくんない?」
俺「。。。」
Quitaにいい記事が!
qiita.com
要はQ&A方式で返信すれば良いっぽい!
こんな感じで返信した。
Q1
> The core use case, intent, or business purpose for your use of the Twitter APIs
何に使うか教えろI will search the title of the song on Twitter
タイトル検索するわ
Q2
> If you intend to analyze Tweets, Twitter users, or their content, share details about the analyses you plan to conduct and the methods or techniques
どうやって使うんだ?I use the following API to search the song title by twitter
GET https://api.twitter.com/1.1/search/tweets.json
このAPIを使うわ
Q3
> If your use involves Tweeting, Retweeting, or liking content, share how you will interact with Twitter users or their content.
ツイートとリツイートはどのような感じ※で使うんだ?
I have no plans to use Tweeting, Retweeting
ツイートとリツイートも使わん
※おそらくどんな内容でツイートするか聞いてるっぽい
Q4
> If you’ll display Twitter content off of Twitter, explain how and where Tweets and Twitter content will be displayed to users of your product or service, including whether Tweets and Twitter content will be displayed at row level or aggregated
集計?行レベル?とりあえず具体例教えれI am assuming to use it in the same way as the attached imag
添付した画像みて。
返信から30分後。。。
Your Twitter developer account application has been approved!
意外に簡単!とりあえず、簡潔に簡単に伝えれば良いっぽい。
.NET Coreで最新バージョンのC#を扱う
.Net Coreでは何も指定しないとC#7.0しか使えません。こんなエラーが出ます。
Feature '新機能' is not available in C# 7.0. Please use language version 7.1 or greater.
7.1以上にあげなさい
7.1以上にあげる場合はcsprojに以下を記載します。
<LangVersion>7.1</LangVersion>
常に最新にしたい場合は「Latest」を指定。現在は7.3です。
<LangVersion>Latest</LangVersion>
C#は言語のバージョンによる影響が少ないので「Latest」で十分運用出来ると思います。非同期Main使えんなーと思ったらこれだったのね。
Azure App Serviceでgit submoduleが動くか
GithubからAzure App Serviceにデプロイする場合、ボタンポチポチで出来るのですがAzure App Service on LinuxだとちょっとKuduを弄らなければいけなかったので、同じようにgit submoduleもデプロイ時に取得してきてくれるのか確認してみました(Jenkinsとかちょっと工夫が必要なのでめんどい)。
git submoduleとは
外部の git リポジトリを、自分の git リポジトリのサブディレクトリとして扱える仕組みです。外部のリポジトリのブランチ単位でなく、コミット単位(CommitID)で管理するので初めて利用する人はちょっと手こずりますが理解出来ると結構便利な機能です。ちなみにsvn:externalsと同じように扱うと地獄なので、変更の少ないライブラリに利用するといいかもしれません。
Git - Submodules
git submoduleを用意する
配置したいディレクトリでsubmoduleを追加します。
git submodule add {url}
そしてgithubにpushすると以下のような感じになります。「@」の後ろはコミット番号になりますのでブランチは関係ありません。ちなみにDapperSlackOffは、Dapperの拡張ライブラリ(僕が作った)なので頻繁に変更するものではありません。DapperSlackOffはもうちょい安定したらブログに書きます。
submodule
デプロイする
デプロイ方法は前回書いた記事を参考にしてください。Windowsの場合は、スクリプトとアプリケーション設定がいりません。
tekitoumemo.hatenablog.com
結果
余裕でデプロイ出来た、動作も問題なし。
結論
Azure便利。submoduleは極力使わないが鉄。