A Slack Bot that pulls Pixiv information and posts the full image(s) into Slack, with iOS shortcuts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB

  1. # coding: utf-8
  2. module SlackMathbot
  3. module Commands
  4. class Pixiv < SlackRubyBot::Commands::Base
  5. match /pixiv\.net(?<url>.*)$/ do |client, _data, _match|
  6. # Initalize Mechanize
  7. agent = Mechanize.new
  8. # Create Pixiv URL
  9. pixiv_url = "http://www.pixiv.net" + _match[:url][0..-2]
  10. puts pixiv_url
  11. # Create iOS Illustration URL
  12. # ios_url = "pixiv://illusts/" + pixiv_url.split("illust_id=")[-1]
  13. illust_url = pixiv_url.split("illust_id=")[-1]
  14. puts illust_url
  15. ios_url = "pixiv://illusts/" + illust_url.slice(/^[0-9]+$/)
  16. puts ios_url
  17. # TODO: Create iOS Member URL, must find correct URL scheme
  18. ios_mem_url = "pixiv://member/" + pixiv_url.split("?id=")[-1]
  19. puts ios_mem_url
  20. # Scrape page title
  21. title = Mechanize.new.get(pixiv_url).title
  22. puts title
  23. # Scrape image
  24. image_url = agent.get(pixiv_url).images_with(:src => /600x600\/img-master/)[0].to_s.sub! '600x600','480x960'
  25. puts image_url
  26. client.web_client.chat_postMessage(
  27. channel: _data.channel,
  28. as_user: true,
  29. attachments: [
  30. {
  31. fallback: title + " - " + pixiv_url,
  32. text: "<" + ios_url + "|Open in App>",
  33. title: title,
  34. title_link: pixiv_url,
  35. image_url: image_url,
  36. color: "#2684BD"
  37. }
  38. ].to_json
  39. )
  40. end
  41. end
  42. end
  43. end