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.

77 lines
2.5 KiB

8 years ago
  1. # coding: utf-8
  2. module SlackMathbot
  3. module Commands
  4. class Illust < 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. # Scrape profile link
  23. profile = agent.get(pixiv_url).at("div.userdata-row > h2.name > a").attributes['href'].to_s
  24. puts profile
  25. # Create iOS Members URL
  26. ios_mem_url = "pixiv://users/" + profile[/\d+/]
  27. puts ios_mem_url
  28. # Scrape page title
  29. title = agent.get(pixiv_url).title
  30. puts title
  31. # Scrape image
  32. image_url = agent.get(pixiv_url).images_with(:src => /600x600\/img-master/)[0].to_s.sub! '600x600','480x960'
  33. begin
  34. # Attempt to check on image
  35. page = agent.head(image_url)
  36. # Make sure page came back kosher
  37. if page.code.to_i == 200
  38. # Image url is fine
  39. puts "Image is good to go!"
  40. else
  41. # Fallback on http error to be safe
  42. image_url = "https://embed.pixiv.net/decorate.php?illust_id=" + pixiv_url[/\d+/]
  43. end
  44. rescue Mechanize::ResponseCodeError
  45. # Fallback to og:image url
  46. puts "Image failed to load"
  47. image_url = "https://embed.pixiv.net/decorate.php?illust_id=" + pixiv_url[/\d+/]
  48. end
  49. puts image_url
  50. client.web_client.chat_postMessage(
  51. channel: _data.channel,
  52. as_user: true,
  53. attachments: [
  54. {
  55. fallback: title + " - " + pixiv_url,
  56. text: "<" + ios_url + "|View Image> | <" + ios_mem_url + "|Open Profile>",
  57. title: title,
  58. title_link: pixiv_url,
  59. image_url: image_url,
  60. color: "#2684BD"
  61. }
  62. ].to_json
  63. )
  64. end
  65. end
  66. end
  67. end