alt.binaries.e-book.technicalPrev. Next
Re: Thanks Psycho! Also a script for extracting the files others might find useful Hawker
Tempest (tempest@hawker.co.uk) 2015/05/28 14:22

Path: news.nzbot.com!not-for-mail
From: Tempest <tempest@hawker.co.uk>
Organization: Hawker
Newsgroups: alt.binaries.e-book.technical
Date: Fri, 29 May 2015 06:22:52 +1000
Message-ID: <5567791c$0$36749$b1db1813$e2fc9064@news.astraweb.com>
References: <5564e688$0$19776$c3e8da3$33881b6a@news.astraweb.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Subject: Re: Thanks Psycho! Also a script for extracting the files others might find useful
User-Agent: Unison/2.1.10
Lines: 99
NNTP-Posting-Host: 7dd758ad.news.astraweb.com
X-Trace: DXC=Bg82JiI0Nn>hn]AcMfkkb9L?0kYOcDh@:E4R[VkOgBe1JBV<3L[baE0ki>@a>bjai<c5_Y>feR_9?O[1c1DWZRm>LOLFVn=jNn8lc20?gImXe3
X-Received-Body-CRC: 2893905889
X-Received-Bytes: 3694
Xref: news.nzbot.com alt.binaries.e-book.technical:933

An update to my dumb little script - now renames short pdf or epub
names to the folder name - strips out periods, and word-capitalises the
filename.



# de-psycho USENET posts
require 'rubygems'
require 'zip'
require 'fileutils'

class Desyco

 def process_folders(dirfilter,destination, moveto='done')
  Dir.glob(dirfilter).each do |dir|
   next unless File.directory? dir
   next if dir == moveto

   puts "Processing #{dir}"
   Dir.chdir dir
   files = Dir.glob("*.zip")
   files.each do |file|
    extract file
   end
   Dir.chdir("..")
   # now unrar whatever came out

   files = Dir.glob("#{escape_glob(dir)}/*.rar")
   successful_unrars = 0
   files.each do |file|
    successful_unrars += 1 if system("unrar x -o+ '#{file}' '#{dir}'")
   end
   if successful_unrars == files.count
    ['.pdf','.epub'].each do |ext|
     files = Dir.glob("#{escape_glob(dir)}/*#{ext}")
     files.each do |file|
      if File.basename(file,ext).length <= 9
       newname = /^.*\[(.+)\].*$/.match(dir)[1].gsub(/\./,'
').strip.split.map(&:capitalize).join(' ')
       puts "renamed to #{newname}"
       FileUtils.mv file,"#{destination}/#{newname}#{ext}"
      end
     end
    end
   # move to done folder if all rar files unpacked OK
   FileUtils.mv dir,moveto
   end
  end
 end

 def escape_glob s
   s.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
 end

 def extract file
  begin
  Zip::File.open(file) do |zip|
    zip.each do |zf|
     puts "Extracting #{zf.name}"
     zf.extract(zf.name) unless File.exists?(zf.name)
    end
   end
  rescue
   puts "error unzipping #{file}"
  end
 end

end


Desyco.new.process_folders(ARGV[0],ARGV[1])


On 2015-05-26 21:32:57 +0000, Tempest said:

> First of all, thanks very much psychopath for your posts, really awesome.
>
> I've made a dumb little ruby script that unzips and unrars Pyscho's
> file structure that others might find useful here.
> The script could easily have been in BASH or anything since it just
> unzips and unrars.
>
> It unzips, then unrars with system() to a nominated folder, then moves
> the processed folder out the way if the unrar was successful - that way
> you can easily see what failed afterwards by what wasn't moved out the
> way.
>
> Very little error checking here but the syntax is:
>
> ruby desyco.rb folders destination
>
> folders can be wildcards, destination is where it unrars to. The
> folders are moved to a folder called "done" in the parent director, you
> need to mkdir that folder before starting.
>
> anyone could easily modify this to also name those files that don't
> have the book name using the books folder name
>


Follow-ups:12
Next Prev. Article List         Favorite