reddit-to-telegram 0.9.0 → 0.10.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: '05893e8e006a0f401dbcec8e91cfd46be3cf3b53bbc1c9e89ccd95620db3087c'
4
- data.tar.gz: 62ef80526ab1051177eafcab26de219b6d196262024c473dc53813fcbac32365
3
+ metadata.gz: 00436ff5f5d09c81d76f4d49f3cc3996e983857f1fbff2ad2db006ea014c0e96
4
+ data.tar.gz: ca6e585387a1165a9dacdee453d4853e531b727f9498918c97675d9484275b6c
5
5
  SHA512:
6
- metadata.gz: 4319bf2ff9cbe27433514492093865c55ee65a77ef6d1eaae1a89376ea37cf0b16a998856b8f5c7ad21f7442d4ca2aca9f710d74fd40a7def9d8859f429d0dee
7
- data.tar.gz: 58a917a293a78fdca894d5249114d26f19c79d27574ea02d3f412152aec321a45fb7ebc6672e63e46ebafcad5c3ed561867ace3420b15a1dd5b77d33d26ad392
6
+ metadata.gz: e2ff22eb616b95a99f3046b0c63677f24f7ca0ee1327bb7a51e97a68190b9c39639562024efaca73afa292b24b05188d9abaedba74de491bf708606526895519
7
+ data.tar.gz: 8658836ee6bb761ff2c75f9cd7c11ea2c2f526604982933d48eced8edf31ac515b8ca3ed97ac34bb515dca7baa21cb3d5de10463e13268519d3b73f8293be8f0
data/.rubocop.yml CHANGED
@@ -1,3 +1,6 @@
1
+ Metrics/ClassLength:
2
+ Max: 150
3
+
1
4
  Metrics/MethodLength:
2
5
  Max: 20
3
6
 
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- reddit-to-telegram (0.5.1)
5
- aws-sdk-simpledb
4
+ reddit-to-telegram (0.10.0)
5
+ aws-sdk-dynamodb (~> 1.106)
6
6
  httparty
7
7
 
8
8
  GEM
@@ -11,15 +11,14 @@ GEM
11
11
  ast (2.4.2)
12
12
  aws-eventstream (1.3.0)
13
13
  aws-partitions (1.883.0)
14
- aws-sdk-core (3.190.3)
14
+ aws-sdk-core (3.192.1)
15
15
  aws-eventstream (~> 1, >= 1.3.0)
16
16
  aws-partitions (~> 1, >= 1.651.0)
17
17
  aws-sigv4 (~> 1.8)
18
18
  jmespath (~> 1, >= 1.6.1)
19
- aws-sdk-simpledb (1.42.0)
20
- aws-sdk-core (~> 3, >= 3.188.0)
21
- aws-sigv2 (~> 1.0)
22
- aws-sigv2 (1.2.0)
19
+ aws-sdk-dynamodb (1.106.0)
20
+ aws-sdk-core (~> 3, >= 3.191.0)
21
+ aws-sigv4 (~> 1.1)
23
22
  aws-sigv4 (1.8.0)
24
23
  aws-eventstream (~> 1, >= 1.0.2)
25
24
  httparty (0.21.0)
data/README.md CHANGED
@@ -20,7 +20,7 @@ Or `gem install reddit-to-telegram`. Don't forget to `require` it.
20
20
 
21
21
  ## Prerequisites
22
22
  - [Obtain a telegram bot token](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)
23
- - (Optionally) You'll need an [AWS account](https://aws.amazon.com/) to host a free SimpleDB (best available storage type, also default one). I also recommend hosting the bot on AWS lambda, since it would be free.
23
+ - (Optionally) You'll need an [AWS account](https://aws.amazon.com/) to host a free DynamoDB (best available storage type, also default one). I also recommend hosting the bot on AWS lambda, since it would be free.
24
24
  - (Optionally) [Create a Reddit app](https://www.reddit.com/prefs/apps), which would allow more requests to reddit
25
25
 
26
26
  It is pretty congifurable, either dynamically or via ENV variables.
@@ -35,16 +35,15 @@ logger | - | Which logger to use.
35
35
  on_error | - | What to do when an error happens. Default is :log, but you can also :raise or :ignore |
36
36
  send_errors_to_telegram | - | Also log errors to telegram (besides regular logging). Accepted values: true or false, default is false |
37
37
  translate | - | Translate posts via Google Translate. Leave empty for no translation. More details below |
38
- aws.access_key_id | RTT_AWS_ACCESS_KEY_ID | Your AWS access key ID. Needed for AWS SimpleDB storage |
39
- aws.domain_name | RTT_AWS_DOMAIN_NAME | Domain name to use for SimpleDB |
40
- aws.region | RTT_AWS_REGION | AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions. |
41
- aws.secret_access_key | RTT_AWS_SECRET_ACCESS_KEY | Your AWS access key ID. Needed for AWS SimpleDB storage. |
38
+ aws.access_key_id | RTT_AWS_ACCESS_KEY_ID | Your AWS access key ID. Needed for AWS DynamoDB storage |
39
+ aws.region | RTT_AWS_REGION | AWS region your DynamoDB is hosted on |
40
+ aws.secret_access_key | RTT_AWS_SECRET_ACCESS_KEY | Your AWS access key ID. Needed for AWS DynamoDB storage. |
42
41
  google.api_key | RTT_GOOGLE_API_KEY | Your Google API key to translate posts via Google Translate |
43
42
  reddit.client_id | RTT_REDDIT_CLIENT_ID | Reddit app credentials to access API. Reddit allows more authenticated requests |
44
43
  reddit.client_secret | RTT_REDDIT_CLIENT_SECRET | Reddit app credentials to access API. Reddit allows more authenticated requests |
45
44
  store.max_stored_posts | RTT_MAX_STORED_POSTS | Number of posts to store in the database to avoid duplicates, default is 25 |
46
45
  store.tmp_dir | RTT_TEMP_DIR | Directory to write temp files to without trailing `/` |
47
- store.type | 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 |
46
+ store.type | RTT_STORE_TYPE | Choose between `aws_dynamo_db`, `memory` or `temp_file`. Default is `aws_dynamo_db`, so if you're not specifying your AWS credentials, you have to choose another store type |
48
47
  telegram.bot_token * | RTT_TELEGRAM_BOT_TOKEN | The token you've received when you've created a telegram bot |
49
48
  telegram.error_channel_id | RTT_TELEGRAM_ERROR_CHANNEL_ID | Telegram channel to send errors to (without `@`, only errors from Telegram API responses would be sent for now) |
50
49
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "reddit_to_telegram/configuration"
3
4
  Dir["#{File.dirname(__FILE__)}/reddit_to_telegram/**/*.rb"].each { |file| require file }
4
5
 
5
6
  module RedditToTelegram
@@ -37,7 +37,7 @@ module RedditToTelegram
37
37
 
38
38
  class Store
39
39
  DEFAULT_TMP_DIR = "#{Dir.pwd}/tmp".freeze
40
- DEFAULT_TYPE = :aws_simple_db
40
+ DEFAULT_TYPE = :aws_dynamo_db
41
41
 
42
42
  class << self
43
43
  attr_writer :max_stored_posts, :tmp_dir, :type
@@ -57,8 +57,7 @@ module RedditToTelegram
57
57
  end
58
58
 
59
59
  class AWS
60
- ATTRS = %i[access_key_id secret_access_key region domain_name].freeze
61
- DEFAULT_DOMAIN_NAME = "reddit_to_telegram"
60
+ ATTRS = %i[access_key_id secret_access_key region].freeze
62
61
 
63
62
  class << self
64
63
  attr_writer(*ATTRS)
@@ -75,10 +74,6 @@ module RedditToTelegram
75
74
  @region ||= ENV["RTT_AWS_REGION"]
76
75
  end
77
76
 
78
- def domain_name
79
- @domain_name ||= ENV["RTT_AWS_DOMAIN_NAME"] || DEFAULT_DOMAIN_NAME
80
- end
81
-
82
77
  def set_up?
83
78
  ATTRS.all? { |a| !a.to_s.empty? }
84
79
  end
@@ -4,7 +4,9 @@ module RedditToTelegram
4
4
  class RedditToTelegramError < StandardError; end
5
5
 
6
6
  class BadResponseFromTelegram < RedditToTelegramError; end
7
+ class FailedToCreateDatabaseTable < RedditToTelegramError; end
7
8
  class FailedToFetchFromReddit < RedditToTelegramError; end
9
+ class FailedToPersistData < RedditToTelegramError; end
8
10
  class InvalidStoreType < RedditToTelegramError; end
9
11
  class MissingConfiguration < RedditToTelegramError; end
10
12
 
@@ -7,9 +7,11 @@ module RedditToTelegram
7
7
  check_config
8
8
  Store.setup
9
9
 
10
- sources.each do |telegram_chat_id, subreddit|
11
- res = Reddit::Fetch.hot(subreddit)
12
- handle_res(res, subreddit, telegram_chat_id)
10
+ sources.each do |telegram_chat_id, subreddits|
11
+ Array(subreddits).each do |subreddit|
12
+ res = Reddit::Fetch.hot(subreddit)
13
+ handle_res(res, subreddit, telegram_chat_id)
14
+ end
13
15
  end
14
16
  end
15
17
 
@@ -36,16 +38,14 @@ module RedditToTelegram
36
38
  def handle_res(res, subreddit, telegram_chat_id)
37
39
  return unless res_ok?(res)
38
40
 
39
- post = find_new_post(subreddit, res)
41
+ post = Store.posts.next(telegram_chat_id, subreddit, res)
40
42
 
41
43
  if post.nil?
42
44
  Configuration.logger.info("Could not find a new post to push")
43
45
  return
44
46
  end
45
47
 
46
- res = Telegram::Post.push(post, telegram_chat_id)
47
- Store::Posts.add(subreddit, post[:id])
48
- res
48
+ Telegram::Post.push(post, telegram_chat_id)
49
49
  end
50
50
 
51
51
  def res_ok?(res)
@@ -60,10 +60,6 @@ module RedditToTelegram
60
60
  end
61
61
  end
62
62
 
63
- def find_new_post(subreddit, posts)
64
- posts.find { |post| !Store::Posts.dup?(subreddit, post[:id]) }
65
- end
66
-
67
63
  def check_from_link_sources(sources)
68
64
  if !sources.is_a?(Hash) || sources.keys.count != 1 || sources.values.count != 1
69
65
  Errors.new(ArgumentError, "Check documentation on usage")
@@ -96,7 +96,7 @@ module RedditToTelegram
96
96
  end
97
97
 
98
98
  def base_post_format_attrs(data)
99
- { id: data["name"],
99
+ { id: data["name"].split("_")&.dig(1),
100
100
  text: CGI.unescapeHTML(data["title"]),
101
101
  misc: {} }
102
102
  end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws-sdk-dynamodb"
4
+ require "json"
5
+
6
+ module RedditToTelegram
7
+ module Store
8
+ class AWSDynamoDB
9
+ ITEM_NAME = "cached_data"
10
+
11
+ class << self
12
+ def client
13
+ @client ||= Aws::DynamoDB::Client.new(
14
+ access_key_id: Configuration.aws.access_key_id,
15
+ secret_access_key: Configuration.aws.secret_access_key,
16
+ region: Configuration.aws.region
17
+ )
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :reddit_token
23
+
24
+ def setup
25
+ check_credentials
26
+ prepare_db
27
+ assign_default_values
28
+ end
29
+
30
+ def check_credentials
31
+ return unless Configuration.store.type == :aws_dynamo_db
32
+
33
+ return if Configuration.aws.set_up?
34
+
35
+ Errors.new(
36
+ MissingConfiguration,
37
+ "Missing AWS credentials. Set them up or change store type to anything other than aws_dynamo_db"
38
+ )
39
+ end
40
+
41
+ def add_post(telegram_chat_id, subreddit, id)
42
+ assign_empty_values_to_posts(telegram_chat_id, subreddit)
43
+
44
+ @posts[telegram_chat_id][subreddit] << id
45
+
46
+ if @posts[telegram_chat_id][subreddit].count > Store.max_stored_posts
47
+ @posts[telegram_chat_id][subreddit].shift
48
+ end
49
+
50
+ persist_posts(telegram_chat_id)
51
+ end
52
+
53
+ def assign_empty_values_to_posts(telegram_chat_id, subreddit)
54
+ @posts[telegram_chat_id] = {} if @posts[telegram_chat_id].nil?
55
+ @posts[telegram_chat_id][subreddit] = [] if @posts[telegram_chat_id][subreddit].nil?
56
+ end
57
+
58
+ def persist_posts(telegram_chat_id)
59
+ res = client.put_item(
60
+ {
61
+ item: {
62
+ "TelegramChannel" => telegram_chat_id.to_s,
63
+ "Posts" => @posts[telegram_chat_id].to_json
64
+ },
65
+ return_consumed_capacity: "TOTAL",
66
+ table_name: POSTS_TABLE_NAME
67
+ }
68
+ )
69
+
70
+ Errors.new(FailedToPersistData, "Failed to persist data to DynamoDB") unless res.successful?
71
+ end
72
+
73
+ def dup_post?(telegram_chat_id, subreddit, id)
74
+ return false if @posts.dig(telegram_chat_id, subreddit).nil?
75
+
76
+ @posts[telegram_chat_id][subreddit].include?(id)
77
+ end
78
+
79
+ def load_posts(telegram_chat_id)
80
+ res = client.get_item(
81
+ { key: { "TelegramChannel" => telegram_chat_id.to_s },
82
+ table_name: POSTS_TABLE_NAME }
83
+ )
84
+ @posts[telegram_chat_id] = JSON.parse(res.item["Posts"]).transform_keys(&:to_sym)
85
+ rescue StandardError
86
+ @posts[telegram_chat_id] = nil
87
+ end
88
+
89
+ def assign_default_values
90
+ @reddit_token = ""
91
+ @posts = {}
92
+ end
93
+
94
+ POSTS_TABLE_NAME = "Posts"
95
+ POSTS_TABLE_ATTRIBUTES = {
96
+ attribute_definitions: [
97
+ { attribute_name: "TelegramChannel",
98
+ attribute_type: "S" }
99
+ ],
100
+ key_schema: [
101
+ { attribute_name: "TelegramChannel",
102
+ key_type: "HASH" }
103
+ ],
104
+ provisioned_throughput: {
105
+ read_capacity_units: 1,
106
+ write_capacity_units: 1
107
+ },
108
+ table_name: POSTS_TABLE_NAME
109
+ }.freeze
110
+
111
+ def prepare_db
112
+ res = client.list_tables
113
+ return unless res.successful?
114
+
115
+ return if res.table_names.include?(POSTS_TABLE_NAME)
116
+
117
+ client.create_table(POSTS_TABLE_ATTRIBUTES)
118
+
119
+ waited = 0
120
+ while client.describe_table(table_name: POSTS_TABLE_NAME).table.table_status != "ACTIVE"
121
+ if waited == 10
122
+ Errors.new(FailedToCreateDatabaseTable, "Failed to create #{POSTS_TABLE_NAME} table in DynamoDB")
123
+ break
124
+ end
125
+ sleep(1)
126
+ waited += 1
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -11,17 +11,26 @@ module RedditToTelegram
11
11
  @posts = {}
12
12
 
13
13
  def setup; end
14
+ def load_posts(_); end
14
15
 
15
- def add_post(subreddit, id)
16
- @posts[subreddit] = [] if @posts[subreddit].nil?
17
- @posts[subreddit] << id
18
- @posts[subreddit].shift if @posts[subreddit].count > Store::MAX_STORED_POSTS
16
+ def add_post(telegram_chat_id, subreddit, id)
17
+ assign_empty_values_to_posts(telegram_chat_id, subreddit)
18
+
19
+ posts[telegram_chat_id][subreddit] << id
20
+ return unless posts[telegram_chat_id][subreddit].count > Store.max_stored_posts
21
+
22
+ posts[telegram_chat_id][subreddit].shift
23
+ end
24
+
25
+ def assign_empty_values_to_posts(telegram_chat_id, subreddit)
26
+ posts[telegram_chat_id] = {} if posts[telegram_chat_id].nil?
27
+ posts[telegram_chat_id][subreddit] = [] if posts[telegram_chat_id][subreddit].nil?
19
28
  end
20
29
 
21
- def dup_post?(subreddit, id)
22
- return false if posts[subreddit].nil?
30
+ def dup_post?(telegram_channel, subreddit, id)
31
+ return false if posts.dig(telegram_channel, subreddit).nil?
23
32
 
24
- posts[subreddit].include?(id)
33
+ posts[telegram_channel][subreddit].include?(id)
25
34
  end
26
35
 
27
36
  def posts
@@ -17,22 +17,34 @@ module RedditToTelegram
17
17
  read_file
18
18
  end
19
19
 
20
+ def load_posts(_); end
21
+
20
22
  def reddit_token=(val)
21
23
  @reddit_token = val
22
24
  write_file
23
25
  end
24
26
 
25
- def add_post(subreddit, id)
26
- @posts[subreddit] = [] if @posts[subreddit].nil?
27
- @posts[subreddit] << id
28
- @posts[subreddit].shift if @posts[subreddit].count > Store::MAX_STORED_POSTS
27
+ def add_post(telegram_chat_id, subreddit, id)
28
+ assign_empty_values_to_posts(telegram_chat_id, subreddit)
29
+
30
+ @posts[telegram_chat_id][subreddit] << id
31
+
32
+ if @posts[telegram_chat_id][subreddit].count > Store.max_stored_posts
33
+ @posts[telegram_chat_id][subreddit].shift
34
+ end
35
+
29
36
  write_file
30
37
  end
31
38
 
32
- def dup_post?(subreddit, id)
33
- return false if posts[subreddit].nil?
39
+ def assign_empty_values_to_posts(telegram_chat_id, subreddit)
40
+ @posts[telegram_chat_id] = {} if @posts[telegram_chat_id].nil?
41
+ @posts[telegram_chat_id][subreddit] = [] if @posts[telegram_chat_id][subreddit].nil?
42
+ end
43
+
44
+ def dup_post?(telegram_channel, subreddit, id)
45
+ return false if posts.dig(telegram_channel, subreddit).nil?
34
46
 
35
- posts[subreddit].include?(id)
47
+ posts[telegram_channel][subreddit].include?(id)
36
48
  end
37
49
 
38
50
  def read_file
@@ -43,14 +55,14 @@ module RedditToTelegram
43
55
  @reddit_token = data["reddit_token"]
44
56
  @posts = {}
45
57
  data.each do |key, value|
46
- @posts[key.split("_").last.to_sym] = value if key.match?(/posts_.+/)
58
+ @posts[key.split("_").last.to_sym] = value.transform_keys(&:to_sym) if key.match?(/posts_.+/)
47
59
  end
48
60
  end
49
61
 
50
62
  def write_file
51
63
  data = { reddit_token: @reddit_token }
52
- @posts.each do |subreddit, values|
53
- data["posts_#{subreddit}".to_sym] = values
64
+ @posts.each do |telegram_chat_id, values|
65
+ data["posts_#{telegram_chat_id}"] = values
54
66
  end
55
67
  File.open(temp_file_path, "w") { |f| f.write(data.to_json) }
56
68
  end
@@ -2,15 +2,12 @@
2
2
 
3
3
  module RedditToTelegram
4
4
  module Store
5
- MAX_STORED_POSTS = Configuration.store.max_stored_posts - 1
6
5
  CLASS_MAP = {
7
- aws_simple_db: "RedditToTelegram::Store::AWSSimpleDB",
6
+ aws_dynamo_db: "RedditToTelegram::Store::AWSDynamoDB",
8
7
  memory: "RedditToTelegram::Store::Memory",
9
8
  temp_file: "RedditToTelegram::Store::TempFile"
10
9
  }.freeze
11
10
 
12
- STORE = Object.const_get("RedditToTelegram::Store::AWSSimpleDB")
13
-
14
11
  class << self
15
12
  attr_accessor :active
16
13
 
@@ -20,6 +17,18 @@ module RedditToTelegram
20
17
  self.active = Object.const_get(CLASS_MAP[Configuration.store.type])
21
18
  active.send(:setup)
22
19
  end
20
+
21
+ def max_stored_posts
22
+ Configuration.store.max_stored_posts - 1
23
+ end
24
+
25
+ def reddit
26
+ Reddit
27
+ end
28
+
29
+ def posts
30
+ Posts
31
+ end
23
32
  end
24
33
 
25
34
  class Reddit
@@ -36,12 +45,19 @@ module RedditToTelegram
36
45
 
37
46
  class Posts
38
47
  class << self
39
- def add(subreddit, id)
40
- Store.active.send(:add_post, subreddit, id)
48
+ def add(telegram_chat_id, subreddit, id)
49
+ Store.active.send(:add_post, telegram_chat_id, subreddit, id)
50
+ end
51
+
52
+ def dup?(telegram_chat_id, subreddit, id)
53
+ Store.active.send(:dup_post?, telegram_chat_id, subreddit, id)
41
54
  end
42
55
 
43
- def dup?(subreddit, id)
44
- Store.active.send(:dup_post?, subreddit, id)
56
+ def next(telegram_chat_id, subreddit, posts)
57
+ Store.active.send(:load_posts, telegram_chat_id)
58
+ new_post = posts.find { |post| !dup?(telegram_chat_id, subreddit, post[:id]) }
59
+ add(telegram_chat_id, subreddit, new_post[:id]) unless new_post.nil?
60
+ new_post
45
61
  end
46
62
  end
47
63
  end
@@ -51,8 +51,7 @@ module RedditToTelegram
51
51
  def add_reddit_link(text, post)
52
52
  return text unless Configuration.add_reddit_link
53
53
 
54
- id = post[:id]&.split("_")&.dig(1)
55
- text += "\n\nhttps://redd.it/#{id}" if id
54
+ text += "\n\nhttps://redd.it/#{post[:id]}"
56
55
  text
57
56
  end
58
57
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedditToTelegram
4
- VERSION = "0.9.0"
4
+ VERSION = "0.10.0"
5
5
  end
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.homepage = "https://github.com/dersnek/reddit-to-telegram"
15
15
  s.license = "MIT"
16
16
 
17
- s.add_dependency "aws-sdk-simpledb"
17
+ s.add_dependency "aws-sdk-dynamodb", "~> 1.106"
18
18
  s.add_dependency "httparty"
19
19
 
20
20
  s.add_development_dependency "rubocop"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reddit-to-telegram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.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-04-08 00:00:00.000000000 Z
11
+ date: 2024-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: aws-sdk-simpledb
14
+ name: aws-sdk-dynamodb
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.106'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '1.106'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: httparty
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -75,7 +75,7 @@ files:
75
75
  - lib/reddit_to_telegram/reddit/output/imgur.rb
76
76
  - lib/reddit_to_telegram/services/translate.rb
77
77
  - lib/reddit_to_telegram/store.rb
78
- - lib/reddit_to_telegram/store/aws_simple_db.rb
78
+ - lib/reddit_to_telegram/store/aws_dynamo_db.rb
79
79
  - lib/reddit_to_telegram/store/memory.rb
80
80
  - lib/reddit_to_telegram/store/temp_file.rb
81
81
  - lib/reddit_to_telegram/telegram/post.rb
@@ -1,123 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "aws-sdk-simpledb"
4
- require "json"
5
-
6
- module RedditToTelegram
7
- module Store
8
- class AWSSimpleDB
9
- ITEM_NAME = "cached_data"
10
-
11
- class << self
12
- def client
13
- @client ||= Aws::SimpleDB::Client.new(
14
- access_key_id: Configuration.aws.access_key_id,
15
- secret_access_key: Configuration.aws.secret_access_key,
16
- region: Configuration.aws.region
17
- )
18
- end
19
-
20
- private
21
-
22
- attr_reader :reddit_token
23
-
24
- def setup
25
- check_credentials
26
- create_domain unless client.list_domains.domain_names.include?(Configuration.aws.domain_name)
27
- read_db
28
- end
29
-
30
- def check_credentials
31
- return unless Configuration.store.type == :aws_simple_db
32
-
33
- return if Configuration.aws.set_up?
34
-
35
- Errors.new(
36
- MissingConfiguration,
37
- "Missing AWS credentials. Set them up or change store type to anything other than aws_simple_db"
38
- )
39
- end
40
-
41
- def reddit_token=(val)
42
- @reddit_token = val
43
- write_db
44
- end
45
-
46
- def add_post(subreddit, id)
47
- @posts[subreddit] = [] if @posts[subreddit].nil?
48
- @posts[subreddit] << id
49
- @posts[subreddit].shift if @posts[subreddit].count > Store::MAX_STORED_POSTS
50
- write_db
51
- end
52
-
53
- def dup_post?(subreddit, id)
54
- return false if @posts[subreddit].nil?
55
-
56
- @posts[subreddit].include?(id)
57
- end
58
-
59
- def read_db
60
- res = client.get_attributes(
61
- {
62
- domain_name: Configuration.aws.domain_name,
63
- item_name: "cached_data",
64
- consistent_read: true
65
- }
66
- )
67
-
68
- return assign_default_values if res.attributes.empty?
69
-
70
- assign_values_from_db(res)
71
- end
72
-
73
- def assign_values_from_db(data)
74
- @reddit_token = data.attributes.find { |a| a.name == "reddit_token" }.value || ""
75
- @posts = {}
76
- data.attributes.each do |attr|
77
- @posts[attr.name.split("_").last.to_sym] = JSON.parse(attr.value) if attr.name.match?(/posts_.+/)
78
- end
79
- end
80
-
81
- def write_db
82
- client.put_attributes(
83
- {
84
- domain_name: Configuration.aws.domain_name,
85
- item_name: ITEM_NAME,
86
- attributes: prepare_db_attrs
87
- }
88
- )
89
- end
90
-
91
- def prepare_db_attrs
92
- attrs = [
93
- {
94
- name: "reddit_token",
95
- value: @reddit_token,
96
- replace: true
97
- }
98
- ]
99
-
100
- @posts.each do |subreddit, values|
101
- attrs << { name: "posts_#{subreddit}", value: values.to_json, replace: true }
102
- end
103
-
104
- attrs
105
- end
106
-
107
- def assign_default_values
108
- @reddit_token = ""
109
- @posts = {}
110
- end
111
-
112
- def create_domain
113
- res = client.list_domains
114
- return unless res.successful?
115
-
116
- return if res.domain_names.include?(Configuration.aws.domain_name)
117
-
118
- client.create_domain({ domain_name: Configuration.aws.domain_name })
119
- end
120
- end
121
- end
122
- end
123
- end