reddit-to-telegram 0.4.0 → 0.5.1

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: cf7cd73ace4a26347b3bdca75b782e28d635617d2952d33cf39ef7ee015a64cf
4
- data.tar.gz: b1bee46a1cf4f490b58258d321e4e6949c393c0d116a5be638b34feadf85b713
3
+ metadata.gz: 3666d02e34ba14d6a7030a0e8cd11a6c6de74b71b6062e7df34e4856c5a7f98c
4
+ data.tar.gz: 8423e5fdb9fd749e06c1a672d36078824d5016bccaec15450a42fc3fc682417e
5
5
  SHA512:
6
- metadata.gz: 073d10eb8a45293ed6aa3c8b6779f50965655accfc234cd89ea43c628f71c93448835457c220fcafa20a17e7ef517f61389a102c1c2dca031e853751d62bbe01
7
- data.tar.gz: e53497c36c061d3ba2758e3dddb97c358d2a62bc8c2469ac2417da83991e0675718b1109ff25f2daa61403aaf6c26757aa49aee89e5de7b1af1d89dd402bfa24
6
+ metadata.gz: a5afa435363aaf50e0fdbee4fa878d752f99889c830dd842e1cae957db1d3ac362ba681f79158095d16a439626c423bbb2b895164bfc87685c87250cf38ca8f7
7
+ data.tar.gz: 0a72c0f8759303c1647dc3871e45349ae103ebfb099f40db32ddfd0cca1214eae5a74ec0ecf2b3f942623b98289ad0547c0c58e821780cfbce596238c9664c23
data/.rubocop.yml CHANGED
@@ -18,3 +18,6 @@ Style/StringLiterals:
18
18
 
19
19
  Style/RaiseArgs:
20
20
  Enabled: false
21
+
22
+ Style/RedundantRegexpEscape:
23
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- reddit-to-telegram (0.2.1)
4
+ reddit-to-telegram (0.5.1)
5
5
  aws-sdk-simpledb
6
6
  httparty
7
7
 
data/README.md CHANGED
@@ -23,21 +23,21 @@ Or `gem install reddit-to-telegram`. Don't forget to `require` it.
23
23
 
24
24
  To run it, you'll need some env variables set.
25
25
  ```
26
- RTT_AWS_ACCESS_KEY_ID= # Your AWS access key ID. Needed for AWS SimpleDB storage.
26
+ RTT_AWS_ACCESS_KEY_ID= # (Optional) Your AWS access key ID. Needed for AWS SimpleDB storage.
27
27
  RTT_AWS_DOMAIN_NAME= # (Optional) Domain name to use for SimpleDB
28
- RTT_AWS_REGION= # AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions.
29
- RTT_AWS_SECRET_ACCESS_KEY= # Your AWS access key ID. Needed for AWS SimpleDB storage.
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
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
+ 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
34
  RTT_TELEGRAM_BOT_TOKEN= # The token you've received when you've created a telegram bot.
35
35
  RTT_TEMP_DIR= (Optional) # Directory to write temp files to without trailing /
36
36
  ```
37
37
 
38
38
  You can also set them dynamically:
39
39
  ```
40
- RedditToTelegram::Variables.aws.aws_access_key_id =
40
+ RedditToTelegram::Variables.aws.access_key_id =
41
41
  RedditToTelegram::Variables.telegram.bot_token =
42
42
  ```
43
43
  Check out `lib/variables` for list of all available variables.
@@ -45,19 +45,15 @@ Check out `lib/variables` for list of all available variables.
45
45
  ## Usage
46
46
 
47
47
  1. Add the bot as administrator to Telegram channels you'd like to post to.
48
- 2a. To fetch latest hot post which hasn't been pushed yet:
48
+ 2. To fetch latest hot post which hasn't been pushed yet:
49
49
  ```
50
50
  RedditToTelegram.hot(
51
51
  subreddit_name_1: :telegram_channel_id_1,
52
52
  subreddit_name_2: :telegram_channel_id_2
53
53
  )
54
54
  ```
55
- 2b. To push one specific post:
55
+ Or to push one specific post (the only thing you need to set up for this is your telegram bot token):
56
56
  ```
57
57
  RedditToTelegram.single("regular_link_to_post", :telegram_channel_id)
58
58
  ```
59
59
  Use `:telegram_channel_id` without the `@`.
60
-
61
- ## Planned features
62
- - Upload Imgur gifv links as videos/gifs
63
- - Error handling
@@ -3,4 +3,5 @@
3
3
  module RedditToTelegram
4
4
  class RedditToTelegramError < StandardError; end
5
5
  class InvalidStoreType < RedditToTelegramError; end
6
+ class MissingVariables < RedditToTelegramError; end
6
7
  end
@@ -37,8 +37,9 @@ module RedditToTelegram
37
37
  post = find_new_post(subreddit, res)
38
38
  return if post.nil?
39
39
 
40
- Telegram::Post.push(post, telegram_chat_id)
40
+ res = Telegram::Post.push(post, telegram_chat_id)
41
41
  Store::Posts.add(subreddit, post[:id])
42
+ res
42
43
  end
43
44
 
44
45
  def find_new_post(subreddit, posts)
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+
5
+ module RedditToTelegram
6
+ module Reddit
7
+ class Output
8
+ class Imgur
9
+ class << self
10
+ def try_extract(data)
11
+ full_url = decode_imgur_url(data)
12
+ video_url = extract_video_url(full_url)
13
+ width = extract_video_width(full_url)
14
+ return if video_url.nil? || width.nil?
15
+
16
+ format_imgur_post(data, video_url, width)
17
+ end
18
+
19
+ private
20
+
21
+ def decode_imgur_url(data)
22
+ encoded_url = data
23
+ .dig("media_embed", "content")
24
+ &.match(/src=\"\S+schema=imgur\"/)&.to_s
25
+ &.gsub(/src=\"|\"/, 'src=\"' => "", '\"' => "")
26
+ CGI.unescape(encoded_url)
27
+ end
28
+
29
+ def extract_video_url(full_url)
30
+ video_url_arr = full_url&.match(/image=(\S+\?|&)/)&.to_s&.send(:[], 6..-2)&.split(".")
31
+ return if Array(video_url_arr).empty?
32
+
33
+ video_url_arr[video_url_arr.length - 1] = "mp4"
34
+ video_url_arr.join(".")
35
+ end
36
+
37
+ def extract_video_width(full_url)
38
+ full_url&.match(/w=(\w+)/)&.to_s&.send(:[], 2..)
39
+ end
40
+
41
+ def format_imgur_post(data, video_url, width)
42
+ RedditToTelegram::Reddit::Output.send(
43
+ :base_post_format_attrs, data
44
+ ).merge(
45
+ {
46
+ type: :video,
47
+ media: video_url,
48
+ misc: {
49
+ binary: false,
50
+ video_width: width
51
+ }
52
+ }
53
+ )
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "cgi"
4
+ require_relative "output/imgur"
4
5
 
5
6
  module RedditToTelegram
6
7
  module Reddit
@@ -41,6 +42,9 @@ module RedditToTelegram
41
42
  end
42
43
 
43
44
  def format_link_post(data)
45
+ res = Imgur.try_extract(data) if data["domain"] == "imgur.com"
46
+ return res unless res.nil?
47
+
44
48
  base_post_format_attrs(data).merge(
45
49
  { type: :text,
46
50
  text: "#{data['title']}\n\n#{data['url']}" }
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "aws-sdk-simpledb"
4
4
  require "json"
5
+ require_relative "../errors"
5
6
  require_relative "../variables"
6
7
 
7
8
  module RedditToTelegram
@@ -23,10 +24,20 @@ module RedditToTelegram
23
24
  attr_reader :reddit_token
24
25
 
25
26
  def setup
27
+ check_credentials
26
28
  create_domain unless client.list_domains.domain_names.include?(Variables.aws.domain_name)
27
29
  read_db
28
30
  end
29
31
 
32
+ def check_credentials
33
+ return unless Variables.store.type == :aws_simple_db
34
+
35
+ return if Variables.aws.set_up?
36
+
37
+ raise(MissingVariables.new("Missing AWS credentials. "\
38
+ "Set them up or change store type to anything other than aws_simple_db"))
39
+ end
40
+
30
41
  def reddit_token=(val)
31
42
  @reddit_token = val
32
43
  write_db
@@ -22,13 +22,14 @@ module RedditToTelegram
22
22
 
23
23
  class << self
24
24
  def push(post, channel)
25
- HTTParty.post(
25
+ res = HTTParty.post(
26
26
  "#{BASE_URI}#{Variables.telegram.bot_token}/send#{METHOD_MAP[post[:type]]}",
27
27
  **params(post, channel)
28
28
  )
29
29
 
30
30
  push({ type: :text, id: post[:id], text: post[:text] }, channel) if post[:type] == :gallery
31
31
  Video.delete_file if post[:type] == :video && post.dig(:misc)&.dig(:binary)
32
+ res
32
33
  end
33
34
 
34
35
  private
@@ -42,10 +42,11 @@ module RedditToTelegram
42
42
  end
43
43
 
44
44
  class AWS
45
+ ATTRS = %i[access_key_id secret_access_key region domain_name].freeze
45
46
  DEFAULT_DOMAIN_NAME = "reddit_to_telegram"
46
47
 
47
48
  class << self
48
- attr_writer :access_key_id, :secret_access_key, :region, :domain_name
49
+ attr_writer(*ATTRS)
49
50
 
50
51
  def access_key_id
51
52
  @access_key_id ||= ENV["RTT_AWS_ACCESS_KEY_ID"]
@@ -62,6 +63,10 @@ module RedditToTelegram
62
63
  def domain_name
63
64
  @domain_name ||= ENV["RTT_AWS_DOMAIN_NAME"] || DEFAULT_DOMAIN_NAME
64
65
  end
66
+
67
+ def set_up?
68
+ ATTRS.all? { |a| !a.empty? }
69
+ end
65
70
  end
66
71
  end
67
72
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedditToTelegram
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.1"
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.4.0
4
+ version: 0.5.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-01-30 00:00:00.000000000 Z
11
+ date: 2024-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-simpledb
@@ -70,6 +70,7 @@ files:
70
70
  - lib/reddit_to_telegram/reddit/auth.rb
71
71
  - lib/reddit_to_telegram/reddit/fetch.rb
72
72
  - lib/reddit_to_telegram/reddit/output.rb
73
+ - lib/reddit_to_telegram/reddit/output/imgur.rb
73
74
  - lib/reddit_to_telegram/store.rb
74
75
  - lib/reddit_to_telegram/store/aws_simple_db.rb
75
76
  - lib/reddit_to_telegram/store/memory.rb