Skip to content

Instantly share code, notes, and snippets.

@markolson
Last active December 17, 2015 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markolson/5523150 to your computer and use it in GitHub Desktop.
Save markolson/5523150 to your computer and use it in GitHub Desktop.
Sync folders of CBI/CBR/CBZ files to ComicZeal.
#!/usr/local/bin/python
# CopyPaste/print programming & debugging at it's finest.
import os, re, sys, subprocess, time
import zipfile, tempfile
import sqlite3
def main():
print sys.argv
filename = sys.argv[1]
newname,ext = os.path.splitext(os.path.basename(filename))
p = re.compile('[^\w_]')
newname = p.sub('_', newname)
os.system("rm -rf work/ *.cbi")
conn = sqlite3.connect(newname + '.cbi')
c = conn.cursor()
setup_db(c)
print filename
print conn
i = 0
if ext == '.cbz':
print "CBZ"
cbz = zipfile.ZipFile(filename)
for info in cbz.infolist():
if info.file_size == 0:
continue
_,ext = os.path.splitext(os.path.basename(info.filename))
if ext != '.jpg':
continue
i = i + 1
data = cbz.read(info.filename)
img = tempfile.NamedTemporaryFile()
img.write(data)
img.flush()
subprocess.call(["convert", img.name, "-resize","1024x1024>", "/tmp/out.%d.jpg" % i])
data = open("/tmp/out.%d.jpg" % i, 'r').read()
add_file(c, newname, i, data)
c.execute("insert into comics(title, page_count) values ('%s', %d)" % (newname, i))
conn.commit()
else:
print "CBR?"
subprocess.call(["mkdir", "work"])
print subprocess.call(["unrar", "e", "-y", filename, "work/"])
dirList=os.listdir(os.path.join(os.getcwd(),"work"))
for fname in dirList:
_,ext = os.path.splitext(os.path.basename(fname))
if ext != '.jpg':
continue
i = i + 1
print os.path.join(os.getcwd(),"work", fname)
subprocess.call(["convert", os.path.join(os.getcwd(),"work", fname), "-resize", "1024x1024>", "/tmp/out.%d.jpg" % i])
data = open("/tmp/out.%d.jpg" % i, 'r').read()
add_file(c, newname, i, data)
c.execute("insert into comics(title, page_count) values ('%s', %d)" % (newname, i))
conn.commit()
def setup_db(c):
c.execute("CREATE TABLE bookmarks(bookmark TEXT,comic TEXT,page_number INTEGER)")
c.execute("CREATE TABLE comics (title TEXT,page_count INTEGER)")
c.execute("CREATE TABLE database_attributes (attribute_name TEXT, attribute_value TEXT)")
c.execute("CREATE TABLE pages (comic_title TEXT,page_number INTEGER,image BLOB)")
c.execute("insert into database_attributes(attribute_name, attribute_value) values ('version', 1)")
def add_file(c, name, page, blob):
c.execute('insert into pages(comic_title, page_number, image) values (?,?,?)', [name, page, buffer(blob)])
main()
require 'fileutils'
require 'shellwords'
require 'sqlite3'
require 'dnssd'
DNSSD.register 'CZ Sync [new]', '_http._tcp', nil, 4566, DNSSD::TextRecord.new('path' => 'index') do |r|
puts "registered #{r.fullname}" if r.flags.add?
end
$db = SQLite3::Database.new "filez.db"
begin
rows = $db.execute <<-SQL
create table filez (
fullpath text,
cbi_name text,
mtime int,
downloaded int,
built int
);
SQL
rescue
p ":("
end
Thread.abort_on_exception = true
require 'sinatra'
$basepath = ARGV.last
exit if $basepath.nil?
$files = {}
set :port => 4566
def check_files
Dir["#{$basepath}/**/*.cb{i,z,r}"].map {|x|
unless File.directory? x
short = File.basename(x, File.extname(x)).gsub(/\W/,'_') + '.cbi'
$files[short] = x
if 0 == $db.get_first_value( "select count(*) from filez where fullpath = ?", x)
fm = File.mtime(x).to_i
$db.execute("INSERT INTO filez (fullpath, cbi_name, mtime, downloaded, built) VALUES (?, ?, ?, 0, 0)", x, short, fm)
end
end
}
end
$builder = Thread.new {
`mkdir -p built`
while true do
check_files
p "Running Build Thread.."
$db.execute("select fullpath, cbi_name from filez where built = 0 order by fullpath asc").each {|r|
p "Starting #{r[1]}"
`python cbr_to_cbi.py #{Shellwords.escape(r[0])}`
"mv #{Shellwords.escape(r[1])} built/#{r[1]}"
`mv #{Shellwords.escape(r[1])} built/#{r[1]}`
$db.execute("UPDATE filez SET built = 1 WHERE cbi_name = ?", r[1])
p "Finished #{r[1]}"
}
res = $db.execute("select cbi_name from filez where downloaded = 1")
p res.count
sleep 120 # give it some time to actually download?
res.each { |r|
p "rm built/#{r[0]}"
`rm built/#{r[0]}`
}
end
}
$server = Thread.new {
get '/list' do
#only_since = File.exists?('lastfetch') ? File.mtime('lastfetch') :
only_since = Time.at(0)
out = ['<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">']
out << '<html><head><title>SyncDocs 0.6.4-RC2: Available Files</title></head><body>'
$db.execute("select cbi_name, mtime from filez where downloaded = 0 and built = 1 order by fullpath asc").each {|r|
out << "<a dir=\"false\" lastModifiedDate=\"#{Time.at(r[1])}\" href=\"/f-#{r[0]}\">#{r[0]}</a><br />"
}
out.join("\r\n")
end
get '/f-:name' do
name = ::URI.decode(params[:name])
r = File.basename(name, File.extname(name))
$db.execute("UPDATE filez SET downloaded = 1 WHERE cbi_name = ?", name)
send_file 'built/' + name
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment