Last active
October 18, 2015 22:53
-
-
Save putzflorian/7a32e18c510511f055bb to your computer and use it in GitHub Desktop.
Instagram Import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // feed | |
| $url = 'https://api.instagram.com/v1/users/{USERID}/media/recent/?access_token={ACCESS_TOKEN}'; | |
| $instagram = new InstagramImport(); | |
| $instagram->fetchURL($url); | |
| class InstagramImport{ | |
| protected $count = 0; | |
| protected $i = 0; | |
| public function fetchURL($url){ | |
| Asset_Service::createFolderByPath('/instagram'); | |
| $feed = Pimcore_Tool::getHttpData($url); | |
| // max pages | |
| $this->count = 10; | |
| if($feed){ | |
| $data = Zend_Json::decode($feed); | |
| $rows = $data['data']; | |
| foreach($rows as $row){ | |
| if($row['type'] == 'image'){ | |
| if(!Asset_Image::getByPath('/instagram/' . Pimcore_File::getValidFilename($row['id'] . '.jpg'))){ | |
| $this->updateImage($row); | |
| } | |
| } | |
| } | |
| if($data['pagination']['next_url'] != ""){ | |
| if($this->count > $this->i){ | |
| echo $this->i . ' --> URL: ' . $data['pagination']['next_url'] . "\n"; | |
| $this->count++; | |
| $this->i++; | |
| $this->fetchURL($data['pagination']['next_url']); | |
| } | |
| } | |
| } | |
| } | |
| private function updateImage($row){ | |
| $img = Pimcore_Tool::getHttpData($row['images']['standard_resolution']['url']); | |
| if($img){ | |
| $temppath = PIMCORE_TEMPORARY_DIRECTORY . '/' . basename($row['images']['standard_resolution']['url']); | |
| file_put_contents($temppath, $img); | |
| $asset = new Asset_Image(); | |
| $asset->setParent(Asset_Folder::getByPath('/instagram')); | |
| $asset->setCreationDate($row['created_time']); | |
| $asset->setModificationDate($row['created_time']); | |
| $asset->setFilename(Pimcore_File::getValidFilename($row['id'] . '.jpg')); | |
| $asset->setData(file_get_contents($temppath)); | |
| $asset->addMetadata("detail", "input", $row['link']); | |
| $asset->save(); | |
| echo 'update Image: ' . Pimcore_File::getValidFilename($row['id'] . '.jpg') . ' ' . "\n"; | |
| unset($temppath); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment