brutalismbot 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90f5979e9db90f44925f11aa92d7fa675ec789ca9566c6fe4a05242e372efffc
4
- data.tar.gz: a40a5b670b464532893c14577afb4f6c1b1f773754e921f1ab8c7cb43a51ac37
3
+ metadata.gz: 28218fbe0be73eed7c6f58124f4845c2ddcbcd5734055295c93ab544767455cf
4
+ data.tar.gz: 304ae833546888f31d9177e907bc6caba0f486a11048b8a16b2fcbe71ec2166b
5
5
  SHA512:
6
- metadata.gz: 530a4b7290fd4555457f2f04d56ac4989b61eef290daaf27df71f71907a94b7d339903ee4889abf0d4a847d96080e28aa0cfee2796e6d0ea0c162dced6cd0478
7
- data.tar.gz: d21755276d9592685a7b65fa23bc047e27ef0b722f54cfc71f6ca744577c0ade72f04f7665b931b7553639e0c87cca9a2f680556cec036cd680ad8d6895bcf9f
6
+ metadata.gz: dd8b2c2d014f52ce4a4d845e7342b4c5c8c4e5d62269b390376ea7de198b2098f800e9dea9931ec55e4c0ce17a4ca8badb66a2c92113c95c89901600cfbf2127
7
+ data.tar.gz: 254fed1155712f30f0440f38fb05576aabda571a9f6a592b8598f27f6b3bce4dbe27ac798b31aa37a6bfef200a07df5045c4633383471726751fbf34af3a6e98
@@ -1,5 +1,6 @@
1
1
  require "forwardable"
2
2
  require "json"
3
+ require "net/http"
3
4
 
4
5
  require "brutalismbot/base"
5
6
 
@@ -38,10 +39,45 @@ module Brutalismbot
38
39
  "#<#{self.class} #{data["permalink"]}>"
39
40
  end
40
41
 
42
+ def is_self?
43
+ data["is_self"]
44
+ end
45
+
41
46
  def kind
42
47
  @item["kind"]
43
48
  end
44
49
 
50
+ def media_uri
51
+ URI.parse(media_url)
52
+ end
53
+
54
+ def media_url
55
+ # Use URL if it's an image
56
+ if mime_type.start_with?("image/")
57
+ data["url"]
58
+
59
+ # Extract preview image URL
60
+ else
61
+ images = data.dig("preview", "images") || {}
62
+ source = images.map{|x| x["source"] }.compact.max do |a,b|
63
+ a.slice("width", "height").values <=> b.slice("width", "height").values
64
+ end
65
+ CGI.unescape_html(source["url"])
66
+ end
67
+ end
68
+
69
+ def mime_type
70
+ @mime ||= begin
71
+ uri = URI.parse(data["url"])
72
+ ssl = uri.scheme == "https"
73
+ Brutalismbot.logger.info("HEAD #{uri}")
74
+ Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
75
+ req = Net::HTTP::Head.new(uri)
76
+ http.request(req)["Content-Type"]
77
+ end
78
+ end
79
+ end
80
+
45
81
  def path
46
82
  created_utc.strftime("year=%Y/month=%Y-%m/day=%Y-%m-%d/%s.json")
47
83
  end
@@ -65,7 +101,7 @@ module Brutalismbot
65
101
  end
66
102
 
67
103
  def to_slack
68
- url.nil? ? to_slack_text : to_slack_image
104
+ is_self? ? to_slack_text : to_slack_image
69
105
  end
70
106
 
71
107
  def to_slack_image
@@ -78,7 +114,7 @@ module Brutalismbot
78
114
  text: "/r/brutalism",
79
115
  emoji: true,
80
116
  },
81
- image_url: url,
117
+ image_url: media_url,
82
118
  alt_text: title,
83
119
  },
84
120
  {
@@ -120,13 +156,7 @@ module Brutalismbot
120
156
  end
121
157
 
122
158
  def url
123
- images = data.dig("preview", "images") || {}
124
- source = images.map{|x| x["source"] }.compact.max do |a,b|
125
- a.slice("width", "height").values <=> b.slice("width", "height").values
126
- end
127
- CGI.unescapeHTML(source.dig("url"))
128
- rescue NoMethodError
129
- data["media_metadata"]&.values&.first&.dig("s", "u")
159
+ data["url"]
130
160
  end
131
161
  end
132
162
  end
@@ -16,18 +16,19 @@ module Brutalismbot
16
16
  created_utc: created_utc.to_i,
17
17
  permalink: "/r/brutalism/comments/#{permalink_id}/test/",
18
18
  title: "Post to /r/brutalism",
19
+ url: "https://image.host/#{image_id}.jpg",
19
20
  preview: {
20
21
  images: [
21
22
  {
22
23
  source: {
23
- url: "https://preview.redd.it/#{image_id}.jpg",
24
+ url: "https://preview.image.host/#{image_id}_large.jpg",
24
25
  width: 1000,
25
26
  height: 1000,
26
27
  },
27
28
  },
28
29
  {
29
30
  source: {
30
- url: "https://preview.redd.it/small.jpg",
31
+ url: "https://preview.image.host/#{image_id}_small.jpg",
31
32
  width: 500,
32
33
  height: 500,
33
34
  }
@@ -20,22 +20,20 @@ module Brutalismbot
20
20
  end
21
21
 
22
22
  def push(post, dryrun:nil)
23
- Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}twitter://@brutalismbot")
24
- method = post.url.nil? ? :push_text : :push_image
23
+ method = post.is_self? ? :push_text : :push_image
25
24
  send(method, post, dryrun: dryrun)
26
25
  end
27
26
 
28
27
  def push_text(post, dryrun:nil)
29
- status = post.to_twitter
30
- @client.update(status) unless dryrun
28
+ Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}twitter://@brutalismbot")
29
+ @client.update(post.to_twitter) unless dryrun
31
30
  end
32
31
 
33
32
  def push_image(post, dryrun:nil)
34
- status = post.to_twitter
35
- uri = URI.parse(post.url)
36
- Brutalismbot.logger.info("GET #{uri}")
37
- uri.open do |media|
38
- @client.update_with_media(status, media)
33
+ Brutalismbot.logger.info("GET #{post.media_uri}")
34
+ Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}twitter://@brutalismbot")
35
+ post.media_uri.open do |media|
36
+ @client.update_with_media(post.to_twitter, media)
39
37
  end unless dryrun
40
38
  end
41
39
  end
@@ -1,3 +1,3 @@
1
1
  module Brutalismbot
2
- VERSION = "1.5.0"
2
+ VERSION = "1.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brutalismbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Mancevice
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-18 00:00:00.000000000 Z
11
+ date: 2020-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twitter