HIRA THAKUR
HIRA THAKUR

Reputation: 17757

get tweets with certain hashtag at regular intervals?

I have successfully extracted tweets belonging to a certain hashtag.

For people interested in code,Its at the bottom.

This is what I have done:#Dhoom3teaser

But for some reasons I need to get every single tweet inserted in to my table.How do I keep inserting something at regular interval without repitition.(i.e tweet should be inserted according to their timestamp).How do I distinguish tweets?

I thought may be I will use setInterval.But then Thought there could be a better solution.

Hope I am clear?

<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
include_once $_SERVER["DOCUMENT_ROOT"]."/includes/db/db_conn.php";

$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#Dhoom3Teaser';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();

$decoded = json_decode($response);

echo '<pre>';print_r($decoded);echo '<pre>';

Upvotes: 0

Views: 994

Answers (2)

user2752661
user2752661

Reputation:

Look here: https://dev.twitter.com/docs/platform-objects/tweets

The attribute you want is "id (identifies a tweet uniquely).

Edit Some further ideas: If i had to do it in PHP, i would use cronjobs if possible. For storing the unique_id (and desired other fields from the tweet data) you could just use a mysql table (and use the id as primary key). If you don't have experience in that field, it will be harder as that is imo the easiest way to save a lot of tweets and avoid duplicates.

For the search api part: There is a parameter "since_id" what is basically a greater than filter. So if you saved your last retrieved tweet, you can just use this to continue your search and retrieve tweets newer than the last one saved (so there shouldn't be any duplicates anyway).

Upvotes: 0

MattDiamant
MattDiamant

Reputation: 8771

You want a cron job. Here's an article on how to set it up: http://www.thesitewizard.com/general/set-cron-job.shtml

What a cron job is, is just a standard way to run your script on a schedule. So, once a day, or once an hour, or at 2:15 on the 3rd Saturday of each month, etc. You're going to write the cron job so that it will run your tweet grabbing script, and then to make sure that there are no repeated tweets, you're going to check the table for duplicates before inserting a new tweet. I'm sure (but could be wrong) that each tweet has some sort of tweet id, so make sure that a tweet with that id doesn't exist in the table before inserting it.

Edit: Actually, it would be better to make your table so that each row's ID is UNIQUE. You don't have to write any extra php that way.

Upvotes: 1

Related Questions