Subversion Repositories instagram

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

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