Skip to content

Commit

Permalink
Fix Pixiv.
Browse files Browse the repository at this point in the history
  • Loading branch information
rphln committed Feb 11, 2021
1 parent 075e5f8 commit ef7a69f
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 361 deletions.
9 changes: 5 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ config :logger,
config :nostrum,
token: System.get_env("DISCORD_TOKEN")

config :mojito,
timeout: :infinity

config :katsuragi,
pixiv_username: System.get_env("PIXIV_USERNAME"),
pixiv_password: System.get_env("PIXIV_PASSWORD")
pixiv_session_token: System.get_env("PIXIV_SESSION_TOKEN")

config :extwitter, :oauth,
consumer_key: System.get_env("TWITTER_CONSUMER_KEY"),
Expand All @@ -20,8 +22,7 @@ config :extwitter, :oauth,

config :katsuragi, Katsuragi.Scheduler,
jobs: [
{"20 19 * * *", {Katsuragi.Twitter, :send, []}},
{"*/45 * * * *", {Katsuragi.Commands.Pixiv.AuthServer, :refresh, []}}
{"20 19 * * *", {Katsuragi.Twitter, :send, []}}
]

if Mix.env() in [:dev, :test] do
Expand Down
3 changes: 1 addition & 2 deletions config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ config :nostrum,
token: System.get_env("DISCORD_TOKEN")

config :katsuragi,
pixiv_username: System.get_env("PIXIV_USERNAME"),
pixiv_password: System.get_env("PIXIV_PASSWORD")
pixiv_session_token: System.get_env("PIXIV_SESSION_TOKEN")
5 changes: 0 additions & 5 deletions lib/katsuragi/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ defmodule Katsuragi.Application do
use Application

alias Katsuragi.Consumer
alias Katsuragi.Commands.Pixiv

@spec start(any, any) :: {:error, any} | {:ok, pid}
def start(_type, _args) do
username = Application.get_env(:katsuragi, :pixiv_username)
password = Application.get_env(:katsuragi, :pixiv_password)

children = [
# {Pixiv.AuthServer, username: username, password: password},
Katsuragi.Scheduler
]

Expand Down
84 changes: 53 additions & 31 deletions lib/katsuragi/commands/pixiv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,74 @@ defmodule Katsuragi.Commands.Pixiv do
alias Nostrum.Struct.Embed

alias Katsuragi.Commands.Pixiv.Work
alias Katsuragi.Commands.Pixiv.AuthServer
alias Katsuragi.Commands.Pixiv.Constants

@pattern ~r"pixiv\S*?/artworks/(\d+)"i

def aliases do
["pixiv"]
end

def reply(message, gallery_id) do
with {:ok, work} <- Work.get(gallery_id),
{:ok, file} <- Work.download(work["urls"]["regular"]) do
name = Path.basename(work["urls"]["regular"])

description =
work["tags"]["tags"]
|> Enum.map(&(&1["translation"]["en"] || &1["tag"]))
|> Enum.join(" • ")

embed =
%Embed{}
|> Embed.put_color(0x0086E0)
|> Embed.put_title(work["title"])
|> Embed.put_author(work["userName"], Work.author_link_for(work), nil)
|> Embed.put_image("attachment://#{name}")
|> Embed.put_description("`#{description}`")
|> Embed.put_footer("""
Gallery with #{work["pageCount"]} page(s).
Shared by #{message.author.username}.
""")
|> Embed.put_timestamp(Work.updated_at!(work))
|> Embed.put_url(Work.link_for(work))

file = %{body: file.body, name: name}

Api.create_message(message, file: file, embed: embed)
end
end

def call(%Request{message: message} = request) do
matches =
for [_match, gallery_id] <- Regex.scan(@pattern, message.content) do
tokens = AuthServer.refresh_and_get()

with {:ok, work} <- Work.get(tokens, gallery_id),
{:ok, file} <- Work.download(tokens, work["image_urls"]["medium"]) do
name = Path.basename(work["image_urls"]["medium"])

description =
work["tags"]
|> Enum.map(&(&1["translated_name"] || &1["name"]))
|> Enum.join(" • ")

embed =
%Embed{}
|> Embed.put_color(0x0086E0)
|> Embed.put_title(work["title"])
|> Embed.put_author(work["user"]["name"], Work.author_link_for(work), nil)
|> Embed.put_image("attachment://#{name}")
|> Embed.put_description("`#{description}`")
|> Embed.put_footer("""
Gallery with #{work["page_count"]} page(s).
Shared by #{message.author.username}.
""")
|> Embed.put_timestamp(Work.updated_at!(work))
|> Embed.put_url(Work.link_for(work))

file = %{body: file.body, name: name}

Api.create_message!(message, file: file, embed: embed)
Process.sleep(2000)
task = Task.async(fn -> reply(message, gallery_id) end)

# Wait for a while to avoid triggering the rate limiter.
Process.sleep(2000)

case Task.await(task, :infinity) do
{:ok, response} ->
{:ok, response}

_error ->
{:error, "Failed to create preview for #{Constants.gallery_url(gallery_id)}"}
end
end

Request.register_after_send(request, fn _request ->
unless Enum.empty?(matches) do
Api.delete_message(message)
errors =
Enum.filter(matches, fn
{:ok, _response} -> false
{:error, _reason} -> true
end)

if Enum.empty?(errors) do
Api.delete_message!(message)
else
Enum.each(errors, fn {:error, reason} -> Api.create_message!(message, reason) end)
end
end
end)
end
Expand Down
57 changes: 0 additions & 57 deletions lib/katsuragi/commands/pixiv/auth_server.ex

This file was deleted.

106 changes: 0 additions & 106 deletions lib/katsuragi/commands/pixiv/authenticator.ex

This file was deleted.

Loading

0 comments on commit ef7a69f

Please sign in to comment.