SlideShare a Scribd company logo
1 of 15
Twitter API はツイートを(一部)
エスケープして来る
ツイートの取得
私は UV で Twitter キャンペーンに関わる仕事をよくしています。
#〇〇 というハッシュタグをつけてツイートしてね! とかいうあれ。
また時折、ハッシュタグをつけたツイートの内
XX と一緒にツイートした人にだけ特別な返信をしたい。
あるいは、YY とツイートした人には返信しない。
という需要があったりします。
取得したツイートのテキストチェック
そんな時は、#〇〇 というハッシュタグを含んだツイートを全て取得して
取得した後に、XXやYYといったテキストを含んでいないかチェックします。
ここで面倒な仕様に巻き込まれました。
実際のコードではないがテスト用にこんなかんじ、(Ruby)
require 'oauth'
consumer = OAuth::Consumer.new(
"YOUR CONSUMER KEY",
"YOUR CONSUMER SECRET",
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(
consumer,
"YOUR ACCESS TOKEN",
"YOUR ACCESS SECRET"
)
endpoint はさっき作ったやつ
require 'json'
search_url = 'https://api.twitter.com/1.1/search/tweets.json'
search_res = endpoint.get(
search_url + '?' +
URI.encode_www_form([
["q","from:スクリーン名 #20201124_test_tweet"]
])
)
# 最初の1件のツイート内容
p JSON.parse(search_res.body, {:symbolize_names => true})[:statuses].first[:text]
# => "#20201124_test_tweet テスト"
なんか変
これだけなら普通。
当時、ツイート内容に「&」が含まれる場合、それが大文字でも小文字でもマッ
チングする様にしようとしていました。
API にはハッシュタグをパラメーターとして与えて、回収したツイートのテキス
トに正規表現を当てる。
text.match?(/XX & YY/)
これがさっぱりマッチしない。
実体参照っぽい
さっきのサンプルコードは「#20201124_test_tweet テスト」とツイートした直
後のもの。
今度は「#20201124_test_tweet black&white」とツイートして試してみます。
「black&white」ですって。
実体参照やないか。
p JSON.parse(search_res.body, {:symbolize_names => true})[:statuses].first[:text]
# => "#20201124_test_tweet black&white"
実体参照
HTMLなどで直接記述できない文字や記号を表記する際に用いられる方法。
そのままHTMLに突っ込むと HTML タグを生成されたりするのでこんな形にエ
スケープする。
require "cgi"
p CGI.escapeHTML('"<black & white>"')
# => "&quot;&lt;black &amp; white&gt;&quot;"
対象記号は?
とりあえず ascii 記号を放り込んでみる。
=> !"#$%&'()*+,-./:;<=>?@[¥]^_`{|}~
目に留まるのは「&amp;」「&lt;」「&gt;」の3つ。
puts JSON.parse(search_res.body, {:symbolize_names => true})[:statuses].first[:text]
# => #20201124_test_tweet !"#$%&amp;'()*+,-./:;&lt;=&gt;?@[¥]^_`{|}~
対象記号は?
最初は HTML エスケープしてるのかな?と思ったけど
XML の CharData として処理してると考えるのが1番それっぽい。
require "cgi"
str = "!¥"#$%&'()*+,-./:;<=>?@[¥¥]^_`{|}~"
puts CGI.escapeHTML(str)
# => !&quot;#$%&amp;&#39;()*+,-./:;&lt;=&gt;?@[¥]^_`{|}~
puts str.encode(xml: :attr)
# => "!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[¥]^_`{|}~"
puts str.encode(xml: :text)
# => !"#$%&amp;'()*+,-./:;&lt;=&gt;?@[¥]^_`{|}~
検索ではどうなる?
どちらでも同じツイートが取得できた。
・・・?
URI.encode_www_form([
["q","from:スクリーン名 #20201124_test_tweet black&white"]
])
URI.encode_www_form([
["q","from:スクリーン名 #20201124_test_tweet black&amp;white"]
])
V2 ではどうなっている?
> We’re building a new Twitter API with a modern and more sustainable
foundation as well as an improved developer experience.
とのことなので少しは期待をして試してみよう。
特定のツイートを回収できそうなのは Tweet lookup かな?
詳しい使い方は省略、さっきと同じ様に認証情報4つ組で呼び出してみる。
ダメでした。
url = "https://api.twitter.com/2/tweets"
res = endpoint.get(
"https://api.twitter.com/2/tweets?ids=1331104166027018241"
)
puts JSON.parse(res.body, {:symbolize_names => true})[:data].first[:text]
# => #20201124_test_tweet black&amp;white
ちなみに
この様な現象は私が確認した限りでは 2007 年ごろからネット上の一部で話題に
上がっている。
いつまで経っても状況が変化しないということは、
1, Twitter としては問題を感じていない
2,問題と思っていても対処できない
(すでにDBにこの形で保存してしまっていて過去のツイート全てに対処するのが
困難など)
のどちらかではないかと思っていた。
ただ、古い記事では (記事1, 記事2, 記事3)
>「<」と「>」のみエスケープされていて、「&」と「"」はされていない
と書かれており、今回の結果と食い違う
2016 年ごろの記事では (記事1)
>「&」と「<」と「>」(全て半角)がエスケープされている
と記載されており、今回の結果に適う。
=> 実は状況は変化している・・・?
私は諦めて「&」「<」「>」は扱わないことにした。

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Twitter api (partially)_escapes_tweets