Kodi Community Forum

Full Version: [Ruby] TV extrafanart grabber
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
*Newer version in post #5*
It Does movies too ;-)


I couldn't find a python addon to fetch extrafanart (might have not looked hard enough) so i wrote an external one in ruby (my language of choice). Mods, I know this isn't python so feel free to move

Description:
Makes an "extrafanart" folder in each of your TV folders, and fills it with ALL the backgrounds from tvdb

Instructions:
sudo gem install fileutils sqlite3
find path to .xbmc/userdata/MyVideosXX.db
make a file called fanartGrabber.rb
paste in the code below
change (line 1) path_to_db="/home/losty/MyVideos34.db" to your path
then run "ruby fanartGrabber.rb"

Code:
path_to_db="/home/losty/MyVideos34.db"

require 'rubygems'
require "fileutils"
require 'sqlite3'
require 'open-uri'

db = SQLite3::Database.new( path_to_db)
db.results_as_hash = true
db.execute( "select c00 as name,c12 as tvdb,strPath as path from tvshow join tvshowlinkpath on tvshowlinkpath.idShow=tvshow.idShow join path on path.idPath= tvshowlinkpath.idPath" ) do |row|

     path=row['path']+"/extrafanart/"
     begin
       Dir.mkdir(path)
     rescue
     end
     workin=true
     i=1

    if not File.exist?(path+"background1.jpg")
      p "Getting art for "+row['name'] +" ("+row['path']+")"
      while workin do
        p "fetching background #"+i.to_s    
         begin
            open(path+"background"+i.to_s+".jpg", 'wb') do |file|
              file << open('http://thetvdb.com/banners/fanart/original/'+row['tvdb']+'-'+i.to_s+'.jpg').read
            end
            i=i+1
         rescue
            FileUtils.rm(path+"background"+i.to_s+".jpg")
            p "Fetched all, moving on..."
            workin=false
         end
      end
    end
end
p "All art fetched :-)"
Could you wrap that in a [code]?
Ran across this post looking for a easier way of setting up extrafanart for TVShows. Since I run Windows I had to find ways to get this to work. A little Google-Fu and got it running right now.

Thought I'd share what I did in case other Windows users are interested in this as well.

First had to get Ruby and install it from
http://rubyinstaller.org/downloads/

Second downloaded and installed the CoreUtils for Windows for the Fileutils the script requires from
Homepage:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
Directlink:
http://gnuwin32.sourceforge.net/downlinks/coreutils.php

Then had to get SQLite3 (both the command line shell and the DLL zip file) from
http://www.sqlite.org/download.html

Followed the instructions from another forum to get Ruby to see the SQLite3
http://www.artima.com/forums/flat.jsp?fo...ead=140416

Quote:Googling "sqlite3 windows" gives you wrong instructions, so here is my attempt to provide some updated and correct instructions. FYI this is how you install SQLite3 to be usable with Ruby on Rails on Windows XP (as of December 2005)...
Install SQLite3 for Windows by downloading the following 2 zip files and unzipping them into /WINDOWS/system32
http://www.sqlite.org/sqlite-shell-win32...070701.zip
http://www.sqlite.org/sqlitedll-3_2_7.zip
If those don't work try the official download site here
gem install sqlite3-ruby (Open a command prompt as a administrator to run this command)
Select option 1 to get the latest win32 driver

Read: Installing SQLite 3 on Windows
Didn't get a prompt to select a option 1, it just said everything was hunky-dory.

Then created a "fanartgrabber.rb" file and posted the contents erolosty gave in the first post. Stuck the file in the \userdata\database directory for XBMC that contained the videoxx.db.

Changed the db_path to my userdata/database/videoxx.db

Note: Used "/" instead of the typical "\" for the directory structure
ie: path_to_db="/XBMC/portable_data/userdata/Database/myvideos54.db" (Will be different for those not running in portable mode)

Opened a command prompt as administrator. Went to the database dir. and ran the fanartgrabber.rb

Now to watch some shows while it runs in the background for a long while heh.

Thanks erolosty for coming up with this. Was looking for a plugin or something that'd do the same, but this works well enough for me Smile
Rygrath Wrote:Ran across this post looking for a easier way of setting up extrafanart for TVShows. Since I run Windows I had to find ways to get this to work. A little Google-Fu and got it running right now.

Thought I'd share what I did in case other Windows users are interested in this as well.

First had to get Ruby and install it from
http://rubyinstaller.org/downloads/

Second downloaded and installed the CoreUtils for Windows for the Fileutils the script requires from
Homepage:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
Directlink:
http://gnuwin32.sourceforge.net/downlinks/coreutils.php

Then had to get SQLite3 (both the command line shell and the DLL zip file) from
http://www.sqlite.org/download.html

Followed the instructions from another forum to get Ruby to see the SQLite3
http://www.artima.com/forums/flat.jsp?fo...ead=140416


Didn't get a prompt to select a option 1, it just said everything was hunky-dory.

Then created a "fanartgrabber.rb" file and posted the contents erolosty gave in the first post. Stuck the file in the \userdata\database directory for XBMC that contained the videoxx.db.

Changed the db_path to my userdata/database/videoxx.db

Note: Used "/" instead of the typical "\" for the directory structure
ie: path_to_db="/XBMC/portable_data/userdata/Database/myvideos54.db" (Will be different for those not running in portable mode)

Opened a command prompt as administrator. Went to the database dir. and ran the fanartgrabber.rb

Now to watch some shows while it runs in the background for a long while heh.

Thanks erolosty for coming up with this. Was looking for a plugin or something that'd do the same, but this works well enough for me Smile
Thank you

Bit of advise:
If your Show has a unicode character in it the script may fail
First of all thanks Rygrath for the windows instructions - I know most of you use it...

I thought noone would ever get any use of it, so glad to know it helped someone at least.

isamu.dragon I've added a utf-8 shebang to the top which should sort your issue.

Also the new version does movie art too :-)

It requires one more gem "ruby-tmdb" to be installed

Code:
# encoding: UTF-8

path_to_db="/home/losty/.xbmc/userdata/Database/MyVideos34.db"

require 'rubygems'
require "fileutils"
require 'sqlite3'
require 'open-uri'
require 'ruby-tmdb'

@saltEarth=false
ARGV.each do|a|
  if a == "--salt-earth"
    p "Salty!"
    @saltEarth=true
  end
end


Tmdb.api_key = "9b939aee0aaafc12a65bf448e4af9543"
Tmdb.default_language = "en"
db = SQLite3::Database.new( path_to_db)
db.results_as_hash = true
db.execute( "select c00 as name,c12 as tvdb,strPath as path from tvshow join tvshowlinkpath on tvshowlinkpath.idShow=tvshow.idShow join path on path.idPath= tvshowlinkpath.idPath" ) do |row|

  path=row['path']+"extrafanart/"
  begin
    Dir.mkdir(path)
  rescue
  end
  workin=true
  i=1

  if not File.exist?(path+"background1.jpg")
    p "Getting art for "+row['name'] +" ("+row['path']+")"
    while workin do
      p "fetching background #"+i.to_s
      begin
        open(path+"background"+i.to_s+".jpg", 'wb') do |file|
          file << open('http://thetvdb.com/banners/fanart/original/'+row['tvdb']+'-'+i.to_s+'.jpg').read
        end
        i=i+1
      rescue
        if i!=1
          FileUtils.rm(path+"background"+i.to_s+".jpg")
        end
        p "Fetched all, moving on..."
      workin=false
      end
    end
  end
end
db.execute( "select c00 as name,c09 as imdb,strPath as path from movie join files on files.idFile=movie.idFile join path on path.idPath= files.idPath" ) do |row|
#p row['name']
#row['path']
  path=row['path']+"extrafanart/"
  begin
    Dir.mkdir(path)
  rescue
  end
  lastId=""
  i=0
  if not ( File.exist?(path+"background0.jpg") || File.exist?(path+"bail"))
    p "Getting art for "+row['name'] +" ("+row['path']+")"
    begin
      movie = TmdbMovie.find(:imdb => row['imdb'], :limit => 1)
      1/movie.backdrops.length
      movie.backdrops.each do |m|
        if m.id!=lastId
          lastId=m.id
          p "fetching background #"+i.to_s
          begin
            open(path+"background"+i.to_s+".jpg", 'wb') do |file|
              file << m.data
            end
            i=i+1
          rescue
            if i!=1
              FileUtils.rm(path+"background"+i.to_s+".jpg")
            end
            p "Fetched all, moving on..."
          end
        end

      end
    rescue
      p "No art for this movie"
      if @saltEarth
        p "Salting the Earth at "+path
        File.open(path+"bail", 'w') {|f| f.write("bail") }
      end
    end
  end
end
p "All art fetched :-)"
erolosty Wrote:First of all thanks Rygrath for the windows instructions - I know most of you use it...

I thought noone would ever get any use of it, so glad to know it helped someone at least.

isamu.dragon I've added a utf-8 shebang to the top which should sort your issue.

Also the new version does [b]movie art too :-)[/b]

It requires one more gem "ruby-tmdb" to be installed
[...]

Movie art! Awesome man, I love this script. Had to get the Ruby DevKit to install the "ruby-tmdb". Here's how I did it for those wanting to do the same.

If you've already followed my instructions above then proceed. If this is your first time, follow my previous instructions first before going on.

First go to http://rubyinstaller.org/downloads/
and download the Development Kit <-Direct Link
Extract that to a easy to remember directory.
Full Installation instructions are here

Summed up version
  • Open a administrative command prompt.
  • cd <DEVKIT_INSTALL_DIR> (Directory you extracted files to)

  • Type "ruby dk.rb init" (no quotes) to generate the config.yml file to be used later in this Step.

  • Edit the generated config.yml file to include installed Rubies not automagically discovered or remove Rubies you do not want to use the DevKit with. The last line should contain the directory you install Ruby into with my previous post. Mine for example reads
    Code:
    - C:/Ruby192

  • Finally, type "ruby dk.rb install" (no quotes) to DevKit enhance your installed Rubies.

The linked instructions has a step 5 that allows you to test your installation to make sure it's all working. I had no problems after running the above steps.
The Dev Kit should now be installed.
  • Now you type "gem install ruby-tmdb". You should see it report installation successful.
  • Copy the new fanartgrabber code from erolosty's post above and paste it into your old fanartgrabber.rb file.
  • Run the fanartgrabber and have a beer while you wait for new movies arts to download Big Grin
An awesome script!

A few small tips for some:

Your MyVideosdb might be 34, 55, 56, etc. depending on your build. Double check the number in case you get a sqlite3 error.

Also, on Linux you need ImageMagick and Ruby dev files to compile requirements with sudo gem. And to install ruby-tmdb do: sudo gem install ruby-tmdb .
Thanks ralob that should hopefully help someone. I got annoyed with it trying to fetch art for Movies that I never expect to see any fan art for, so I've added a commandline option:

ruby fanartGetter.rb --salt-earth

if you run that it will tell the script not to recheck any of the folders it can't find art for, meaning that next time you add a new movie fetching art takes less time Smile
First of all, thank you for your effort!

I have managed to get everything installed and the script running. However, I have about 100 movies on my fileserver, and it doesn't download fanart for any of them. Is the script able to handle smb://-paths? I'm thinking that might be the issue. I'm using xbmc-live Dharma 10.1, if that matters.

Example script output (similar for every movie):

Code:
"Getting art for Wall Street (smb://FILESERVER/data/Movies/Wall.Street.1987/)"
"fetching background #0"
"No art for this movie"
"Getting art for Natural Born Killers (smb://FILESERVER/data/Movies/Natural.Born.Killers.1994/)"
"No art for this movie"
updated version works like a dream
This seems to be working great! Good job!