Grab Profile Pictures by user list

Hey guys.

I’ve got a big problem with getting twitch profile pictures by a big user list.

What I have is:

  • A Textarea, where the user can put in a list of twitch usernames

  • When he or she submits, an ajax function sends this list to the following php script:

    <?php if(isset($_POST["userlist"])){ $users = explode("\n", str_replace("\r", "", str_replace(" ","",strtolower($_POST["userlist"])))); $package = ""; for($i = 0;$i<10;$i++){ shuffle($users); foreach($users as $user){ $avatar = 'avatars/'.$user.'.jpeg'; if (!file_exists($avatar)) { $hue = file_get_contents("https://api.twitch.tv/kraken/channels/$user"); $hue = json_decode($hue); if($hue->status != 404){ if($hue->logo != null){ copy($hue->logo, 'avatars/'.$user.'.jpeg'); $package .= '
    '.$user.'
    '; }else{ $package .= '
    '.$user.'
    '; } } }else{ $package .= '
    '.$user.'
    '; } } // End Loop Users } // End Loop Times echo $package; } ?>

This script checks if the avatar on the system, if not, then copy.
(just doin it by this way cuz of the long wait time.)

The big deal is: If the list is bigger than about 10 Users, twitch will timeout the script and there are no returns.

The question is: how can I create a more efficient way to do this???
Is there any way to display the profile pictures to the users faster?

I don’t believe Twitch is timing you out here. I ran a script that looped through 20 Twitch users and pulled their public channel data just as you do here, and I had no issues. It took 20.17 seconds to make and receive the 20 requests. Your timeout is likely coming from other sources.

I’m not sure if there is a better API call for batching requests like this, so my suggestion would be to continue to use AJAX to handle all of this, but don’t batch the entire list of API calls before rendering individual user requests. In other words, after each API call, render your new HTML element before moving on to the next iteration in your loop. You may need to push the for loop into your JavaScript handler to best accomplish this.

So you are downloading the images from Twitch and storing them on your database?

If I’m displaying profile images usually what ill do is create an image tag and set the source directly to the image on Twitch servers.

Also, if I’m not mistaken, you can’t hold a Twitch users data on your database for more than 24 hours, in this case their profile image.

Source: http://www.twitch.tv/user/legal?page=api_terms_of_service (See 12.c)

Hope this helps!

Kakeiso

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.