How to use the Twitter API in Windows PowerShell to search for Bitcoin related tweets

avatar
(Edited)

Experimentiere gerade mit der Twitter API, um die Auswertung des Splinterlands Quiz zu automatisieren.

Um die Twitter API verwenden zu können, braucht man einen Twitter Account und man muss sich bei Twitter als Developer registrieren.

https://developer.twitter.com/

Das funktioniert relativ einfach, indem man ein Formular ausfüllt, den Realnamen angibt und seine E-Mail-Adresse verifiziert.

Wenn alles gut geht, bekommt man in wenigen Sekunden einen einfachen, beschränkten "Essential Level"-Zugriff mit den entsprechenden Keys.

Mit dem "Essential"-Zugriff kann man Tweets der letzten 7 Tage abrufen, nicht mehr als 25 Requests alle 15 Minuten abschicken und im Monat nicht mehr als eine halbe Million Tweets verarbeiten.

Blockchain-Enthusiasten werden jetzt sofort bemerken, dass der Zugriff nicht permissionless ist, aber immerhin bekommt man einen Zugriff auf die Datenbank der letzten 7 Tage. Das ist schon ziemlich cool.

Mit einem einfachen PowerShell-Kommando kann man dann zum Beispiel die letzten 10 Tweets mit dem Hashtag #bitcoin abfragen.

Für diesen Zugriff braucht man den Twitter API Bearer Authentifizierungs-Token (xxx durch den Token ersetzen):

$headers = @{'Authorization' = 'Bearer xxx'}

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin') -Method Get -Headers $headers

$r.data

Um die nächsten 10 Ergebnisse zu bekommen braucht man das next_token von $r.meta

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin&pagination_token='+$r.meta.next_token) -Method Get -Headers $headers

Voilà! Was sagt ihr dazu? Habt ihr die Twitter API schon mal verwendet?

Using Twitter API with Windows PowerShell to search for Bitcoin related tweets

twitter-api-search-1.jpg

English

I'm currently experimenting with the Twitter API to automate the scoring of the Splinterlands quiz on twitter.

In order to use the Twitter API you need a Twitter account and you have to register as a developer on Twitter:

https://developer.twitter.com/

This works relatively simple, by filling out a form with your real name and verifying your email address.

If all goes well, in a few seconds you will get a simple, limited "Essential Level" access with the corresponding keys.

With "Essential" access, one can retrieve tweets from the last 7 days, send no more than 25 requests every 15 minutes, and process no more than half a million tweets in a month.

Blockchain enthusiasts will now immediately realize that access is not permissionless, but at least you get access to the database of the last 7 days. That's pretty cool in itself.

With a simple PowerShell command, you can then query the last 10 tweets with the hashtag #bitcoin, for example.

For this you need the Twitter API Bearer authentication token (replace xxx with the token):

$headers = @{'Authorization' = 'Bearer xxx'}

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin') -Method Get -Headers $headers

$r.data

To get the next 10 results you need the next_token of $r.meta

$r=Invoke-RestMethod -Uri ('https://api.twitter.com/2/tweets/search/recent?query=%23bitcoin&pagination_token='+$r.meta.next_token) -Method Get -Headers $headers

Voila! Have you used the Twitter API already?

Sources

[1] Twitter API https://developer.twitter.com/en/docs/platform-overview
[2] Twitter API Search Tweets: https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction
[3] Pagination https://developer.twitter.com/en/docs/twitter-api/pagination

Live your Secrets and Hive Prosper 🍯

xx Viki @vikisecrets

Posted with STEMGeeks



14 comments
avatar

Die Frage ist, ob diese Datenbank auch geblockte oder geshadowbannte Tweets bzw. User ausspuckt. Twitter hat ja da etliche Zensurtools.

0
0
0.000
avatar

Gute Frage, das müsste ich einmal testen, wenn ich einen zensurierten Tweet entdecke, man kann ja nur die letzten 7 Tage abfragen. Denke aber, dass Twitter auch die API filtert. Interessant wäre auch, ob man Corona-Warnungen etc per API bekommt. Denke aber eher nicht.

0
0
0.000
avatar

It is a nice ttool to be aware with
!1UP

0
0
0.000
avatar

Could be somewhat useful in filtering out all that toxicity. lol

!discovery 29

0
0
0.000
avatar

haha, and you wont get the usual Corona / content warnings per API I guess 😂

0
0
0.000
avatar

Sehr interessant!

LG Michael

!invest_vote

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

avatar

That's cool! very easy but very powerful!!

0
0
0.000
avatar

7 Tage ist natürlich nicht viel... Habe eine Projektidee. Mit nur 7 Tagen läuft das wohl auf Handarbeit hinaus.

0
0
0.000
avatar

Das mit dem 7-Tage-Limit ist blöd, man kann auch eine Academic Research-Lizenz beantragen, dann könnte man das ganze Archiv durchsuchen, wenn das Projekt rein wissenschaftlicher Natur ist.

0
0
0.000