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.

57 lines
1.7 KiB

8 years ago
  1. # coding: utf-8
  2. module SlackMathbot
  3. module Commands
  4. class Pixiv < SlackRubyBot::Commands::Base
  5. match /member_illust\.php(?<url>.*)$/ do |client, _data, _match|
  6. # Initalize Mechanize
  7. agent = Mechanize.new
  8. # Create Pixiv URL
  9. # If a full URL is given, extra characters are appended for some reason.
  10. # This ensures we catch them better
  11. if (_data[:text].include? "pixiv.net")
  12. pixiv_url = "http://www.pixiv.net/member_illust.php" + _match[:url][0..-2]
  13. else
  14. pixiv_url = "http://www.pixiv.net/member_illust.php" + _match[:url][0..-1]
  15. end
  16. puts pixiv_url
  17. # Create iOS Illustration URL, regex pixiv_url to
  18. # extract numbers only, such as /^[0-9]+$/
  19. #ios_url = "pixiv://illusts/" + pixiv_url.split("illust_id=")[-1]
  20. ios_url = "pixiv://illusts/" + pixiv_url[/\d+/]
  21. puts ios_url
  22. # TODO: Create iOS Member URL, must find correct URL scheme
  23. ios_mem_url = "pixiv://member/" + pixiv_url.split("?id=")[-1]
  24. puts ios_mem_url
  25. # Scrape page title
  26. title = Mechanize.new.get(pixiv_url).title
  27. puts title
  28. # Scrape image
  29. image_url = agent.get(pixiv_url).images_with(:src => /600x600\/img-master/)[0].to_s.sub! '600x600','480x960'
  30. puts image_url
  31. client.web_client.chat_postMessage(
  32. channel: _data.channel,
  33. as_user: true,
  34. attachments: [
  35. {
  36. fallback: title + " - " + pixiv_url,
  37. text: "<" + ios_url + "|Open in App>",
  38. title: title,
  39. title_link: pixiv_url,
  40. image_url: image_url,
  41. color: "#2684BD"
  42. }
  43. ].to_json
  44. )
  45. end
  46. end
  47. end
  48. end