reddit-to-telegram 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81b0c79fce533367384f2a5bce697b0fafa0e914f772ce23e7ac903aeeec1577
4
- data.tar.gz: aac0c0448ab4964df36acf62819e2c73e1d7f90d750091fa88b43a1605612580
3
+ metadata.gz: 3cd7fa85cc2779888fb3ea2a3f778e0875ed0393021774c35c50d8e3c74b99bb
4
+ data.tar.gz: 2fe6776eea5999edb03316b26d664f084c7405664cac2b539c40254ed8632cbb
5
5
  SHA512:
6
- metadata.gz: 1b0f0c32452b4784a50ed4f0557bf1347d84e0fb3f0bc6ba6af1baa0c2426f066283ad9c0a30f22e1232462c2aa2e45f44e637e942208548130152bf5611d081
7
- data.tar.gz: 54807fe38f1a4f03e6baff998dc517b1fd3835f435837e75e1ca23809df7b5edef768106a4477f43f9490af63d3df280273e420c153004760e7f19680856aa8b
6
+ metadata.gz: 85f2191d33d580a8692b92e5d30c5a4e883f8e39e47f4a30054f95e971e7c26bf79b52770318a9cb9ad07dcad95a45b3531ba791dfe161c9bb31c4a0b289c63a
7
+ data.tar.gz: 963e0e776365e757fe051f33d91211a2189c6f0a1fd166964937a965ae93e8776e1bd4b6f1e1243a140d0530653b683d446cf6b562082eb90dc4dcaf929eb2e4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- reddit-to-telegram (0.5.0)
4
+ reddit-to-telegram (0.5.1)
5
5
  aws-sdk-simpledb
6
6
  httparty
7
7
 
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
@@ -32,7 +32,7 @@ module RedditToTelegram
32
32
  def check_credentials
33
33
  return unless Variables.store.type == :aws_simple_db
34
34
 
35
- return if Variables.aws.all_present?
35
+ return if Variables.aws.set_up?
36
36
 
37
37
  raise(MissingVariables.new("Missing AWS credentials. "\
38
38
  "Set them up or change store type to anything other than aws_simple_db"))
@@ -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.0"
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.0
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