Created
September 14, 2012 19:10
-
-
Save skord/3724040 to your computer and use it in GitHub Desktop.
simple wrapper for the 4chan JSON api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'json' | |
| require 'net/http' | |
| require 'time' | |
| class Chanpi | |
| attr_accessor :url, :posts, :http_resp_code, :last_modified | |
| attr_reader :board, :html_uri | |
| def initialize(json_url) | |
| @url = json_url | |
| @board = URI(json_url).path.split('/')[1] | |
| @html_uri = "http://boards.4chan.org/#{@board}/res/#{URI(json_url).path.split('/')[-1].split('.')[0]}" | |
| uri = URI.parse(json_url) | |
| http = Net::HTTP.new(uri.host) | |
| response = http.get(uri.path) | |
| @last_modified = response['last-modified'] | |
| if response.code == '200' | |
| json_response = JSON.parse(response.body) | |
| @posts = json_response['posts'].collect {|x| Post.new(x, @board)} | |
| @http_resp_code = '200' | |
| else | |
| @http_resp_code = response.code | |
| end | |
| end | |
| def refresh! | |
| uri = URI.parse(@url) | |
| http = Net::HTTP.new(uri.host) | |
| response = http.get(uri.path, {'if-modified-since' => @last_modified}) | |
| @last_modified = response['last-modified'] | |
| if response.code == '200' && @http_resp_code == '200' | |
| json_response = JSON.parse(response.body) | |
| @posts = json_response['posts'].collect {|x| Post.new(x, @board)} | |
| @http_resp_code = '200' | |
| elsif response.code == '304' | |
| @http_resp_code = response.code | |
| else | |
| @http_resp_code = response.code | |
| end | |
| return @http_resp_code | |
| end | |
| def images | |
| self.posts.collect {|x| x.image}.compact | |
| end | |
| def thumbnails | |
| self.posts.collect {|x| x.thumbnail}.compact | |
| end | |
| end | |
| class Post | |
| attr_reader :board, :no, :sticky, :resto, :closed, :now, :time, :name, | |
| :id, :capcode, :country, :country_name, :email, :sub, | |
| :com, :tim, :filename, :ext, :fsize, :md5, :w, :h, :tn_w, | |
| :tn_h, :filedeleted, :spoiler, :custom_spoiler, :image, | |
| :thumbnail | |
| def initialize(json_hash, board) | |
| @board = board | |
| @no = json_hash['no'].to_i | |
| @resto = json_hash['resto'].to_i | |
| @sticky = eval_bool(json_hash['sticky'].to_i) | |
| @closed = eval_bool(json_hash['closed'].to_i) | |
| @now = json_hash['now'] | |
| @time = Time.at(json_hash['time'].to_i).utc | |
| @name = json_hash['name'] | |
| @id = json_hash['id'] | |
| @capcode = json_hash['capcode'] | |
| @country = json_hash['country'] | |
| @country_name = json_hash['country_name'] | |
| @email = json_hash['email'] | |
| @sub = json_hash['subject'] | |
| @com = json_hash['com'] | |
| if !json_hash['filename'].nil? | |
| @tim = json_hash['tim'].to_i | |
| @filename = json_hash['filename'] | |
| @ext = json_hash['ext'] | |
| @fsize = json_hash['fsize'].to_i | |
| @md5 = json_hash['md5'] | |
| @w = json_hash['w'].to_i | |
| @h = json_hash['h'].to_i | |
| @tn_w = json_hash['tn_w'].to_i | |
| @tn_h = json_hash['tn_h'].to_i | |
| @filedeleted = eval_bool(json_hash['filedeleted'].to_i) | |
| @spoiler = eval_bool(json_hash['spoiler'].to_i) | |
| @custom_spoiler = json_hash['custom_spoiler'].to_i | |
| @image = Image.new(@board, @filename, @tim, @ext, @w, @h, @fsize, @md5) | |
| @thumbnail = Thumbnail.new(@board, @tim, @tn_w, @tn_h) | |
| end | |
| end | |
| def eval_bool(value) | |
| if value == 0 | |
| false | |
| elsif value == 1 | |
| true | |
| end | |
| end | |
| end | |
| class Image | |
| attr_reader :url, :original_filename, :remote_filename, :width, :height, :file_size, :md5 | |
| def initialize(board, filename, tim, ext, w, h, fsize, md5) | |
| @url = "http://images.4chan.org/#{board}/src/#{tim}#{ext}" | |
| @original_filename = filename + ext | |
| @remote_filename = tim.to_s + ext | |
| @width = w | |
| @height = h | |
| @file_size = fsize | |
| @md5 = md5 | |
| end | |
| end | |
| class Thumbnail | |
| attr_reader :url, :width, :height, :remote_filename | |
| def initialize(board, tim, tn_w, tn_h) | |
| @width = tn_w | |
| @height = tn_h | |
| @remote_filename = "#{tim}.jpg" | |
| @url = "http://0.thumbs.4chan.org/#{board}/thumb/#{tim}.jpg" | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment