Extract images with a certain tag from Instagram

MultiformeIngegno

  • Posts: 1,337
Re: Extract images with a certain tag from Instagram
« Reply #30, on April 20th, 2012, 06:12 PM »
Ok, another problem.. :P
Instagram created_time timestamp is GMT (I presume.. :P).. I need to get the italian timezone..

So I changed this:
Code: [Select]
'.htmlentities(strftime('%e %B %Y alle %R', $value->caption->created_time)).'

to:
Code: [Select]
'.htmlentities(gmstrftime('%e %B %Y alle %R',time()+3600,$value->caption->created_time)).'

But I get:
Warning: gmstrftime() expects at most 2 parameters, 3 given

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Extract images with a certain tag from Instagram
« Reply #31, on April 20th, 2012, 06:27 PM »
I do get so confused in trying to decipher the mindfuck that is time and date in PHP.

I'm confused why you're putting in time() + 3600. gmstrftime as PHP's manual says wants two parameters - the format you want to display and the time you want to display in that format, putting in time() + 3600 is just going to confuse it (and it complains)

The description seems to indicate that it would actually deal with the offset itself (since setlocale should be providing it), which would suggest simply:
Code: [Select]
'.htmlentities(gmstrftime('%e %B %Y alle %R', $value->caption->created_time)).'

Failing that, the more naive but workable solution (though won't take into account DST)
Code: [Select]
'.htmlentities(strftime('%e %B %Y alle %R', $value->caption->created_time + 3600)).'
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

MultiformeIngegno

  • Posts: 1,337
Re: Extract images with a certain tag from Instagram
« Reply #32, on April 20th, 2012, 06:40 PM »
Second solution worked. :)

To change the "caching time" to ~16 min instead of 1 hour I need to change this:
Code: [Select]
if(file_exists($cache) && filemtime($cache) > time() - 60*60)

to:
Code: [Select]
if(file_exists($cache) && filemtime($cache) > time() - 100*10)

..? 100*10 are seconds, right?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

MultiformeIngegno

  • Posts: 1,337

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Extract images with a certain tag from Instagram
« Reply #35, on April 20th, 2012, 06:56 PM »
Only for readability later on. Using 3600 will be faster than using 60*60, but 60*60 is more readable. Unless we're talking an insanely busy site it doesn't matter (and if we are, there are bigger things to worry about than that anyway)

MultiformeIngegno

  • Posts: 1,337
Re: Extract images with a certain tag from Instagram
« Reply #36, on April 20th, 2012, 08:40 PM »Last edited on April 20th, 2012, 08:48 PM
Do you think that 500 seconds (8 minutes) is a time too short and will impact on the server..?
I don't think it is.. also because the file cached is smaller than 10kb..

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Extract images with a certain tag from Instagram
« Reply #37, on April 20th, 2012, 08:42 PM »
So you're fetching tags from Instagram... how often are items posted from Instagram that fit your tags?

If they're posted every 5 minutes or so, 3-4 minutes (180-240 seconds) is fine, if they're posted every 10 minutes, 8 minutes of cache is fine.

MultiformeIngegno

  • Posts: 1,337
Re: Extract images with a certain tag from Instagram
« Reply #38, on April 20th, 2012, 08:48 PM »
Quote from Arantor on April 20th, 2012, 08:42 PM
So you're fetching tags from Instagram... how often are items posted from Instagram that fit your tags?
Uhm.. not really 5 or 10 minutes..it's a unused tag (#bombacarta) and it's used only by our "organization".. I presume that there will be a new image every.. day? or every 2 days..?

What's a good timing..?

And what if I was extracting photos only from MY recent photos (only photos by the user_id xxx) instead of tags (i.e. there are no other images to "discard", every photo in the provided stream has to be shown)? Would this be better? :)

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Extract images with a certain tag from Instagram
« Reply #39, on April 20th, 2012, 08:57 PM »
If you're talking about one a day or thereabouts, there's really no need to be fetching any more frequently than perhaps once every 12 hours, or if you're really bothered, 1 to 6 hours.

I can't tell you whether using your account vs tags would be better. Which better describes what you want to show?

MultiformeIngegno

  • Posts: 1,337
Re: Extract images with a certain tag from Instagram
« Reply #40, on April 20th, 2012, 09:15 PM »
Quote from Arantor on April 20th, 2012, 08:57 PM
If you're talking about one a day or thereabouts, there's really no need to be fetching any more frequently than perhaps once every 12 hours, or if you're really bothered, 1 to 6 hours.
Uhm, I know but doing so I have to wait 12 hours to see the image on the website after I took it. Right..? I'd like to have max 10/15 min delay... do you think it's not possible?
Quote
I can't tell you whether using your account vs tags would be better. Which better describes what you want to show?
I asked that because I have 2 sites where I'm using this snippet. The first retrieves the images of my user_id stream only, the other retrieves photos with a certain tag (so it has to scan every photo to ensure they have or not my tag, I presume). So I was wondering if the user-stream only snippet is less intrusive for the server because it has to scan a stream only.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Extract images with a certain tag from Instagram
« Reply #41, on April 20th, 2012, 09:31 PM »
Quote
Uhm, I know but doing so I have to wait 12 hours to see the image on the website after I took it. Right..? I'd like to have max 10/15 min delay... do you think it's not possible?
And therein lies the problem. Your code is passive, it's doing lookups to see if there's something new. So if you want it to appear with no more than 10 minutes delay, that's the maximum caching time. But then your server does much, much more work in doing lookups to figure out if there's something new.

If you don't like these options, you have to figure out something different, namely something such that when you perform an upload, you somehow notify your other site.
Quote
So I was wondering if the user-stream only snippet is less intrusive for the server because it has to scan a stream only.
*shrug* I doubt there's much in it.

MultiformeIngegno

  • Posts: 1,337
Re: Extract images with a certain tag from Instagram
« Reply #42, on April 22nd, 2012, 07:55 PM »
Okay, I changed the code so that after 5 entries it displays the images left as thumbnails instead of standard_resolution. I also added a different text for standard_resolution images and thumbnail ($x_data):

Code: [Select]
<?php
setlocale(LC_TIME'it_IT');
function get_instagram($tag=bombacarta,$count=10,$width=612,$height=612){
$url 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
// Also Perhaps you should cache the results as the instagram API is slow
$cache './wp-content/themes/raymond-31/instagram_json/'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 500){
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData json_decode(file_get_contents($cache));
} else {
$jsonData json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
}
$result '<div style="border-top:1px solid #ddd">'.PHP_EOL;
$ic=0;
foreach ($jsonData->data as $key=>$value) {
$location = (!empty($value->location->name))?'presso '.$value->location->name:null;
if($ic>=5){$width=150;$height=150;$res_c=thumbnail;$x_data="thumbnail text";}else{$res_c=standard_resolution;$x_data="standard resolution text";}
$ic++;
$result .= "\t".'<em>'.htmlentities($value->caption->textENT_QUOTES"UTF-8").'</em><br /><img src="'.$value->images->$res_c->url.'" alt="'.htmlentities($value->caption->textENT_QUOTES"UTF-8").'" width="'.$width.'" height="'.$height.'" /><br /><div class="postinfo">'.$x_data.'</div><br />'.PHP_EOL;
}
$result .= '</div>'.PHP_EOL;
return $result;
}
echo get_instagram();
?>


The problem is that instead of "standard resolution text" (added as an example), I'd like to add there something like "Scattata da $value->caption->from->full_name". But I don't know how to "include" the $value->caption->from->full_name stuff without errors..

So I tried with:
Code: [Select]
<?php
setlocale(LC_TIME'it_IT');
function get_instagram($tag=bombacarta,$count=10,$width=612,$height=612){
            
$url 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
            
// Also Perhaps you should cache the results as the instagram API is slow
            
$cache './wp-content/themes/raymond-31/instagram_json/'.sha1($url).'.json';
            if(
file_exists($cache) && filemtime($cache) > time() - 500){
                
// If a cache file exists, and it is newer than 1 hour, use it
                
$jsonData json_decode(file_get_contents($cache));
            } else {
                
$jsonData json_decode((file_get_contents($url)));
                
file_put_contents($cache,json_encode($jsonData));
            }
            
$result '<div style="border-top:1px solid #ddd">'.PHP_EOL;
            
$ic=0;
            foreach (
$jsonData->data as $key=>$value) {
                
$location = (!empty($value->location->name))?'presso '.$value->location->name:null;
if($ic>=5){$res_c=thumbnail_resolution$x_data="thumb text";}else{$res_c=standard_resolution;$x_data=Scattata da $value->caption->from->full_name 'il' htmlentities(gmstrftime('%e %B %Y alle %R'$value->caption->created_time 7200)) htmlentities($location) }
$ic++;
                
$result .= "\t".'<em>'.htmlentities($value->caption->textENT_QUOTES"UTF-8").'</em><br /><img src="'.$value->images->$res_c->url.'" alt="'.htmlentities($value->caption->textENT_QUOTES"UTF-8").'" width="'.$width.'" height="'.$height.'" /><br /><div class="postinfo">'.$x_data.'</div><br />'.PHP_EOL;
            }
            
$result .= '</div>'.PHP_EOL;
            return 
$result;
        }
echo get_instagram();
?>


But I got a Parse error: syntax error, unexpected T_STRING on line 30, which is:
Code: [Select]
if($ic>=5){$res_c=thumbnail_resolution; $x_data="thumb text";}else{$res_c=standard_resolution;$x_data=Scattata da $value->caption->from->full_name 'il' htmlentities(gmstrftime('%e %B %Y alle %R', $value->caption->created_time + 7200)) htmlentities($location) }
Re: Extract images with a certain tag from Instagram
« Reply #43, on April 22nd, 2012, 08:04 PM »
Ok, solved. :)
Code: [Select]
$x_data='Scattata da '.$value->caption->from->full_name.' il '.htmlentities(gmstrftime('%e %B %Y alle %R', $value->caption->created_time + 7200)).' '.htmlentities($location);
Re: Extract images with a certain tag from Instagram
« Reply #44, on May 3rd, 2012, 04:11 PM »
Inexplicable behavior by Internet Explorer. >:( >:( >:(
I have implemented this stuff in 2 websites:
1st: multiformeingegno.it
2nd: bombacarta.com/le-attivita/bombafoto/

Everything works fine in the first website with every browser (including IE), fancybox loads properly and it's ok. In the second website with IE if I click on the thumbnails after the 3 big images, instead of opening Fancybox, IE goes to the thumbnail url!! I can't understand why!! The links are:
<a href="BIG IMAGE URL" class="fancybox" data-fancybox-group="gallery"><img src="THUMB URL" style="margin-right:5px;border:none" alt="" width="150" height="150"/></a>

Everything should work as expected (I have 0 errors on JS console and it is THE SAME FANCYBOX IMPLEMENTATION, SAME FILES AND EVERYTHING)! The thing I can't explain is WHY THE HELL Internet Explorer goes to the thumb URL when the <a> links to the big image!!

Of course everything works properly with ALL other browsers (including Safari Mobile and Android browser)!!!