commonwealth-of-puerto-rico / fortaleza

Modules and solutions implemented within the Drupal iteration (yet not limited to Drupal) of the Executive Office of the Governor of Puerto Rico
6 stars 3 forks source link

Twitter API integration #13

Open rfalfaro opened 11 years ago

rfalfaro commented 11 years ago

We've been having issues for some time now with the Twitter JSON integration which is returning an empty result.

http://search.twitter.com/search.json?q=%23EnVivo+from:fortalezapr

This is used in order to identify a live broadcast using the latest tweet sent out from the official @fortalezapr Twitter handle alongside the Ustream API. When a broadcast is originated on Ustream, an automatic notification appears all across the site indicating that there is a live broadcast via the Ustream JSON:

http://api.ustream.tv/json/channel/fortalezapr/getValueOf/status?key=4D1869A6089C22AC737C29A041DE4ACC

Help on an alternative for the Twitter JSON feed will be greatly appreciated.

jpadilla commented 11 years ago

Skimming along the overview for version 1.1 of the Twitter API:

In version 1.1, we're requiring applications to authenticate all of their requests with OAuth 1.0a or Application-only authentication

GET search/tweets confirms that authentication is now required. Might this be it?

rubenvarela commented 11 years ago

As @jpadilla said, this issue is because Twitter disabled their 1.0 API

Opening the link says:

{"errors": [{"message": "The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.", "code": 68}]}

And it's not just the authentication. The way to access the API and work with it changes with version 1.1.

- EDIT

Here is a quick example on using the Twitter API 1.1 in PHP http://www.1stwebdesigner.com/tutorials/twitter-app-oauth-php/

It includes the library to be used and the example code.

On https://dev.twitter.com/docs/twitter-libraries, you can find other Twitter PHP libraries for the 1.1 API.

jonathanagosto commented 11 years ago

Has this issue been resolved yet?

rubenvarela commented 11 years ago

I haven't seen any pull requests or commits. I think it hasn't been resolved yet.

jonathanagosto commented 11 years ago

Okay, let's see if I understand correctly.

The idea is that based on tweet from the official Twitter account (fortalezapr) starting with "En Vivo", a notification of a live Ustream broadcast is to be placed somewhere in the website?

rubenvarela commented 11 years ago

Okay, I'll explain it with the code

First, connect to ustream and get the output of the status:

    $includeFile = file_get_contents('http://api.ustream.tv/json/channel/fortalezapr/getValueOf/status?key=4D1869A6089C22AC737C29A041DE4ACC');
    $json_output = json_decode($includeFile);

The $json_output object is this:

stdClass Object
(
    [results] => offline
    [msg] =>
    [error] =>
    [processTime] => 1
    [version] => mashery-r10
)

So, get the result and store it in $live

    $live = $json_output->results;

Now, connect to Twitter and get the latest tweet: (this is the broken part...)

    $twitter_includeFile = file_get_contents('http://search.twitter.com/search.json?q=%23EnVivo+from:fortalezapr');
    $twitter_json_output = (array) json_decode($twitter_includeFile,true);
    $twitter_result = $twitter_json_output['results'];
    $text = $twitter_result['0']['text'];

If the result from Ustream is live

if($live == "live") 

Then it shows:

<div style="margin-bottom:-20px;">
    <table width="100%" border="0" align="center" cellpadding="2" cellspacing="0">
              <tr>
                <td width="24" align="left" valign="middle" bgcolor="#333333"><img src="/img/record_icon.png" width="24" height="24" alt="En Directo" /></td>
                <td align="right" valign="middle" bgcolor="#333333">
                    <div align="left" style="width:180px; float:left;">
                        <img src="/img/endirecto_text.png" width="180" height="24" />
                    </div>
                    <div class="live_text_link">
                        <a href="/envivo" class="live_text_link"><?php echo($text); ?></a>
                     </div>
                 </td>
              </tr>
        </table>
    <br/><br/>
</div>

So, to recap. If the show from Ustream is live, it should connect to Twitter to get the latest tweet from the @fortalezapr and print it.

That's not the exact logic right now... Right now, it gets the value from Ustream, then gets the value from Twitter, then it checks if it should show the tweet...

You can check lines 26-44 on drupal/templates/page.tpl.php.

--EDIT--

Based on this, you'd authenticate with OAuth, then use a url like,

https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=fortalezapr&count=1

Here is the documentation: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

\ -- EDIT 2-- **

If you're planning on implementing this, some reading: https://github.com/J7mbo/twitter-api-php#how-to-use https://github.com/J7mbo/twitter-api-php/wiki/Twitter-API-PHP-Wiki#get-a-users-tweets---documentation-link

jonathanagosto commented 11 years ago

To my knowledge, there is no module that supports such a feature. However, if I have enough time this weekend I'll try to make a quick module for this issue. It's important to try to keep the theming files as clean as possible. It is better to use the template.php instead of the page.tpl / node.tpl

rubenvarela commented 11 years ago

No module does this. This would be a custom feature.

The site is... not up to Drupal standards. I agree that the template.php would be a better place to implement the functionality. The .tpl.php files are more to simply structure and call different functions.

Let me know if you do it

Now, there is an Oauth module, if you want to check it out, rupal.org/project/oauth I think they rewrite most of it, but it used to have good documentation...

rubenvarela commented 11 years ago

@rfalfaro Here's a module that implements this.

https://github.com/rubenvarela/drupal-tweet_if_streaming

If you have any questions let me know.