Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'socket'
- require 'cgi'
- @host = "xkcd.com"
- @port = 80
- path = "/"
- def logic(i,errors,max,table)
- s = true
- begin
- ssocket = TCPSocket.open(@host,@port)
- ssocket.print(request ("/"+i.to_s+"/"))
- response = ssocket.read
- headers,body = response.split("\r\n\r\n",2)
- img = body[(/cs\//=~body)+3..(/[gf]" t/=~body)]
- /"/=~body
- if img.count(">") > 0
- img = img[(/src="/=~img)+33..-1]
- end
- imgfmt = img[-3..-1]
- ft = true
- ctitle = body[(/ctitle/=~body)+8..(/comicNav/=~body)-19]
- table[i] = CGI.unescapeHTML ctitle
- ctitle = CGI.escapeHTML ctitle
- ctitle = ctitle.gsub(/\'/,"'")
- begin
- funnytext = CGI.unescapeHTML(body[(/[gf]" t/=~body)+10..(/"#{Regexp.escape ctitle}"/=~body)-7])
- rescue
- errors << [i,"Mouse Over Text Not Downloaded, please download manually"]
- ft = false
- end
- puts "Downloading #{img} to #{i}.#{imgfmt} (#{i}/#{max})"
- ssocket2 = TCPSocket.open("imgs.xkcd.com",80)
- ssocket2.print(request2("/comics/"+img))
- response = ssocket2.read
- headers,imgdata = response.split("\r\n\r\n",2)
- f = File.new("./imgs/"+i.to_s+"."+imgfmt,"w")
- f.syswrite(imgdata)
- f.close
- f = File.new("./txts/"+i.to_s+".txt","w")
- f.syswrite(funnytext)
- f.close
- s = ft
- rescue
- errors << [i,"Image NOT Downloaded, please download manually"]
- s = false
- end
- return s
- end
- def request(path)
- return "GET #{path} HTTP/1.0\r\n\r\n"
- end
- def request2(path)
- return "GET #{path} HTTP/1.1\r\nHost:imgs.xkcd.com\r\n\r\n"
- end
- if Dir["./imgs"] == []
- Dir.mkdir("imgs")
- end
- if Dir["./txts"] == []
- Dir.mkdir("txts")
- end
- socket = TCPSocket.open(@host,@port)
- socket.print(request (path))
- response = socket.read
- headers,body = response.split("\r\n\r\n",2)
- max = body[(/Permanent\ link\ to\ this\ comic:\ http:\/\/xkcd.com\//=~body)+46..(/\/\<\/h3\>$/=~body)-1].to_i
- t = 0
- continue = true
- errors = []
- s = ""
- if File.exists?("strips.db")
- f = File.new("strips.db", "r+")
- f.each_line do |line|
- s += line
- end
- else
- s = "[]"
- end
- numt=45
- table = eval s
- while t <= max/numt + numt+1
- threads = []
- numt.times do |i|
- threads << Thread.new do
- i = i + t*numt + 1
- if i <= max
- if !File.exists?("./txts/"+i.to_s+".txt") && i != 404
- logic(i,errors,max,table)
- end
- end
- end
- end
- threads.each{|a| a.join}
- t+=1
- end
- errors.each do |arr|
- v = []
- fixed = logic(arr[0],v,max,table)
- if !fixed
- puts "Error in downloading #{v[0][0]}: #{v[0][1]}"
- end
- end
- table[0]=max
- f = File.new("strips.db", "w+")
- f.syswrite(table.inspect)
- f.close
Add Comment
Please, Sign In to add comment