Browse Source

Sanitize content before tweet

tags/v0.1.2
aeris 1 year ago
parent
commit
966b7554a2
3 changed files with 40 additions and 18 deletions
  1. 10
    0
      lib/cross-post.rb
  2. 6
    3
      lib/cross-post/mastodon.rb
  3. 24
    15
      lib/cross-post/twitter.rb

+ 10
- 0
lib/cross-post.rb View File

@@ -2,6 +2,7 @@ require 'cross-post/config'
2 2
 require 'cross-post/mastodon'
3 3
 require 'cross-post/twitter'
4 4
 require 'open-uri'
5
+require 'logger'
5 6
 
6 7
 # Force OpenURI#open to return a TempFile and not a StringIO
7 8
 OpenURI::Buffer.send :remove_const, 'StringMax'
@@ -9,6 +10,15 @@ OpenURI::Buffer.const_set 'StringMax', 0
9 10
 
10 11
 
11 12
 class CrossPost
13
+	LOGGER = Logger.new STDERR
14
+	LOGGER.level = Logger.const_get ENV.fetch('LOG', 'INFO')
15
+	LOGGER.formatter = proc do |severity, time, _, msg|
16
+		time = time.strftime '%Y-%m-%dT%H:%M:%S.%6N'.freeze
17
+		"#{time} #{severity} #{msg}\n"
18
+	end
19
+
20
+	attr_reader :mastodon, :twitter
21
+
12 22
 	def initialize
13 23
 		@config   = Config.new
14 24
 		@mastodon = Mastodon.new @config

+ 6
- 3
lib/cross-post/mastodon.rb View File

@@ -1,5 +1,6 @@
1 1
 require 'mastodon'
2 2
 require 'sanitize'
3
+require 'awesome_print'
3 4
 
4 5
 class CrossPost
5 6
 	class Mastodon
@@ -18,11 +19,13 @@ class CrossPost
18 19
 					case object
19 20
 					when ::Mastodon::Status
20 21
 						next if reject? object
21
-						twitter.post object
22
+						LOGGER.info { 'Receiving status' }
23
+						LOGGER.debug { status.ap }
24
+						twitter.post_status object
22 25
 					end
23 26
 				rescue => e
24
-					#$stderr.puts e
25
-					raise
27
+					LOGGER.error e
28
+					#raise
26 29
 				end
27 30
 			end
28 31
 		end

+ 24
- 15
lib/cross-post/twitter.rb View File

@@ -1,5 +1,7 @@
1 1
 require 'twitter'
2 2
 require 'twitter-text'
3
+require 'sanitize'
4
+require 'cgi'
3 5
 
4 6
 class CrossPost
5 7
 	class Twitter
@@ -14,21 +16,11 @@ class CrossPost
14 16
 			@stream = ::Twitter::Streaming::Client.new config
15 17
 		end
16 18
 
17
-		def post(status)
18
-			content = Sanitize.clean status.content
19
-			last    = nil
20
-			parts   = split content
21
-
22
-			attachments = status.media_attachments
23
-			media       = attachments.collect do |f|
24
-				f = open f.url
25
-				begin
26
-					@client.upload f
27
-				ensure
28
-					f.close
29
-					f.unlink
30
-				end
31
-			end
19
+		def post(content, media = [])
20
+			media = media.collect { |f| @client.upload f }
21
+
22
+			parts = split content
23
+			last  = nil
32 24
 
33 25
 			unless media.empty?
34 26
 				first, *parts = parts
@@ -37,6 +29,23 @@ class CrossPost
37 29
 			parts.each { |p| last = @client.update p, in_reply_to_status: last }
38 30
 		end
39 31
 
32
+		def post_status(status)
33
+			content = Sanitize.clean status.content
34
+			content = CGI.unescape_html content
35
+			media   = status.media_attachments.collect { |f| open f.url }
36
+
37
+			LOGGER.info { 'Sending to twitter' }
38
+			LOGGER.debug { "  Content: #{content}" }
39
+			LOGGER.debug { "  Attachments: #{media.size}" }
40
+
41
+			self.post content, media
42
+
43
+			media.each do |f|
44
+				f.close
45
+				f.unlink
46
+			end
47
+		end
48
+
40 49
 		private
41 50
 
42 51
 		def split(text)

Loading…
Cancel
Save