Subversion Repositories instagram

[/] [cronjobs/] [importImagesFromRemote.php] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ffruehstueck
<?php
2
 
3
$cli->output( 'Instagram Photosync started');
4
 
5
// from URL:
6
// http://share.ez.no/blogs/sebastiaan-van-der-vliet/importing-image-objects-from-a-rss-feed
7
//call this script as follows:
8
//php extension/[name of your extension]/scripts/importrssimages.php --node=43 --feed="http://api.flickr.com/services/feeds/photos_public.gne?id=11398192@N02&lang=en-us&format=rss_200"
9
 
10
//You could put the import user id in a config file
11
 
12
$IGIni = eZINI::instance( 'instagram.ini' );
13
 
14
$importUserID =  $IGIni->variable( 'ImportSettings', 'ImportUserID' );
15
$user = eZUser::fetch( $importUserID );
16
 
17
$db = eZDB::instance();
18
 
19
$importNodeID = $IGIni->variable( 'ImportSettings', 'ImportNodeID' );
20
 
21
 
22
if ( $importNodeID )
23
{
24
    $folderID = $importNodeID;
25
    $folderNode = eZContentObjectTreeNode::fetch( $folderID );
26
 
27
    if (!$folderNode)
28
    {
29
        $cli->error( "Folder with ID ".$folderID." does not exist." );
30
    }
31
}
32
else
33
{
34
     $cli->error( "Specify a folder NodeID where the image objects should be stored." );
35
}
36
 
37
// fetch all images via IG api and store them to the database
38
eZIGFunctionCollection::fetchAllImages( );
39
 
40
$allConnectedUsers = eZIG::fetchAll();
41
$userParentFolder = array();
42
foreach( $allConnectedUsers as $connectedUser )
43
{
44
        $params = array();
45
        $params['class_identifier'] = 'folder';
46
        $params['creator_id'] = $user->attribute( 'contentobject_id' );
47
        $params['parent_node_id'] = $folderID;
48
        $attributesData = array();  
49
        $attributesData['name'] = ( trim( $connectedUser->attribute('ig_username') ) != "" ) ? $connectedUser->attribute('ig_username') : $connectedUser->attribute('ig_id');
50
        $params['attributes'] = $attributesData;
51
        $params['remote_id'] = "IG_USER_FOLDER_".$connectedUser->attribute('ig_id');
52
 
53
        $contentObject = eZContentObject::fetchByRemoteID( $params['remote_id'] );
54
 
55
        if( $contentObject instanceof eZContentObject )
56
        {
57
                eZContentFunctions::updateAndPublishObject( $contentObject, $params );
58
                $userFolder = $contentObject;
59
        }
60
        else
61
        {      
62
                $userFolder = eZContentFunctions::createAndPublishObject( $params );
63
        }
64
 
65
        $userParentFolder[$connectedUser->attribute('ig_id')] = $userFolder->attribute('main_node_id');
66
}
67
// print_R($userParentFolder);
68
$allImageArray = eZIGPhotos::fetchAllUnimported();
69
 
70
//Looping through all the collected images.
71
$limit = 0;
72
foreach( $allImageArray as $image )
73
{
74
        $tmp = unserialize( $image->images );
75
        $item['image'] = $tmp['standard_resolution']['url'];
76
 
77
        //strip GET stuff.
78
    $item['image']=preg_replace('/\\?.*/', '', $item['image']);
79
 
80
    if ( $item['image'] )
81
    {
82
        //this part is mostly based on http://share.ez.no/learn/ez-publish/creating-ez-publish-objects-in-php/
83
        $params = array();
84
        $params['class_identifier'] = 'image';
85
        $params['creator_id'] = $user->attribute( 'contentobject_id' );
86
                $params['parent_node_id'] = ( $userParentFolder[ $image->attribute('user_id') ] > 0 ) ? $userParentFolder[ $image->attribute('user_id') ] : $folderID;
87
 
88
                //things might be better if the function eZImageAliasHandler::initializeFromFile would use
89
        //if ( !@fopen($f ilename,"r") ) instead of if ( !file_exists( $filename ) )
90
        //so image files could be retrieved across servers. Now we need to store the files locally twice.
91
 
92
        $sys = eZSys::instance();
93
        $storage_dir = $sys->storageDirectory();
94
        $tempDir = $storage_dir . '/instagram/orginal/'.$folderID.'/';
95
        $imageName = basename( $item['image'] );
96
        if ( file_exists( $tempDir.$imageName) )
97
        {
98
            $cli->notice("Item already exists");
99
            continue;
100
        }
101
 
102
        if ( @fopen($item['image'],"r") )
103
        {
104
            $buf = eZHTTPTool::sendHTTPRequest( $item['image'], 80, false, 'eZ Publish', false );
105
            $header = false;
106
            $body = false;
107
            if ( eZHTTPTool::parseHTTPResponse( $buf, $header, $body ) )
108
            {
109
                eZFile::create( $imageName, $tempDir, $body );
110
            }
111
            $sourceMimeData = eZMimeType::findByFileContents( $imageName );
112
        }
113
        $params ['storage_dir' ] = $tempDir;    
114
        $item['image'] = $imageName;
115
        //extra code ends here
116
 
117
        $attributesData = array();
118
        $attributesData['name'] = ( trim( $image->attribute('caption') ) != "" ) ? $image->attribute('caption') : $image->attribute('id');
119
        $attributesData['image'] = $sourceMimeData['filename'];
120
 
121
        $XMLContent = strip_tags('');
122
        $parser = new eZSimplifiedXMLInputParser( false, true, eZXMLInputParser::ERROR_ALL, true );
123
        $parser->setParseLineBreaks( true );
124
        $document = $parser->process( $XMLContent );
125
        $xmlString = eZXMLTextType::domString( $document );
126
        $attributesData['caption'] = $xmlString;
127
 
128
        $params['attributes'] = $attributesData;
129
 
130
 
131
                $params['remote_id'] = "IG_IMAGE_".$image->id;
132
 
133
                $imageObject = eZContentObject::fetchByRemoteID( $params['remote_id'] );
134
 
135
                if( $imageObject instanceof eZContentObject )
136
                {
137
                        eZContentFunctions::updateAndPublishObject( $imageObject, $params );
138
                }
139
                else
140
                {      
141
                        $imageObject = eZContentFunctions::createAndPublishObject( $params );
142
                }
143
 
144
        if ($imageObject)
145
        {
146
 
147
            $cli->notice("Created image IG ID: ".$image->id ." -> ContentObjectID: ".$imageObject->attribute('id') );
148
            $image->setAttribute('contentobject_id', $imageObject->attribute('id') );
149
            $image->store();
150
        }
151
    }
152
        if( $limit > 100 )
153
                break;
154
 
155
    $limit++;
156
 
157
}
158
 
159
$cli->notice("Instagram Photosync is done.");
160
 
161
?>