Class: Tsumanne::API

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tsumanne/api.rb

Overview

Client for the tsumanne.net endpoints of a single board.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board_id:) ⇒ API

Returns a new instance of API.



26
27
28
# File 'lib/tsumanne/api.rb', line 26

def initialize(board_id:)
  @board_id = T.let(T.must(BOARD_IDS[board_id]), String)
end

Instance Attribute Details

#board_idObject (readonly)

Returns the value of attribute board_id.



23
24
25
# File 'lib/tsumanne/api.rb', line 23

def board_id
  @board_id
end

Instance Method Details

#get_thread_from_path(thread_path) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/tsumanne/api.rb', line 53

def get_thread_from_path(thread_path)
  # https://tsumanne.net/si/data/2023/08/30/8883354/
  match_data = %r{^\d{4}/\d{2}/\d{2}/(?<thread_id>\d+)$}.match(thread_path)
  return if match_data.nil?

  get_thread_mht(T.must(match_data[:thread_id]))
end

#get_thread_mht(thread_id) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
# File 'lib/tsumanne/api.rb', line 42

def get_thread_mht(thread_id)
  # https://tsumanne.net/si/mht.php?id=129691 -> 302 ./mhttmp/86279902.html
  uri = join_paths(BASE_URL, [@board_id, "mht.php"])
  uri.query = URI.encode_www_form({ id: thread_id })
  res = Net::HTTP.get_response(uri)
  raise Error, "thread #{thread_id} is not archived (HTTP #{res.code})" unless res.is_a?(Net::HTTPRedirection)

  T.must(Net::HTTP.get(URI.join(uri, res["location"])))
end

#get_threads(index: "all", page: 1) ⇒ Object



31
32
33
34
35
# File 'lib/tsumanne/api.rb', line 31

def get_threads(index: "all", page: 1)
  # https://tsumanne.net/si/all/1
  # https://tsumanne.net/si/hoge/1
  GetThreadsResponse.from_hash(fetch_json(paths: [index, page.to_s]))
end

#register_thread(uri, indexes: nil) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/tsumanne/api.rb', line 79

def register_thread(uri, indexes: nil)
  # post, https://tsumanne.net/si/input.php?format=json&url=...&category=...
  RegisterThreadResponse.from_hash(
    fetch_json(paths: ["input.php?format=json"], query: { url: uri, category: (indexes || []).join(",") },
               method: :post)
  )
end

#search_indexes(keyword: nil, order: :newer, page: 1) ⇒ Object



71
72
73
74
75
76
# File 'lib/tsumanne/api.rb', line 71

def search_indexes(keyword: nil, order: :newer, page: 1)
  # https://tsumanne.net/si/indexes.php?format=json&w=&sbmt=%E2%86%93%E6%96%B0
  SearchIndexesResponse.from_hash(
    fetch_json(paths: ["indexes.php"], query: { w: keyword, sbmt: INDEXES_ORDERS[order], p: page })
  )
end

#search_thread_from_uri(uri) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/tsumanne/api.rb', line 62

def search_thread_from_uri(uri)
  # https://tsumanne.net/si/indexes.php?format=json&w=...&sbmt=URL
  # https://tsumanne.net/si/indexes.php?format=json&w=https%3A%2F%2Fimg.2chan.net%2Fb%2Fres%2F86279902.htm&sbmt=URL
  SearchThreadFromUriResponse.from_hash(
    fetch_json(paths: ["indexes.php"], query: { w: uri, sbmt: :URL })
  )
end