reddit-to-telegram 0.5.1 → 0.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: 3666d02e34ba14d6a7030a0e8cd11a6c6de74b71b6062e7df34e4856c5a7f98c
4
- data.tar.gz: 8423e5fdb9fd749e06c1a672d36078824d5016bccaec15450a42fc3fc682417e
3
+ metadata.gz: 3cd7fa85cc2779888fb3ea2a3f778e0875ed0393021774c35c50d8e3c74b99bb
4
+ data.tar.gz: 2fe6776eea5999edb03316b26d664f084c7405664cac2b539c40254ed8632cbb
5
5
  SHA512:
6
- metadata.gz: a5afa435363aaf50e0fdbee4fa878d752f99889c830dd842e1cae957db1d3ac362ba681f79158095d16a439626c423bbb2b895164bfc87685c87250cf38ca8f7
7
- data.tar.gz: 0a72c0f8759303c1647dc3871e45349ae103ebfb099f40db32ddfd0cca1214eae5a74ec0ecf2b3f942623b98289ad0547c0c58e821780cfbce596238c9664c23
6
+ metadata.gz: 85f2191d33d580a8692b92e5d30c5a4e883f8e39e47f4a30054f95e971e7c26bf79b52770318a9cb9ad07dcad95a45b3531ba791dfe161c9bb31c4a0b289c63a
7
+ data.tar.gz: 963e0e776365e757fe051f33d91211a2189c6f0a1fd166964937a965ae93e8776e1bd4b6f1e1243a140d0530653b683d446cf6b562082eb90dc4dcaf929eb2e4
data/README.md CHANGED
@@ -22,18 +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
- ```
26
- RTT_AWS_ACCESS_KEY_ID= # (Optional) Your AWS access key ID. Needed for AWS SimpleDB storage.
27
- RTT_AWS_DOMAIN_NAME= # (Optional) Domain name to use for SimpleDB
28
- RTT_AWS_REGION= # (Optional) AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions.
29
- RTT_AWS_SECRET_ACCESS_KEY= # (Optional) Your AWS access key ID. Needed for AWS SimpleDB storage.
30
- RTT_MAX_STORED_POSTS= # (Optional) Number of posts to store in the database to avoid duplicates, default is 25.
31
- RTT_REDDIT_CLIENT_ID= # Reddit app credentials to access API. Might not be needed depending on setup, reddit allows some requests without authentication.
32
- RTT_REDDIT_CLIENT_SECRET= # Reddit app credentials to access API. Might not be needed depending on setup, reddit allows some requests without authentication.
33
- RTT_STORE_TYPE= # (Optional) 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 specify another store type.
34
- RTT_TELEGRAM_BOT_TOKEN= # The token you've received when you've created a telegram bot.
35
- RTT_TEMP_DIR= (Optional) # Directory to write temp files to without trailing /
36
- ```
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_TEMP_DIR | Directory to write temp files to without trailing `/` | No |
38
+
37
39
 
38
40
  You can also set them dynamically:
39
41
  ```
@@ -57,3 +59,21 @@ Or to push one specific post (the only thing you need to set up for this is your
57
59
  RedditToTelegram.single("regular_link_to_post", :telegram_channel_id)
58
60
  ```
59
61
  Use `:telegram_channel_id` without the `@`.
62
+
63
+ ### Options
64
+
65
+ Translate option is supported. You will have to set up Google Translate API key and add it to env. You can find available languages in [Google Translate docs](https://cloud.google.com/translate/docs/languages).
66
+ ```
67
+ RedditToTelegram.hot(
68
+ { subreddit_name_1: :telegram_channel_id_1 },
69
+ translate: :ja
70
+ )
71
+ ```
72
+ You can also specify if you want to add reddit link or telegram channel handle to the post text. By default they won't be added.
73
+ ```
74
+ RedditToTelegram.hot(
75
+ { subreddit_name_1: :telegram_channel_id_1 },
76
+ add_reddit_link: true,
77
+ add_channel_handle: true
78
+ )
79
+ ```
@@ -8,36 +8,37 @@ require_relative "variables"
8
8
  module RedditToTelegram
9
9
  class Post
10
10
  class << self
11
- def hot(sources)
11
+ def hot(sources, opts = {})
12
12
  return if sources.empty?
13
13
 
14
14
  Store.setup
15
15
 
16
16
  sources.each do |subreddit, telegram_chat_id|
17
17
  res = Reddit::Fetch.hot(subreddit)
18
- handle_res(res, subreddit, telegram_chat_id)
18
+ handle_res(res, subreddit, telegram_chat_id, opts)
19
19
  end
20
20
  end
21
21
 
22
- def single(link, telegram_chat_id)
22
+ def single(link, telegram_chat_id, opts = {})
23
23
  return if link.empty?
24
24
 
25
25
  Variables.store.type = :memory
26
26
  Store.setup
27
27
 
28
28
  res = Reddit::Fetch.post(link)
29
- Telegram::Post.push(res, telegram_chat_id)
29
+ Telegram::Post.push(res, telegram_chat_id, opts)
30
+ res
30
31
  end
31
32
 
32
33
  private
33
34
 
34
- def handle_res(res, subreddit, telegram_chat_id)
35
+ def handle_res(res, subreddit, telegram_chat_id, opts = {})
35
36
  return if res.nil?
36
37
 
37
38
  post = find_new_post(subreddit, res)
38
39
  return if post.nil?
39
40
 
40
- res = Telegram::Post.push(post, telegram_chat_id)
41
+ res = Telegram::Post.push(post, telegram_chat_id, opts)
41
42
  Store::Posts.add(subreddit, post[:id])
42
43
  res
43
44
  end
@@ -33,12 +33,15 @@ module RedditToTelegram
33
33
 
34
34
  def post(link, retries_left = 5)
35
35
  headers = BASE_HEADERS.merge("Authorization" => "Bearer #{Store::Reddit.token}")
36
+ link = link.gsub("www", "oauth") if link.match(/www.reddit.com/)
37
+ link += ".json" unless link.match(/.json/)
38
+
36
39
  res = HTTParty.get(
37
- "#{link.gsub('www', 'oauth')}.json",
40
+ link,
38
41
  query: QUERY_FOR_POST,
39
42
  headers:
40
43
  )
41
- handle_response(res, post: retries_left)
44
+ handle_response(res, post: [link, retries_left])
42
45
  end
43
46
 
44
47
  private
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httparty"
4
+ require_relative "../store"
5
+
6
+ module RedditToTelegram
7
+ module Services
8
+ class Translate
9
+ include HTTParty
10
+
11
+ BASE_URI = "https://translation.googleapis.com/language/translate/v2"
12
+ HEADERS = { "Content-Type" => "application/json; charset=utf-8", "Accept" => "application/json" }.freeze
13
+
14
+ class << self
15
+ def text(string, target_language)
16
+ check
17
+
18
+ res = HTTParty.post(
19
+ BASE_URI,
20
+ body: body(string, target_language),
21
+ headers: HEADERS.merge(
22
+ "X-goog-api-key" => Variables.google.api_key
23
+ )
24
+ )
25
+ res.dig("data", "translations")&.first&.dig("translatedText")
26
+ end
27
+
28
+ private
29
+
30
+ def check
31
+ return if Variables.google.set_up?
32
+
33
+ raise(MissingVariables.new("Missing Google credentials. Set them up or disable translation"))
34
+ end
35
+
36
+ def body(string, target_language)
37
+ {
38
+ "q" => [string],
39
+ "source" => "en",
40
+ "target" => target_language,
41
+ "format" => "text"
42
+ }.to_json
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -21,10 +21,10 @@ module RedditToTelegram
21
21
  }.freeze
22
22
 
23
23
  class << self
24
- def push(post, channel)
24
+ def push(post, channel, opts = {})
25
25
  res = HTTParty.post(
26
26
  "#{BASE_URI}#{Variables.telegram.bot_token}/send#{METHOD_MAP[post[:type]]}",
27
- **params(post, channel)
27
+ **params(post, channel, opts)
28
28
  )
29
29
 
30
30
  push({ type: :text, id: post[:id], text: post[:text] }, channel) if post[:type] == :gallery
@@ -34,9 +34,9 @@ module RedditToTelegram
34
34
 
35
35
  private
36
36
 
37
- def params(post, channel)
37
+ def params(post, channel, opts = {})
38
38
  binary = post.dig(:misc)&.dig(:binary)
39
- body = PrepareRequest.body(post, channel)
39
+ body = PrepareRequest.body(post, channel, opts)
40
40
 
41
41
  pars = {
42
42
  body: binary ? body : body.to_json,
@@ -1,35 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "video"
4
+ require_relative "../services/translate"
4
5
 
5
6
  module RedditToTelegram
6
7
  module Telegram
7
8
  class PrepareRequest
8
9
  class << self
9
- def body(post, chat_id)
10
+ def body(post, chat_id, opts = {})
10
11
  case post[:type]
11
12
  when :image
12
- { chat_id: "@#{chat_id}", photo: post[:media], caption: prepare_text(post, chat_id) }
13
+ { chat_id: "@#{chat_id}", photo: post[:media], caption: prepare_text(post, chat_id, opts) }
13
14
  when :gallery
14
- { chat_id: "@#{chat_id}", media: prepare_gallery_media(post), caption: prepare_text(post, chat_id) }
15
+ { chat_id: "@#{chat_id}", media: prepare_gallery_media(post), caption: prepare_text(post, chat_id, opts) }
15
16
  when :text
16
- { chat_id: "@#{chat_id}", text: prepare_text(post, chat_id) }
17
+ { chat_id: "@#{chat_id}", text: prepare_text(post, chat_id, opts) }
17
18
  when :video
18
19
  {
19
20
  chat_id: "@#{chat_id}",
20
21
  video: prepare_video(post),
21
22
  height: post[:misc][:video_height],
22
23
  width: post[:misc][:video_width],
23
- caption: prepare_text(post, chat_id)
24
+ caption: prepare_text(post, chat_id, opts)
24
25
  }
25
26
  end
26
27
  end
27
28
 
28
29
  private
29
30
 
30
- def prepare_text(post, chat_id)
31
- id = post[:id].split("_")[1]
32
- "#{post[:text]}\n\nhttps://redd.it/#{id}\n@#{chat_id}"
31
+ def prepare_text(post, chat_id, opts = {})
32
+ text = post[:text]
33
+
34
+ text = Services::Translate.text(text, opts[:translate]) if opts[:translate]
35
+
36
+ if opts[:add_reddit_link]
37
+ id = post[:id].split("_")[1]
38
+ text += "\n\nhttps://redd.it/#{id}"
39
+ end
40
+
41
+ if opts[:add_channel_handle]
42
+ text += opts[:add_reddit_link] ? "\n" : "\n\n"
43
+ text += "@#{chat_id}"
44
+ end
45
+
46
+ text
33
47
  end
34
48
 
35
49
  def prepare_gallery_media(post)
@@ -7,6 +7,10 @@ module RedditToTelegram
7
7
  RedditToTelegram::Variables::AWS
8
8
  end
9
9
 
10
+ def google
11
+ RedditToTelegram::Variables::Google
12
+ end
13
+
10
14
  def reddit
11
15
  RedditToTelegram::Variables::Reddit
12
16
  end
@@ -65,7 +69,21 @@ module RedditToTelegram
65
69
  end
66
70
 
67
71
  def set_up?
68
- ATTRS.all? { |a| !a.empty? }
72
+ ATTRS.all? { |a| !a.to_s.empty? }
73
+ end
74
+ end
75
+ end
76
+
77
+ class Google
78
+ class << self
79
+ attr_writer :api_key
80
+
81
+ def api_key
82
+ @api_key ||= ENV["RTT_GOOGLE_API_KEY"]
83
+ end
84
+
85
+ def set_up?
86
+ !api_key.to_s.empty?
69
87
  end
70
88
  end
71
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedditToTelegram
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  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.5.1
4
+ version: 0.6.0
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-03-05 00:00:00.000000000 Z
11
+ date: 2024-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-simpledb
@@ -71,6 +71,7 @@ files:
71
71
  - lib/reddit_to_telegram/reddit/fetch.rb
72
72
  - lib/reddit_to_telegram/reddit/output.rb
73
73
  - lib/reddit_to_telegram/reddit/output/imgur.rb
74
+ - lib/reddit_to_telegram/services/translate.rb
74
75
  - lib/reddit_to_telegram/store.rb
75
76
  - lib/reddit_to_telegram/store/aws_simple_db.rb
76
77
  - lib/reddit_to_telegram/store/memory.rb