]> git.sesse.net Git - vlc/commitdiff
modules/meta_engine/googleimage.c: Art Fetcher module that gets it's arts from images...
authorPierre d'Herbemont <pdherbemont@videolan.org>
Sun, 12 Aug 2007 21:18:08 +0000 (21:18 +0000)
committerPierre d'Herbemont <pdherbemont@videolan.org>
Sun, 12 Aug 2007 21:18:08 +0000 (21:18 +0000)
configure.ac
modules/meta_engine/Modules.am
modules/meta_engine/googleimage.c [new file with mode: 0644]

index 070948ca64411555103fb2730c43311109ef7f49..c01dd2bb90ffac0e46f91d2fb5de9b2ae85dfff0 100644 (file)
@@ -1209,6 +1209,7 @@ VLC_ADD_PLUGINS([access_filter_bandwidth])
 VLC_ADD_PLUGINS([packetizer_mpegvideo packetizer_h264])
 VLC_ADD_PLUGINS([packetizer_mpeg4video packetizer_mpeg4audio])
 VLC_ADD_PLUGINS([packetizer_vc1])
+VLC_ADD_PLUGINS([googleimage])
 
 
 if test "${SYS}" != "mingwce"; then
index a7fabc8eb3741c8db3de2d5071d6955cfca4bbe3..9e1c9c1892e94f49359c9d078493e966511b2fef 100644 (file)
@@ -1,4 +1,5 @@
 SOURCES_musicbrainz = musicbrainz.c
 SOURCES_folder = folder.c
 SOURCES_taglib = taglib.cpp
+SOURCES_googleimage = googleimage.c
 SOURCES_id3tag = id3tag.c id3genres.h $(NULL)
diff --git a/modules/meta_engine/googleimage.c b/modules/meta_engine/googleimage.c
new file mode 100644 (file)
index 0000000..05c2b3e
--- /dev/null
@@ -0,0 +1,156 @@
+/*****************************************************************************
+ * googleimage.c
+ *****************************************************************************
+ * Copyright (C) 2006 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#include <stdlib.h>                                      /* malloc(), free() */
+
+#include <vlc/vlc.h>
+#include <vlc_input.h>
+#include <vlc_playlist.h>
+#include <vlc_meta.h>
+#include <vlc_stream.h>
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int FindArt( vlc_object_t * );
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+
+vlc_module_begin();
+    set_shortname( N_( "GoogleImage" ) );
+    set_description( _("Fetch Artwork from Google Images") );
+
+    /* Go to image.google.com and download the first image closest to the
+     * title */
+    set_capability( "art finder", 30 );
+    set_callbacks( FindArt, NULL );
+vlc_module_end();
+
+/*****************************************************************************
+ *****************************************************************************/
+
+static const char * kpsz_google_image_server = "images.google.com";
+static const char * kpsz_first_image_token = "imgurl=";
+
+static int FindArt( vlc_object_t *p_this )
+{
+    playlist_t *p_playlist = (playlist_t *)p_this;
+    char *request, *fname, *psz;
+    stream_t *p_stream;
+       unsigned int count;
+       unsigned int i;
+    input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
+    char psz_buffer[65536]; /* XXX: We might want to lower that */
+    assert( p_item );
+
+    if( !p_item->p_meta || !p_item->p_meta->psz_title )
+    {
+               if( !p_item->psz_name )
+               {
+                       msg_Dbg( p_this, "item has no name" );
+               return VLC_EGENERIC;
+               }
+               fname = strdup( p_item->psz_name );
+    }
+    else
+    {
+        fname = strdup( p_item->p_meta->psz_title );
+    }
+
+       /* If there is a .xxx remove it, we get bad matches with it */
+       count = strlen( fname );
+       if( count > 4 && fname[count-4] == '.' )
+               fname[count-4] = 0;
+
+    /* Replace some special char by + */
+    for( i = 0; i < strlen( fname ); i++)
+    {
+        if( fname[i] == ' ' || fname[i] == '-' || fname[i] == '.' )
+            fname[i] = '+';
+    }
+
+    asprintf( &request, "http://%s/images?q=%s&ie=UTF-8", kpsz_google_image_server, fname );
+       free( fname );
+
+    msg_Dbg( p_this, "going to request %s", request );
+    
+    /* Sending the request */
+    p_stream = stream_UrlNew( p_playlist, request );
+       free( request );
+    if( !p_stream )
+    {
+               msg_Err( p_this, "Can't connect" );
+               return VLC_EGENERIC;
+       }
+
+       /* The access will return the whole page at once, we just abuse from that */
+       count = stream_Read( p_stream , (uint8_t*)psz_buffer, sizeof(psz_buffer)-1 );
+       if( count <= 0 )
+       {
+               msg_Warn( p_this, "Can't read the page" );
+               stream_Delete( p_stream );                      
+               return VLC_EGENERIC;
+       }
+
+       psz_buffer[count] = 0;
+
+       /* Find the first occurences of kpsz_first_image_token */
+       if( !(psz = strstr( psz_buffer, kpsz_first_image_token )) )
+       {
+               msg_Warn( p_this, "No image found" );
+               stream_Delete( p_stream );                      
+               return VLC_EGENERIC;
+       }
+
+       /* Found */
+
+       psz += 7;
+
+       /* Now get the end of the link */
+       fname = psz;
+       psz = strchr( fname, '&' );
+       if( !psz )
+       {
+               msg_Dbg( p_this, "Invalid page (or chunk) (!)" );
+               stream_Delete( p_stream );
+               return VLC_EGENERIC;
+       }
+       *psz = 0;
+       
+       /* fname points to the url */
+
+       stream_Delete( p_stream );
+
+       /* We have the link */
+
+       msg_Dbg( p_this, "Image is at '%s'\n", fname );
+       vlc_meta_SetArtURL( p_item->p_meta, fname );
+       return VLC_SUCCESS;
+}
+