simple-fourchan 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # 4chan
2
2
 
3
- Simple 4chan is a gem that fetches posts from 4chan. Current version only fetches a specific listing. Once 4chan released api for index, simple-fourchan will be able to show index treads of a message board.
3
+ Simple 4chan is a gem that fetches posts from 4chan. A complete reading solution to fetch posts from 4chan into your ruby, any rack-based or rails app. You set the board and simple-fourchan will fetch all posts from a board or thread number.
4
4
 
5
5
  ## Installation
6
6
 
@@ -4,25 +4,74 @@ require "json"
4
4
  require "ostruct"
5
5
 
6
6
  module Fourchan
7
+
8
+ FOURCHAN_BOARDS = ["a", "b", "c", "d", "e", "f", "g", "gif", "h", "hr", "k", "m", "o", "p", "r", "s", "t", "u", "v", "vg", "w", "wg", "i", "ic", "r9k", "cm", "hm", "y", "3", "adv", "an", "cgl", "ck", "co", "diy", "fa", "fit", "hc", "int", "jp", "lit", "mlp", "mu", "n", "po", "pol", "sci", "soc", "sp", "tg", "toy", "trv", "tv", "vp", "wsg", "x", "rs", "status", "q"]
9
+
7
10
  class Post
8
11
  def initialize( board, thread)
9
12
  @board = board
10
- @thread = thread.to_i
13
+ thread = thread.to_i
11
14
  @posts = []
12
- temp = JSON.parse(open("http://api.4chan.org/#{@board}/res/#{@thread}.json").read)["posts"]
13
- temp.each do |post|
14
- tim = post["tim"]
15
- post.merge!({"image" => "http://images.4chan.org/#{@board}/src/#{tim}.jpg"}) unless tim.nil?
16
- post.merge!({"thumb" => "http://thumbs.4chan.org/#{@board}/thumb/#{tim}s.jpg"}) unless tim.nil?
17
- post.merge!({"link" => "http://boards.4chan.org/#{@board}/res/#{@thread}"})
18
- @posts << OpenStruct.new(post)
19
- end
20
- @posts
15
+ begin
16
+ temp = JSON.parse(open("http://api.4chan.org/#{@board}/res/#{thread}.json").read)["posts"]
17
+ temp.each do |post|
18
+ tim = post["tim"]
19
+ post.merge!({"image" => "http://images.4chan.org/#{@board}/src/#{tim}.jpg"}) unless tim.nil?
20
+ post.merge!({"thumb" => "http://thumbs.4chan.org/#{@board}/thumb/#{tim}s.jpg"}) unless tim.nil?
21
+ post.merge!({"link" => "http://boards.4chan.org/#{@board}/res/#{thread}"})
22
+ @posts << OpenStruct.new(post)
23
+ end
24
+ @posts
25
+ rescue Exception => e
26
+ puts "The thread number #{thread} cannot be fetch. Please try later."
27
+ end
21
28
  end
22
29
 
23
30
  def all
24
31
  @posts
25
32
  end
33
+ # end of post class
34
+ end
35
+
36
+ class Board
37
+ def initialize( board )
38
+ @board = board
39
+ end
26
40
 
41
+ def exist?
42
+ FOURCHAN_BOARDS.include?(@board)
43
+ end
44
+
45
+ # page fetches all threads from that page
46
+ def page(page = 0)
47
+ @threads = []
48
+
49
+ begin
50
+ temp = JSON.parse(open("http://api.4chan.org/#{@board}/#{page}.json").read)["threads"]
51
+ temp.each do |post_thread|
52
+ post_thread_number = post_thread["posts"][0]["no"]
53
+ tim = post_thread["posts"][0]["tim"]
54
+ post_thread_thumb = "http://thumbs.4chan.org/#{@board}/thumb/#{tim}s.jpg"
55
+ thread_url = "http://boards.4chan.org/#{@board}/res/#{post_thread_number}"
56
+ hash = {"thread" => post_thread_number, "board" => @board, "thumb" => post_thread_thumb, "url" => thread_url}
57
+ @threads << OpenStruct.new(hash) unless post_thread_number.nil?
58
+ end
59
+ rescue Exception => e
60
+ puts "The board #{@board} page number #{page} cannot be fetched. Please try again."
61
+ end
62
+ @threads
63
+ end
64
+
65
+ # threads return all thread numbers and thumbs from a specific board
66
+ def threads
67
+ threads = []
68
+ (0..15).each do |i|
69
+ threads << self.page(i)
70
+ end
71
+ threads.flatten!
72
+ end
73
+ # end of board class
27
74
  end
75
+
76
+ # End of module
28
77
  end
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module Fourchan
3
- VERSION = "0.0.8"
3
+ VERSION = "0.0.9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-fourchan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-07 00:00:00.000000000 Z
12
+ date: 2012-09-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json