Class: Sync
- Inherits:
-
Object
- Object
- Sync
- Defined in:
- lib/examples/db.rb,
lib/examples/sync.rb
Instance Method Summary collapse
- #advanced_set(set, name, s) ⇒ Object
- #download(post, name, set, id = 0) ⇒ Object
- #fetch_posts ⇒ Object
-
#initialize ⇒ Sync
constructor
A new instance of Sync.
- #threads ⇒ Object
- #worker(mod) ⇒ Object
Constructor Details
#initialize ⇒ Sync
Returns a new instance of Sync.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/examples/db.rb', line 51 def initialize E621::Config.config = File.("~/.hexagon/conf.json") if $stdout.tty? then @log = Logger.new($stdout) @log.level = Logger::DEBUG else @log = Logger.new(File.(E621::Config.paths["logging"])) @log.level = Logger::INFO end @log.formatter = proc do |sev,dat,prog,msg| "#{Time.now.strftime($form)} [#{sev.pad(5)}] #{msg}#$/" end api = API.new("user") # Used for login @log.info("Program #$0 started.") @uid = api.get("index",{"name"=>api.user}).first["id"] @max_wait = 1 Dir.chdir(File.(E621::Config.paths["files"])) @db = DB.new("danbooru") @queue = Queue.new @mt = Mutex.new end |
Instance Method Details
#advanced_set(set, name, s) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/examples/sync.rb', line 115 def advanced_set(set,name,s) sid, owner, query = set["id"], set["owner"], set["search"] if owner == @uid then Search.new("fav:maxine_red #{query} order:id".split(" ")).each_post do |post| next if @posts["downloads"].include?(post.id) download(post,name,s) end api = API.new("set") set = api.get("show", {"id"=>sid}) posts = set["posts"].map{|post|Post.new(post).id} name = set["shortname"] local = Dir["#{name}/*"].reject{|x|x.match(/db$/)}.map{|x|x.split(".")[1].to_i}.sort.uniq (posts-local).each do |id| post = Post.new({"id"=>id}) f = post.remove_from_set(sid) if f["success"] then s = "Removed Post #{" "*(6-post.id.to_s.length)}#{post.id} from \"#{set["name"]}\"" puts "#{Time.now.strftime($form)}: \e[1;33m#{s}\e[0m" @log.info(s) else s = "#{f["reason"].to_s.gsub(/<.+?>/,"")}." puts "#{Time.now.strftime($form)}: \e[1;31m#{s}\e[0m" @log.info(s) end end (local-posts).each do |id| next if id == 0 post = Post.new({"id"=>id}) f = post.add_to_set(sid) if f["success"] then s = "Added Post #{" "*(6-post.id.to_s.length)}#{post.id} to \"#{set["name"]}\"." puts "#{Time.now.strftime($form)}: #{s}" @log.info(s) else s = "#{f["reason"].gsub(/<.+?>/,"")}. Removing local file." puts "#{Time.now.strftime($form)}: #{s}" @log.info(s) File.unlink(Dir["#{set["shortname"]}/*.#{"0"*(8-post.id.to_s.length)}#{post.id}.*"].first) end end end end |
#download(post, name, set, id = 0) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/examples/db.rb', line 96 def download Thread.current.exit if @queue.empty? post,path = @queue.pop m = String.new path.split("/")[0,2].each do |d| m += "#{d}/" Dir.mkdir(m) unless File.exists?(m) end tries = 0 begin post.download(path) rescue raise if tries > 8 tries += 1 sleep 2 retry end data = @cols.map do |x| y=post.__send__(x.to_sym) y = y.to_i if x == "created_at" y = y.join(" ") if x == "artist" y != nil ? @db.escape_string(y.to_s) : "NULL" end if !@db.exec("SELECT id FROM e621.posts WHERE id = #{post.id};").first then query = "INSERT INTO e621.posts (#{@cols.join(",")}) VALUES ('#{data.join("','")}');".gsub("'NULL'","NULL") @db.exec(query) @mt.synchronize do @log.info("Added post ##{post.id.pad(6," ")} to library.") end end end |
#fetch_posts ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/examples/db.rb', line 83 def fetch_posts lid = @db.exec("SELECT id FROM e621.posts ORDER BY id DESC LIMIT 1;").first if lid then lid = lid["id"].to_i else lid = 0 end posts = Search.new(["order:id", "id:#{lid}.."]) @cols = ["id","tags","artist","created_at","score","md5","file_ext","description","rating","width","height","has_children","parent_id","file_size"] posts.each_post do |post| @queue << [post,"#{post.md5[0,2]}/#{post.md5[2,2]}/#{post.md5}.#{post.file_ext}"] end end |
#threads ⇒ Object
73 74 75 |
# File 'lib/examples/db.rb', line 73 def threads return E621::Config.threads end |
#worker(mod) ⇒ Object
77 78 79 80 81 |
# File 'lib/examples/db.rb', line 77 def worker(mod) if mod == "post" then fetch_posts end end |