How to use Twitter Search API for beginners

Photograph by Brett Jordan on Unsplash

Fb, Instagram, and Twitter are thought-about high social media platforms. In response to BusinessofApps, Fb, Instagram, and Twitter have 2,700, 1,160, and 330 million estimated lively customers each month respectively. By taking a look at these numbers we are able to say that social media has turn out to be one of many largest sources of information.

Although Fb and Instagram have extra lively customers, Twitter stays the preferred platform for tutorial researchers and builders. The principle cause for this may very well be the provision and insights of information offered by Twitter.

As of 2018, based on Oberlo, 500 million tweets are despatched every day. That equates to five,787 tweets per second. At first, Twitter was used as only a textual platform however with rising recognition, additionally it is getting used for sharing pictures and movies. Aside from sharing their ideas and concepts, many individuals use Twitter as a tutorial and neighborhood platform.

Earlier than utilizing Twitter API, we have to arrange a developer account and must create a challenge to get API keys. When you shouldn’t have a Twitter account you’ll want to create one after which go to Twitter Developers Dashboard and sign up. You will notice the dashboard as proven within the picture under:

Click on on the Create Undertaking button; it’ll take you to the display under. Title your challenge. You can provide it any title, nevertheless it must be distinctive. I named by challenge MyBlogProject.

After that, choose the rationale for this challenge and write down some descriptions concerning the challenge. (I suppose, it isn’t obligatory, however they’re asking these anyway.)

In the long run, you simply want to offer the title of the applying. Once more, it doesn’t matter, nevertheless it must be distinctive. When you press full, you’re going to get your API keys as proven within the picture under:

You’ll be able to copy these keys now or you will get them afterward as effectively.

Don’t share your keys with anybody.

Now, in the event you click on on “Tasks and Apps” after which overview from the left aspect panel, you will notice all of your tasks and standalone apps. You should use each or any one of many “Undertaking” or “Standalone Apps.”

For this tutorial, we are going to use the app that we created inside Undertaking. You’ll be able to see I’ve each apps contained in the challenge and a standalone app. Earlier, there was just one sort of Twitter API, which was V1.1, however on the time I used to be writing a weblog when the brand new V2 was within the early entry stage. So, for the challenge, I’ve entry to each variations, whereas, for the standalone app, I had entry to solely V1.1.

Each endpoint in V1.1 API begins with api.twitter.com/1.1/search/tweets.json. We are able to use completely different question parameters as per our wants. On this tutorial, we’re utilizing Customary API which places some restrictions on utilization. As per official documentation, there are a lot of sorts of question parameters accessible that are:

  1. q
  2. geocode
  3. lang
  4. result_type
  5. rely
  6. include_entities
  7. till
  8. since_id and max_id
  9. locale (solely ja is at the moment efficient, so we are going to keep away from this one.)
  10. tweet_mode (this has not been documented in official docs.)

From the above-mentioned parameters, solely q is a required question parameter, and all others are optionally available parameters.

Question parameter, q, is used to look particular phrases, hashtags, and customers. For example, we are able to use #python to get all tweets that comprise #python or we are able to use "internet improvement" as a single phrase to retrieve all tweets which have “internet improvement” in it. Furthermore, through the use of @elonmusk, we are able to get all tweets and retweets of the person, Elon Musk.

Notice: Customary API will get you solely information from the final seven days, and if you don’t use rely, it’ll return solely 15 information.

  1. Instance of fetching tweets with hashtag: python
    https://api.twitter.com/1.1/search/tweets.json?q=%23python
  2. Instance of fetching tweets that comprise: “internet improvement” https://api.twitter.com/1.1/search/tweets.json?q="internet improvement"
  3. Instance of fetching tweets with username: elonmusk https://api.twitter.com/1.1/search/tweets.json?q=@elonmusk

Right here, within the first instance, you’ll be able to see now we have used %23 as an alternative of the image #. We have to use such illustration as we cannot use some symbols immediately in our URI. By default, Twitter API offers truncated tweet information. So, to get the complete tweets, now we have to cross the tweet_mode=prolonged question parameter.

As soon as now we have determined what we wish to search, and which hashtag, key phrases, or person to look, we are able to search tweets for particular geolocation utilizing the geocode parameter. This question parameter is optionally available, so, one can use it per your wants.

Now, if I wish to search the key phrase “python round Bangalore”, I must cross the latitude, longitude, and radius. So, I’ll cross the question parameter as geocode=12.97194,77.59369,1mi. Right here, 12.97194,77.59369 and 1mi are latitude, longitude, and radius respectively. The place 1mi means 1 mile. We are able to use km (kilometres) as effectively.

To get tweets which have the python key phrase and are inside a mile radius of Bangalore, one can use the next URI: https://api.twitter.com/1.1/search/tweets.json?q=python&geocode=12.97194,77.59369,1mi

We are able to get tweets with a particular language. For example, we are able to get tweets which have the key phrase “India” and the language is Hindi. To fetch this sort of information, we are able to use the lang question parameter as proven under: https://api.twitter.com/1.1/search/tweets.json?q=India&lang=hi

One can use the next Wikipedia web page to get a listing of all languages and their respective codes.

There are some conditions the place we would like the newest tweets and generally there are conditions the place we would like the preferred tweets. In this sort of state of affairs, we are able to use the question parameter result_type. This question parameter is optionally available simply as geocodeis.

We are able to both use combined, latest, or widespread as result_type the place combined is the default worth.

The rely parameter is used once we desire a particular variety of tweets. By default, we are going to get 15 tweets, however in a single request, we are able to stand up to a most of 100 tweets.

One can get some further information by passing the include_entities question parameter as true.

Utilizing the URI proven under offers further entities discipline information.
https://api.twitter.com/1.1/search/tweets.json?q=%22webpercent20developmentpercent22&include_entities=true&rely=1

Entities Information:

One factor to note right here is, I’ve used %22 as an alternative of double quotes (“) and %20 as an alternative of white area.

To get all tweets created earlier than a particular date, we are able to use till question parameter. The date must be formatted as YYYY-MM-DD. Understand that the search index has a seven-day restrict (for Customary API). In different phrases, no tweets might be discovered for a date older than one week.

After I was penning this weblog it was 2021-03-28, so, I can request information as much as 2021-03-22. If I request the date 2021-03-21, it’ll give me an empty array. One can use the next format to make use of till parameter:
https://api.twitter.com/1.1/search/tweets.json?q=python&till=2021-03-22

Really, in documentation, I couldn’t discover how can we get/generate since_id and max_Id.

As per the documentation, if we use _sinceid, it’ll return outcomes with an ID higher than (that’s, newer than) the desired ID. On different hand, _maxid will return outcomes with an ID lower than (that’s, older than) or equal to the desired ID.

We are able to use boolean operators and grouping mechanisms to get extra particular tweets. We are able to use logical And, OR, and NOT(-) operators. We are able to use _round parenthesis for grouping a number of key phrases and filters.

If we wish to search tweets that comprise python and builders, then we are able to write q=pythonpercent20developer. This may search tweets with each key phrases python and developer. Right here, observe that python and the developer needn’t come collectively.

If we would like them collectively, we are able to write q=%22pythonpercent20developerpercent22 or q="python developer". If we would like tweets with both python or developer then we are able to write q=(python OR developer).

If we wish to ignore tweets with some key phrases, we are able to use hyphen(-). So, to get tweets with the key phrase python or Django and ignore developer, we are able to write a question like q=(python OR Django) -developer.

Notice: We are able to use a number of OR and negation in our question. To make use of a number of negations as an alternative of utilizing -(iPhone OR iMac OR MacBook), use the next: -iPhone -iMac -MacBook.

There might be some uncertainty whereas utilizing a number of operations. For instance:

  • apple OR iPhone iPad could be evaluated as apple OR (iPhone iPad)
  • iPad iPhone OR android could be evaluated as (iPhone iPad) OR android

To get rid of uncertainty and be sure that your guidelines are evaluated as meant, group phrases along with parentheses the place acceptable. For instance:

  • (apple OR iPhone) iPad
  • iPhone (iPad OR android)

As per different on-line sources and official documentation, we are able to filter information by replies, retweets, and primarily based on the account is verified or not as effectively.

Filter Options For Twitter API
Filter Choices For Twitter API

To make use of these filters, we want logical AND and OR operators. It does the identical because the title suggests. To know extra, we are able to undergo some examples.

  1. Get tweets with the key phrase python and exclude retweets: https://api.twitter.com/1.1/search/tweets.json?q=python AND -filter:retweets
    or
    https://api.twitter.com/1.1/search/tweets.json?q=python AND exclude:retweets
  2. Get tweets with the key phrase python and exclude replies: https://api.twitter.com/1.1/search/tweets.json?q=python AND -filter:replies
    or
    https://api.twitter.com/1.1/search/tweets.json?q=python AND exclude:replies
  3. Get tweets with the key phrase python and exclude retweets and replies: https://api.twitter.com/1.1/search/tweets.json?q=python AND -filter:retweets AND -filter:replies
  4. Get tweets with the key phrase “apple iPad” and from a verified account: https://api.twitter.com/1.1/search/tweets.json?q="apple iPad" AND filter:verified

Some extra filers are:

Filter Options For Twitter API
Filter Choices For Twitter API

There are numerous different filters, however it isn’t a good suggestion to say all of them right here. You’ll find all the opposite filters within the official documentation.

Notice: Right here, write username with out @

More Posts