Class: Tsumanne::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board_id:) ⇒ API

Returns a new instance of API.



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

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.



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

def board_id
  @board_id
end

Instance Method Details

#get_thread_from_path(thread_path) ⇒ Object



53
54
55
56
57
58
# 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



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

def get_thread_mht(thread_id)
  # https://tsumanne.net/si/mht.php?id=129691
  res = fetch_json(paths: ["mht.php"], query: { id: thread_id }, method: :get_response)
  mht_gz = fetch(paths: [T.let(res["path"], String)])
  # https://ksef-3go.hatenadiary.org/entry/20070924/1190563143
  # https://docs.ruby-lang.org/ja/latest/method/Zlib=3a=3aInflate/s/new.html
  zstream = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
  buf = zstream.inflate(mht_gz)
  zstream.finish
  zstream.close
  Mhtml::RootDocument.new(buf)
end

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



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

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



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

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



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

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



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

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