reddit-to-telegram 0.6.1 → 0.7.1
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 +4 -4
- data/LICENSE +21 -0
- data/README.md +14 -13
- data/lib/reddit_to_telegram/reddit/output.rb +14 -5
- data/lib/reddit_to_telegram/telegram/post/gallery.rb +49 -0
- data/lib/reddit_to_telegram/telegram/post.rb +28 -3
- data/lib/reddit_to_telegram/telegram/prepare_request.rb +11 -2
- data/lib/reddit_to_telegram/variables.rb +5 -1
- data/lib/reddit_to_telegram/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08430693bae08f13ed579d54b5647c7a08e2f41cc9b56d5f5b81de3c2a6ae843'
|
4
|
+
data.tar.gz: 6e84d4876a8168ba5a7eee55b00e65529487f45a06bccc7df421a0579af3859c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee7c43b1db322d243a46f555821f13d81fbdf2651fa9af8ee949bf44dacc6225dd2b63762643b5fbc3008f59985d4d1f100e883aa2bec4d4acc9c18375d8f721
|
7
|
+
data.tar.gz: 39b4de9770189c74240f77c8dc07be38ede2f656e9d385afbd0ee221d87c6bd1d90c482f3d8345ae74d985f39deb1a3fc4d87f8bda6218fb9bbf7bb359f7cb08
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Mark Tityuk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -22,19 +22,20 @@ Or `gem install reddit-to-telegram`. Don't forget to `require` it.
|
|
22
22
|
- [Obtain a telegram bot token](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)
|
23
23
|
|
24
24
|
To run it, you'll need some env variables set.
|
25
|
-
| Variable Name
|
26
|
-
| -------------
|
27
|
-
| RTT_AWS_ACCESS_KEY_ID
|
28
|
-
| RTT_AWS_DOMAIN_NAME
|
29
|
-
| RTT_AWS_REGION
|
30
|
-
| RTT_AWS_SECRET_ACCESS_KEY
|
31
|
-
| RTT_GOOGLE_API_KEY
|
32
|
-
| RTT_MAX_STORED_POSTS
|
33
|
-
| RTT_REDDIT_CLIENT_ID
|
34
|
-
| RTT_REDDIT_CLIENT_SECRET
|
35
|
-
| RTT_STORE_TYPE
|
36
|
-
| RTT_TELEGRAM_BOT_TOKEN
|
37
|
-
|
|
25
|
+
| Variable Name | Description | Required |
|
26
|
+
| ------------- | ----------- | -------- |
|
27
|
+
| RTT_AWS_ACCESS_KEY_ID | Your AWS access key ID. Needed for AWS SimpleDB storage | No |
|
28
|
+
| RTT_AWS_DOMAIN_NAME | Domain name to use for SimpleDB | No |
|
29
|
+
| RTT_AWS_REGION | AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions. | No |
|
30
|
+
| RTT_AWS_SECRET_ACCESS_KEY | Your AWS access key ID. Needed for AWS SimpleDB storage. | No |
|
31
|
+
| RTT_GOOGLE_API_KEY | Your Google API key to translate posts via Google Translate. | No |
|
32
|
+
| RTT_MAX_STORED_POSTS | Number of posts to store in the database to avoid duplicates, default is 25. | No |
|
33
|
+
| RTT_REDDIT_CLIENT_ID | Reddit app credentials to access API. Reddit allows more authenticated requests. | No |
|
34
|
+
| RTT_REDDIT_CLIENT_SECRET | Reddit app credentials to access API. Reddit allows more authenticated requests. | No |
|
35
|
+
| RTT_STORE_TYPE | Choose between `aws_simple_db`, `memory` or `temp_file`. Default is `aws_simple_db`, so if you're not specifying your AWS credentials, you have to choose another store type. | No |
|
36
|
+
| RTT_TELEGRAM_BOT_TOKEN | The token you've received when you've created a telegram bot. | Yes |
|
37
|
+
| RTT_TELEGRAM_ERROR_CHANNEL_ID | Telegram channel to send errors to (without `@`, only errors from Telegram API responses would be sent for now) | No |
|
38
|
+
| RTT_TEMP_DIR | Directory to write temp files to without trailing `/` | No |
|
38
39
|
|
39
40
|
|
40
41
|
You can also set them dynamically:
|
@@ -35,8 +35,10 @@ module RedditToTelegram
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def format_image_post(data)
|
38
|
+
post_type = data["url"]&.split(".")&.last == "gif" ? :gif : :image
|
39
|
+
|
38
40
|
base_post_format_attrs(data).merge(
|
39
|
-
{ type:
|
41
|
+
{ type: post_type,
|
40
42
|
media: data["url"] }
|
41
43
|
)
|
42
44
|
end
|
@@ -52,10 +54,17 @@ module RedditToTelegram
|
|
52
54
|
end
|
53
55
|
|
54
56
|
def format_gallery_post(data)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
gallery_links = prepare_gallery_links(data)
|
58
|
+
|
59
|
+
gallery_attrs = {
|
60
|
+
type: :gallery,
|
61
|
+
media: gallery_links.first(10)
|
62
|
+
}
|
63
|
+
|
64
|
+
remaining_links = gallery_links.drop(10)
|
65
|
+
gallery_attrs[:additional_media] = remaining_links unless remaining_links.empty?
|
66
|
+
|
67
|
+
base_post_format_attrs(data).merge(gallery_attrs)
|
59
68
|
end
|
60
69
|
|
61
70
|
def format_video_post(data)
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../post"
|
4
|
+
|
5
|
+
module RedditToTelegram
|
6
|
+
module Telegram
|
7
|
+
class Post
|
8
|
+
class Gallery
|
9
|
+
class << self
|
10
|
+
def push_remaining_gallery_data(post, channel, res, opts = {})
|
11
|
+
if post[:additional_media]
|
12
|
+
push_remaining_gallery_images(post, channel, opts)
|
13
|
+
else
|
14
|
+
push_gallery_caption(post, channel, res, opts)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def push_remaining_gallery_images(post, channel,opts = {})
|
21
|
+
post[:media] = post[:additional_media].first(10)
|
22
|
+
remaining = post.delete(:additional_media).drop(10)
|
23
|
+
post[:additional_media] = remaining unless remaining.empty?
|
24
|
+
Post.push(post, channel, opts)
|
25
|
+
end
|
26
|
+
|
27
|
+
def push_gallery_caption(post, channel, res, opts = {})
|
28
|
+
Post.push(
|
29
|
+
{ type: :text,
|
30
|
+
id: post[:id],
|
31
|
+
text: post[:text] },
|
32
|
+
channel,
|
33
|
+
opts.merge(gallery_caption_opts(res, opts))
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
def gallery_caption_opts(res)
|
38
|
+
gallery_caption_options = { disable_link_preview: true }
|
39
|
+
reply_to = res.dig("result", 0, "message_id")
|
40
|
+
return gallery_caption_options if reply_to.nil?
|
41
|
+
|
42
|
+
gallery_caption_options[:reply_to] = reply_to
|
43
|
+
gallery_caption_options
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "httparty"
|
4
|
+
require_relative "post/gallery"
|
4
5
|
require_relative "prepare_request"
|
5
6
|
require_relative "video"
|
6
7
|
require_relative "../variables"
|
@@ -16,6 +17,7 @@ module RedditToTelegram
|
|
16
17
|
METHOD_MAP = {
|
17
18
|
image: :photo,
|
18
19
|
gallery: :mediagroup,
|
20
|
+
gif: :animation,
|
19
21
|
text: :message,
|
20
22
|
video: :video
|
21
23
|
}.freeze
|
@@ -27,9 +29,7 @@ module RedditToTelegram
|
|
27
29
|
**params(post, channel, opts)
|
28
30
|
)
|
29
31
|
|
30
|
-
|
31
|
-
Video.delete_file if post[:type] == :video && post.dig(:misc)&.dig(:binary)
|
32
|
-
res
|
32
|
+
handle_response(post, channel, res, opts)
|
33
33
|
end
|
34
34
|
|
35
35
|
private
|
@@ -45,6 +45,31 @@ module RedditToTelegram
|
|
45
45
|
pars[:multipart] = true if binary
|
46
46
|
pars
|
47
47
|
end
|
48
|
+
|
49
|
+
def handle_response(post, channel, res, opts = {})
|
50
|
+
push_error(post, channel, res, opts) unless res["ok"] || opts[:no_retry]
|
51
|
+
Gallery.push_remaining_gallery_data(post, channel, res, opts) if post[:type] == :gallery
|
52
|
+
Video.delete_file if post[:type] == :video && post.dig(:misc)&.dig(:binary)
|
53
|
+
res
|
54
|
+
end
|
55
|
+
|
56
|
+
def push_error(post, channel, res, opts = {})
|
57
|
+
return if Variables.telegram.error_channel_id.to_s.empty?
|
58
|
+
|
59
|
+
push(
|
60
|
+
{
|
61
|
+
type: :text,
|
62
|
+
id: post[:id],
|
63
|
+
text: "Channel: @#{channel}\n\nResponse: #{res}"
|
64
|
+
},
|
65
|
+
Variables.telegram.error_channel_id,
|
66
|
+
opts.merge(
|
67
|
+
add_reddit_link: true,
|
68
|
+
disable_link_preview: true,
|
69
|
+
no_retry: true
|
70
|
+
)
|
71
|
+
)
|
72
|
+
end
|
48
73
|
end
|
49
74
|
end
|
50
75
|
end
|
@@ -8,11 +8,22 @@ module RedditToTelegram
|
|
8
8
|
class PrepareRequest
|
9
9
|
class << self
|
10
10
|
def body(post, chat_id, opts = {})
|
11
|
+
body = prepare_body(post, chat_id, opts)
|
12
|
+
body[:link_preview_options] = { is_disabled: true } if opts[:disable_link_preview]
|
13
|
+
body[:reply_parameters] = { message_id: opts[:reply_to] } if opts[:reply_to]
|
14
|
+
body
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def prepare_body(post, chat_id, opts = {})
|
11
20
|
case post[:type]
|
12
21
|
when :image
|
13
22
|
{ chat_id: "@#{chat_id}", photo: post[:media], caption: prepare_text(post, chat_id, opts) }
|
14
23
|
when :gallery
|
15
24
|
{ chat_id: "@#{chat_id}", media: prepare_gallery_media(post), caption: prepare_text(post, chat_id, opts) }
|
25
|
+
when :gif
|
26
|
+
{ chat_id: "@#{chat_id}", animation: post[:media], caption: prepare_text(post, chat_id, opts) }
|
16
27
|
when :text
|
17
28
|
{ chat_id: "@#{chat_id}", text: prepare_text(post, chat_id, opts) }
|
18
29
|
when :video
|
@@ -26,8 +37,6 @@ module RedditToTelegram
|
|
26
37
|
end
|
27
38
|
end
|
28
39
|
|
29
|
-
private
|
30
|
-
|
31
40
|
def prepare_text(post, chat_id, opts = {})
|
32
41
|
text = post[:text]
|
33
42
|
|
@@ -104,11 +104,15 @@ module RedditToTelegram
|
|
104
104
|
|
105
105
|
class Telegram
|
106
106
|
class << self
|
107
|
-
attr_writer :bot_token
|
107
|
+
attr_writer :bot_token, :error_channel_id
|
108
108
|
|
109
109
|
def bot_token
|
110
110
|
@bot_token ||= ENV["RTT_TELEGRAM_BOT_TOKEN"]
|
111
111
|
end
|
112
|
+
|
113
|
+
def error_channel_id
|
114
|
+
@error_channel_id ||= ENV["RTT_TELEGRAM_ERROR_CHANNEL_ID"]
|
115
|
+
end
|
112
116
|
end
|
113
117
|
end
|
114
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reddit-to-telegram
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Tityuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-simpledb
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- ".tool-versions"
|
64
64
|
- Gemfile
|
65
65
|
- Gemfile.lock
|
66
|
+
- LICENSE
|
66
67
|
- README.md
|
67
68
|
- lib/reddit-to-telegram.rb
|
68
69
|
- lib/reddit_to_telegram/errors.rb
|
@@ -77,6 +78,7 @@ files:
|
|
77
78
|
- lib/reddit_to_telegram/store/memory.rb
|
78
79
|
- lib/reddit_to_telegram/store/temp_file.rb
|
79
80
|
- lib/reddit_to_telegram/telegram/post.rb
|
81
|
+
- lib/reddit_to_telegram/telegram/post/gallery.rb
|
80
82
|
- lib/reddit_to_telegram/telegram/prepare_request.rb
|
81
83
|
- lib/reddit_to_telegram/telegram/video.rb
|
82
84
|
- lib/reddit_to_telegram/variables.rb
|