• # Piwigo c'est cool

    Posté par . En réponse au journal Partager ses photos et auto-hébergement. Évalué à 8.

    Par contre moi c'est toute la famille qui upload et je veux un backup bien organiser en local ( pour consultation sur le kodi brancher sur la tv du salon.

    J'avais donc dev un script ruby pour synchroniser piwigo sur mon dédié et le filesystem en local

    Si ça peux être utile a quelqu'un ... :)

    require 'mechanize'
    require 'json'
    require 'fileutils'
    require 'pp'
    #Variable
    username = 'XXXXX'
    password = 'XXXX'
    wsurl = 'http://XXX.XXXX.XXX/ws.php?format=json'
    rootpath = "/home/data/Images/webgallery/"
    #Start Mechanize agent
    agent = Mechanize.new
    #Auth against the gallery
    data = "method=pwg.session.login&username=#{username}&password=#{password}"
    response = agent.post(wsurl, {
     "method" => "pwg.session.login",
     "username" => username,
     "password" => password
    })
    #Get Album list
    response = agent.get( "#{wsurl}&method=pwg.categories.getList&recursive=true")
    puts "List folders and pictures ..."
    #Source Album and picture array
    result = JSON.parse(response.body)
    album_h = {}
    folder_a = []
    pictureurl_h = {}
    picturepath_h = {}
    result['result']['categories'].each do |album|
     album_h[album['id']] = album['name']
     catspath = album['uppercats'].split(',')
     fullpath = rootpath
     until catspath.empty?
     fullpath = "#{fullpath}#{album_h[catspath.shift.to_i]}/"
     end
     folder_a<<fullpath
     if album['nb_images'] > 0 then
     response = agent.get( "#{wsurl}&method=pwg.categories.getImages&cat_id=#{album['id']}&per_page=500") 
     result2 = JSON.parse(response.body)
     result2['result']['images'].each do |image|
     pictureurl_h[image['id']] = "#{image['element_url']}"
     picturepath_h[image['id']] = "#{fullpath}#{image['file']}"
     end
     end
    end
    puts "There is #{pictureurl_h.to_a.count} pictures in #{folder_a.count} folders on the webgallery"
    #Delete no more needed things
    puts "Now delete local item not present on web"
    Dir.glob("#{rootpath}**/*/").each do |localdir|
     unless folder_a.include?(localdir) then
     puts "=> Need to delete #{localdir}"
     FileUtils.rm_r localdir
     end
    end