reddit-to-telegram 0.2.1 → 0.3.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: 1ec45bcf41a275baa3ea712968010d520b4af973a78149837ebe4cbc0b4c3870
4
- data.tar.gz: 328805f68a80798c6c9c3a26fbd00aac37560ac97d55b601df9471b0587c2a31
3
+ metadata.gz: e6c7b26fa99c0cd58ee1e6bc13e4bc357d20aa30c3efbc4ca1df02a75948afbc
4
+ data.tar.gz: '00183a34218ed5e32012af4b7614438bec200ecf1443cb3a5f4db649aa8dac19'
5
5
  SHA512:
6
- metadata.gz: 7184c7bddf2027cfef0bd40ddd90a044e309482b1467d1ab942114d43d4b6c4b7c03d2081a06035ba4b408d82aa19c64ba28d74d5ef98c03f58399153e61dbe3
7
- data.tar.gz: e46c030bd186fbef1185970769d8d9263849e2b8048c043ca5b7ffa28c9a5b7d4e980fde260ee705ae2358419c7c581ebba458e620c68c3742541bc511dbdaae
6
+ metadata.gz: 1f5a3a2c3d03f37ff932245225e84c7ffd35c79051db95816598323456e95fa88ab710f66f1b01e9927b897db4df2227cc1a44673431e0eee655be8f5bde897a
7
+ data.tar.gz: 43a98d11dc600a57ede04ff2d6279373a3965be64b27084d761e9ffc3f943f4e94147e0f0293504d33ad6ae857ef4cb117f1a0c53964157b533f4240efe62d63
data/.rubocop.yml CHANGED
@@ -12,3 +12,6 @@ Style/SingleArgumentDig:
12
12
 
13
13
  Style/StringLiterals:
14
14
  EnforcedStyle: double_quotes
15
+
16
+ Style/RaiseArgs:
17
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- reddit-to-telegram (0.0.0)
4
+ reddit-to-telegram (0.2.1)
5
5
  aws-sdk-simpledb
6
6
  httparty
7
7
 
data/README.md CHANGED
@@ -21,32 +21,27 @@ Or `gem install reddit-to-telegram`, but don't forget to `require` it first then
21
21
  - (Optionally) Create a Reddit app, which would allow more requests to reddit
22
22
  - [Obtain](https://core.telegram.org/bots/tutorial#obtain-your-bot-token) a telegram bot token
23
23
 
24
- ## Installation
25
24
  To run it, you'll need some env variables set.
26
25
  ```
27
26
  RTT_AWS_ACCESS_KEY_ID= # Your AWS access key ID. Needed for AWS SimpleDB storage.
27
+ RTT_AWS_DOMAIN_NAME= # (Optional) Domain name to use for SimpleDB
28
28
  RTT_AWS_REGION= # AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions.
29
29
  RTT_AWS_SECRET_ACCESS_KEY= # Your AWS access key ID. Needed for AWS SimpleDB storage.
30
30
  RTT_MAX_STORED_POSTS= # (Optional) Number of posts to store in the database to avoid duplicates, default is 25.
31
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
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
33
34
  RTT_TELEGRAM_BOT_TOKEN= # The token you've received when you've created a telegram bot.
34
35
  RTT_TEMP_DIR= (Optional) # Directory to write temp files to without trailing /
35
36
  ```
36
37
 
37
38
  You can also set them dynamically:
38
39
  ```
39
- RedditToTelegram::Vars.assign_values(
40
- max_stored_posts:,
41
- tmp_dir:
42
- aws_access_key_id:,
43
- aws_secret_access_key:,
44
- aws_region:,
45
- reddit_client_id:,
46
- reddit_client_secret:,
47
- telegram_bot_token:
48
- )
40
+ RedditToTelegram::Variables.aws.aws_access_key_id =
41
+ RedditToTelegram::Variables.telegram.bot_token =
49
42
  ```
43
+ Check out `lib/variables` for list of all available variables.
44
+
50
45
  ## Usage
51
46
 
52
47
  1. Add the bot as administrator to Telegram channels you'd like to post to.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pry"
4
+
3
5
  require_relative "reddit_to_telegram/reddit/fetch"
4
6
  require_relative "reddit_to_telegram/store"
5
7
  require_relative "reddit_to_telegram/telegram/post"
@@ -18,6 +20,8 @@ module RedditToTelegram
18
20
  end
19
21
  end
20
22
 
23
+ private
24
+
21
25
  def handle_res(res, subreddit, telegram_chat_id)
22
26
  return if res.nil?
23
27
 
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedditToTelegram
4
+ class RedditToTelegramError < StandardError; end
5
+ class InvalidStoreType < RedditToTelegramError; end
6
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "httparty"
4
- require_relative "../vars"
4
+ require_relative "../variables"
5
5
 
6
6
  module RedditToTelegram
7
7
  module Reddit
@@ -18,7 +18,7 @@ module RedditToTelegram
18
18
  URI,
19
19
  body: PARAMS,
20
20
  headers: HEADERS,
21
- basic_auth: { username: Vars::Reddit.client_id, password: Vars::Reddit.client_secret }
21
+ basic_auth: { username: Variables.reddit.client_id, password: Variables.reddit.client_secret }
22
22
  )["access_token"]
23
23
  end
24
24
  end
@@ -4,7 +4,7 @@ require "httparty"
4
4
  require_relative "auth"
5
5
  require_relative "output"
6
6
  require_relative "../store"
7
- require_relative "../vars"
7
+ require_relative "../variables"
8
8
 
9
9
  module RedditToTelegram
10
10
  module Reddit
@@ -12,7 +12,7 @@ module RedditToTelegram
12
12
  include HTTParty
13
13
 
14
14
  BASE_URI = "https://oauth.reddit.com/r"
15
- QUERY = { g: "GLOBAL", limit: Vars.max_stored_posts }.freeze
15
+ QUERY = { g: "GLOBAL", limit: Variables.store.max_stored_posts }.freeze
16
16
  BASE_HEADERS = {
17
17
  "Content-Type" => "application/json",
18
18
  "Accept" => "application/json"
@@ -29,6 +29,8 @@ module RedditToTelegram
29
29
  handle_response(res, subreddit, retries_left)
30
30
  end
31
31
 
32
+ private
33
+
32
34
  def handle_response(res, subreddit, retries_left)
33
35
  case res.code
34
36
  when 401
@@ -10,6 +10,8 @@ module RedditToTelegram
10
10
  posts.map { |post| format_post(post) }.compact
11
11
  end
12
12
 
13
+ private
14
+
13
15
  def format_post(post)
14
16
  data = post["data"]
15
17
  if data["post_hint"] == "image"
@@ -2,19 +2,28 @@
2
2
 
3
3
  require "aws-sdk-simpledb"
4
4
  require "json"
5
- require_relative "../vars"
5
+ require_relative "../variables"
6
6
 
7
7
  module RedditToTelegram
8
8
  module Store
9
9
  class AWSSimpleDB
10
- DOMAIN_NAME = "reddit_to_telegram"
11
10
  ITEM_NAME = "cached_data"
12
11
 
13
12
  class << self
13
+ def client
14
+ @client ||= Aws::SimpleDB::Client.new(
15
+ access_key_id: Variables.aws.access_key_id,
16
+ secret_access_key: Variables.aws.secret_access_key,
17
+ region: Variables.aws.region
18
+ )
19
+ end
20
+
21
+ private
22
+
14
23
  attr_reader :reddit_token
15
24
 
16
25
  def setup
17
- create_domain unless client.list_domains.domain_names.include?(DOMAIN_NAME)
26
+ create_domain unless client.list_domains.domain_names.include?(Variables.aws.domain_name)
18
27
  read_db
19
28
  end
20
29
 
@@ -36,18 +45,10 @@ module RedditToTelegram
36
45
  @posts[subreddit].include?(id)
37
46
  end
38
47
 
39
- def client
40
- @client ||= Aws::SimpleDB::Client.new(
41
- access_key_id: Vars::AWS.access_key_id,
42
- secret_access_key: Vars::AWS.secret_access_key,
43
- region: Vars::AWS.region
44
- )
45
- end
46
-
47
48
  def read_db
48
49
  res = client.get_attributes(
49
50
  {
50
- domain_name: "reddit_to_telegram",
51
+ domain_name: Variables.aws.domain_name,
51
52
  item_name: "cached_data",
52
53
  consistent_read: true
53
54
  }
@@ -69,7 +70,7 @@ module RedditToTelegram
69
70
  def write_db
70
71
  client.put_attributes(
71
72
  {
72
- domain_name: DOMAIN_NAME,
73
+ domain_name: Variables.aws.domain_name,
73
74
  item_name: ITEM_NAME,
74
75
  attributes: prepare_db_attrs
75
76
  }
@@ -101,9 +102,9 @@ module RedditToTelegram
101
102
  res = client.list_domains
102
103
  return unless res.successful?
103
104
 
104
- return if res.domain_names.include?(DOMAIN_NAME)
105
+ return if res.domain_names.include?(Variables.aws.domain_name)
105
106
 
106
- client.create_domain({ domain_name: DOMAIN_NAME })
107
+ client.create_domain({ domain_name: Variables.aws.domain_name })
107
108
  end
108
109
  end
109
110
  end
@@ -4,6 +4,8 @@ module RedditToTelegram
4
4
  module Store
5
5
  class Memory
6
6
  class << self
7
+ private
8
+
7
9
  attr_accessor :reddit_token
8
10
 
9
11
  @posts = {}
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
- require_relative "../vars"
4
+ require_relative "../variables"
5
5
 
6
6
  module RedditToTelegram
7
7
  module Store
8
8
  class TempFile
9
9
  class << self
10
+ private
11
+
10
12
  attr_reader :reddit_token, :posts
11
13
 
12
14
  @reddit_token = nil
@@ -60,7 +62,7 @@ module RedditToTelegram
60
62
  end
61
63
 
62
64
  def temp_file_path
63
- "#{Vars.tmp_dir}/store.json"
65
+ "#{Variables.store.tmp_dir}/store.json"
64
66
  end
65
67
  end
66
68
  end
@@ -1,26 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "errors"
3
4
  require_relative "store/aws_simple_db"
4
5
  require_relative "store/memory"
5
6
  require_relative "store/temp_file"
7
+ require_relative "variables"
6
8
 
7
9
  module RedditToTelegram
8
10
  module Store
9
- MAX_STORED_POSTS = Vars.max_stored_posts ? Vars.max_stored_posts - 1 : 24
10
- STORE_MAP = {
11
+ MAX_STORED_POSTS = Variables.store.max_stored_posts - 1
12
+ CLASS_MAP = {
11
13
  aws_simple_db: "RedditToTelegram::Store::AWSSimpleDB",
12
14
  memory: "RedditToTelegram::Store::Memory",
13
15
  temp_file: "RedditToTelegram::Store::TempFile"
14
16
  }.freeze
15
- DEFAULT_STORE = :aws_simple_db
16
17
 
17
18
  STORE = Object.const_get("RedditToTelegram::Store::AWSSimpleDB")
18
19
 
19
20
  class << self
20
21
  attr_accessor :active
21
22
 
22
- def setup(selected_store = DEFAULT_STORE)
23
- self.active = Object.const_get(STORE_MAP[selected_store.to_sym])
23
+ def setup
24
+ raise(InvalidStoreType.new) unless CLASS_MAP.keys.include?(Variables.store.type)
25
+
26
+ self.active = Object.const_get(CLASS_MAP[RedditToTelegram::Variables.store.type])
24
27
  active.send(:setup)
25
28
  end
26
29
  end
@@ -3,6 +3,7 @@
3
3
  require "httparty"
4
4
  require_relative "prepare_request"
5
5
  require_relative "video"
6
+ require_relative "../variables"
6
7
 
7
8
  module RedditToTelegram
8
9
  module Telegram
@@ -22,14 +23,16 @@ module RedditToTelegram
22
23
  class << self
23
24
  def push(post, channel)
24
25
  HTTParty.post(
25
- "#{BASE_URI}#{Vars::Telegram.bot_token}/send#{METHOD_MAP[post[:type]]}",
26
+ "#{BASE_URI}#{Variables.telegram.bot_token}/send#{METHOD_MAP[post[:type]]}",
26
27
  **params(post, channel)
27
28
  )
28
29
 
29
30
  push({ type: :text, id: post[:id], text: post[:text] }, channel) if post[:type] == :gallery
30
- Video.delete_file if post[:type] == :video
31
+ Video.delete_file if post[:type] == :video && post.dig(:misc)&.dig(:binary)
31
32
  end
32
33
 
34
+ private
35
+
33
36
  def params(post, channel)
34
37
  binary = post.dig(:misc)&.dig(:binary)
35
38
  body = PrepareRequest.body(post, channel)
@@ -19,6 +19,8 @@ module RedditToTelegram
19
19
  end
20
20
  end
21
21
 
22
+ private
23
+
22
24
  def prepare_text(post, chat_id)
23
25
  id = post[:id].split("_")[1]
24
26
  "#{post[:text]}\n\nhttps://redd.it/#{id}\n@#{chat_id}"
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open-uri"
4
- require_relative "../vars"
4
+ require_relative "../variables"
5
5
 
6
6
  module RedditToTelegram
7
7
  module Telegram
@@ -23,7 +23,7 @@ module RedditToTelegram
23
23
  end
24
24
 
25
25
  def temp_video_path
26
- "#{Vars.tmp_dir}/video.mp4"
26
+ "#{Variables.store.tmp_dir}/video.mp4"
27
27
  end
28
28
  end
29
29
  end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RedditToTelegram
4
+ module Variables
5
+ class << self
6
+ def aws
7
+ RedditToTelegram::Variables::AWS
8
+ end
9
+
10
+ def reddit
11
+ RedditToTelegram::Variables::Reddit
12
+ end
13
+
14
+ def store
15
+ RedditToTelegram::Variables::Store
16
+ end
17
+
18
+ def telegram
19
+ RedditToTelegram::Variables::Telegram
20
+ end
21
+ end
22
+
23
+ class Store
24
+ DEFAULT_TMP_DIR = "#{Dir.pwd}/tmp".freeze
25
+ DEFAULT_TYPE = :aws_simple_db
26
+
27
+ class << self
28
+ attr_writer :max_stored_posts, :tmp_dir, :type
29
+
30
+ def max_stored_posts
31
+ @max_stored_posts ||= ENV["RTT_MAX_STORED_POSTS"].to_i || 50
32
+ end
33
+
34
+ def tmp_dir
35
+ @tmp_dir ||= ENV["RTT_TEMP_DIR"] || DEFAULT_TMP_DIR
36
+ end
37
+
38
+ def type
39
+ @type ||= ENV["RTT_STORE_TYPE"]&.to_sym || DEFAULT_TYPE
40
+ end
41
+ end
42
+ end
43
+
44
+ class AWS
45
+ DEFAULT_DOMAIN_NAME = "reddit_to_telegram"
46
+
47
+ class << self
48
+ attr_writer :access_key_id, :secret_access_key, :region, :domain_name
49
+
50
+ def access_key_id
51
+ @access_key_id ||= ENV["RTT_AWS_ACCESS_KEY_ID"]
52
+ end
53
+
54
+ def secret_access_key
55
+ @secret_access_key ||= ENV["RTT_AWS_SECRET_ACCESS_KEY"]
56
+ end
57
+
58
+ def region
59
+ @region ||= ENV["RTT_AWS_REGION"]
60
+ end
61
+
62
+ def domain_name
63
+ @domain_name ||= ENV["RTT_AWS_DOMAIN_NAME"] || DEFAULT_DOMAIN_NAME
64
+ end
65
+ end
66
+ end
67
+
68
+ class Reddit
69
+ class << self
70
+ attr_writer :client_id, :client_secret
71
+
72
+ def client_id
73
+ @client_id ||= ENV["RTT_REDDIT_CLIENT_ID"]
74
+ end
75
+
76
+ def client_secret
77
+ @client_secret ||= ENV["RTT_REDDIT_CLIENT_SECRET"]
78
+ end
79
+ end
80
+ end
81
+
82
+ class Telegram
83
+ class << self
84
+ attr_writer :bot_token
85
+
86
+ def bot_token
87
+ @bot_token ||= ENV["RTT_TELEGRAM_BOT_TOKEN"]
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedditToTelegram
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Tityuk
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-26 00:00:00.000000000 Z
11
+ date: 2024-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-simpledb
@@ -65,6 +65,7 @@ files:
65
65
  - Gemfile.lock
66
66
  - README.md
67
67
  - lib/reddit-to-telegram.rb
68
+ - lib/reddit_to_telegram/errors.rb
68
69
  - lib/reddit_to_telegram/reddit/auth.rb
69
70
  - lib/reddit_to_telegram/reddit/fetch.rb
70
71
  - lib/reddit_to_telegram/reddit/output.rb
@@ -75,7 +76,7 @@ files:
75
76
  - lib/reddit_to_telegram/telegram/post.rb
76
77
  - lib/reddit_to_telegram/telegram/prepare_request.rb
77
78
  - lib/reddit_to_telegram/telegram/video.rb
78
- - lib/reddit_to_telegram/vars.rb
79
+ - lib/reddit_to_telegram/variables.rb
79
80
  - lib/reddit_to_telegram/version.rb
80
81
  - reddit-to-telegram.gemspec
81
82
  - tmp/.keep
@@ -83,7 +84,7 @@ homepage: https://github.com/dersnek/reddit-to-telegram
83
84
  licenses:
84
85
  - MIT
85
86
  metadata: {}
86
- post_install_message:
87
+ post_install_message:
87
88
  rdoc_options: []
88
89
  require_paths:
89
90
  - lib
@@ -99,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  version: '0'
100
101
  requirements: []
101
102
  rubygems_version: 3.4.10
102
- signing_key:
103
+ signing_key:
103
104
  specification_version: 4
104
105
  summary: Fetches hot posts from reddit and pushes them to telegram
105
106
  test_files: []
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RedditToTelegram
4
- module Vars
5
- DEFAULT_TMP_DIR = "#{Dir.pwd}/tmp".freeze
6
-
7
- class << self
8
- # rubocop:disable Metrics/ParameterLists
9
- def assign_values(
10
- max_stored_posts:,
11
- tmp_dir:,
12
- aws_access_key_id:,
13
- aws_secret_access_key:,
14
- aws_region:,
15
- reddit_client_id:,
16
- reddit_client_secret:,
17
- telegram_bot_token:
18
- )
19
-
20
- @max_stored_posts = max_stored_posts
21
- @tmp_dir = tmp_dir
22
- AWS.instance_variable_set(:@access_key_id, aws_access_key_id)
23
- AWS.instance_variable_set(:@secret_access_key, aws_secret_access_key)
24
- AWS.instance_variable_set(:@region, aws_region)
25
- Reddit.instance_variable_set(:@client_id, reddit_client_id)
26
- Reddit.instance_variable_set(:@client_secret, reddit_client_secret)
27
- Telegram.instance_variable_set(:@bot_token, telegram_bot_token)
28
- end
29
- # rubocop:enable Metrics/ParameterLists
30
-
31
- def max_stored_posts
32
- @max_stored_posts ||= ENV["RTT_MAX_STORED_POSTS"].to_i || 25
33
- end
34
-
35
- def tmp_dir
36
- @tmp_dir ||= ENV["RTT_TEMP_DIR"] || DEFAULT_TMP_DIR
37
- end
38
- end
39
-
40
- class AWS
41
- class << self
42
- def access_key_id
43
- @access_key_id ||= ENV["RTT_AWS_ACCESS_KEY_ID"]
44
- end
45
-
46
- def secret_access_key
47
- @secret_access_key ||= ENV["RTT_AWS_SECRET_ACCESS_KEY"]
48
- end
49
-
50
- def region
51
- @region ||= ENV["RTT_AWS_REGION"]
52
- end
53
- end
54
- end
55
-
56
- class Reddit
57
- class << self
58
- def client_id
59
- @client_id ||= ENV["RTT_REDDIT_CLIENT_ID"]
60
- end
61
-
62
- def client_secret
63
- @client_secret ||= ENV["RTT_REDDIT_CLIENT_SECRET"]
64
- end
65
- end
66
- end
67
-
68
- class Telegram
69
- class << self
70
- def bot_token
71
- @bot_token ||= ENV["RTT_TELEGRAM_BOT_TOKEN"]
72
- end
73
- end
74
- end
75
- end
76
- end