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.

69 lines
2.0 KiB

8 years ago
  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. # Get time
  9. time = Time.now
  10. # Create Pixiv URL
  11. pixiv_url = "http://www.pixiv.net" + _match[:url][0..-2]
  12. puts pixiv_url
  13. # Create iOS Illustration URL, regex pixiv_url to
  14. # extract numbers only, such as /^[0-9]+$/
  15. #ios_url = "pixiv://illusts/" + pixiv_url.split("illust_id=")[-1]
  16. ios_url = "pixiv://illusts/" + pixiv_url[/\d+/]
  17. puts ios_url
  18. # TODO: Create iOS Member URL, must find correct URL scheme
  19. ios_mem_url = "pixiv://member/" + pixiv_url.split("?id=")[-1]
  20. puts ios_mem_url
  21. # Scrape page title
  22. title = Mechanize.new.get(pixiv_url).title
  23. puts title
  24. # Scrape image
  25. r = Range.new(
  26. Time.local(time.year, time.month, time.day, 12),
  27. Time.local(time.year, time.month, time.day, 20)
  28. ) === time
  29. if ((Date.today.to_s == "2016-04-01") && (r))
  30. api_url = "http://rule34.paheal.net/api/danbooru/find_posts/index.xml"
  31. agent.get(api_url)
  32. xml = agent.current_page.body
  33. status = XmlSimple.xml_in(xml)
  34. image_url = status["post"].sample["file_url"]
  35. puts agent.get(pixiv_url).images_with(:src => /600x600\/img-master/)[0].to_s.sub! '600x600','480x960'
  36. else
  37. image_url = agent.get(pixiv_url).images_with(:src => /600x600\/img-master/)[0].to_s.sub! '600x600','480x960'
  38. puts image_url
  39. end
  40. client.web_client.chat_postMessage(
  41. channel: _data.channel,
  42. as_user: true,
  43. attachments: [
  44. {
  45. fallback: title + " - " + pixiv_url,
  46. text: "<" + ios_url + "|Open in App>",
  47. title: title,
  48. title_link: pixiv_url,
  49. image_url: image_url,
  50. color: "#2684BD"
  51. }
  52. ].to_json
  53. )
  54. end
  55. end
  56. end
  57. end