reddit-to-telegram 0.1.1 → 0.2.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: a9c3ba44a492a2303944ed479ebc4f298123cda19be8e743de74c6d95749554f
4
- data.tar.gz: 18a449672d997581888344def9fc23dc9f43ceef6e8fd818bfe0868fa22cb1d8
3
+ metadata.gz: 36d1523dfcd1455f13aaf69a3460c86c588dc51a318c956bcaa25acbbee0cd82
4
+ data.tar.gz: b5d4d20eedf4a41ffec66d26479955627e205c944696ef445e539e1bb2d9d2a0
5
5
  SHA512:
6
- metadata.gz: 4fb6aab0a80c918760853feb9045b59a11d978129c5ae7c1a77ba7f266a019b5b3353bed17f4173afaf6a1947e6dae252b1c166bbfbad9d46eb800e4cf342533
7
- data.tar.gz: 797a680487b6791e76239cd66ad4a893d37432457e9e6c3ae21051a749add53d55add7115fc548750ed0e6aad5859126485a18fb8802b8f269828a4b6ce6f4bb
6
+ metadata.gz: 51f3e8460aa53bb0d028e27b9852d9a23501a3fa4d6a886d382ae232d68d7a3fcf466c61af82fb38079eea4911fdb10cd830226b4bc4373b5a96426ce8e1378f
7
+ data.tar.gz: 04465e55387e6e5c96e1b891a0ea2189538b9b038e92c80c7a369f4e417092b0f6b2dba6411f9ad83405d9712b764c53b6ab6e207b9bc99d5dd3005823ca8ecb
data/README.md CHANGED
@@ -4,7 +4,60 @@
4
4
 
5
5
  #### Fetches hot posts from chosen subreddits and pushes them to Telegram channels.
6
6
 
7
- I'll update readme with instructions soon.
7
+ Beware, this is remotely not production-ready and you'll see lots of bugs. Be sure to check for gem updates.
8
8
 
9
- ### TODO:
9
+ ## Installation
10
+ In your `Gemfile` add:
11
+ ```
12
+ gem "reddit-to-telegram"
13
+ ```
14
+ Then run `bundle install`.
15
+
16
+ Or `gem install reddit-to-telegram`, but don't forget to `require` it first then.
17
+
18
+ ## Prerequisites
19
+ - You need an AWS account to host a free SimpleDB (memory and local file storage options are available, but no way to switch for now)
20
+ - (Optionally) Create a Reddit app, which would allow more requests to reddit
21
+ - [Obtain](https://core.telegram.org/bots/tutorial#create-your-project) a telegram bot token
22
+
23
+ ## Installation
24
+ To run it, you'll need some env variables set.
25
+ ```
26
+ RTT_AWS_ACCESS_KEY_ID= # Your AWS access key ID. Needed for AWS SimpleDB storage.
27
+ RTT_AWS_REGION= # AWS region your SimpleDB will be hosted on. Beware, it's on available in all regions.
28
+ RTT_AWS_SECRET_ACCESS_KEY= # Your AWS access key ID. Needed for AWS SimpleDB storage.
29
+ RTT_MAX_STORED_POSTS= # (Optional) Number of posts to store in the database to avoid duplicates, default is 25.
30
+ RTT_REDDIT_CLIENT_ID= # Reddit app credentials to access API. Might not be needed depending on setup, reddit allows some requests without authentication.
31
+ RTT_REDDIT_CLIENT_SECRET= # Reddit app credentials to access API. Might not be needed depending on setup, reddit allows some requests without authentication.
32
+ RTT_TELEGRAM_BOT_TOKEN= # The token you've received when you've created a telegram bot.
33
+ RTT_TEMP_DIR= (Optional) # Directory to write temp files to without trailing /
34
+ ```
35
+
36
+ You can also set them dynamically:
37
+ ```
38
+ RedditToTelegram::Vars.assign_values(
39
+ max_stored_posts:,
40
+ aws_access_key_id:,
41
+ aws_secret_access_key:,
42
+ aws_region:,
43
+ reddit_client_id:,
44
+ reddit_client_secret:,
45
+ telegram_bot_token:
46
+ )
47
+ ```
48
+ ## Usage
49
+
50
+ 1. Add the bot as administrator to Telegram channels you'd like to post to.
51
+ 2.
52
+ ```
53
+ RedditToTelegram.post(
54
+ subreddit_name_1: :telegram_channel_id_1,
55
+ subreddit_name_2: :telegram_channel_id_2
56
+ )
57
+
58
+ ```
59
+ Use `:telegram_channel_id` without the `@`.
60
+
61
+ ## TODO
62
+ - Storage options
10
63
  - Error handling (maybe send them to another channel in Telegram)
@@ -3,6 +3,7 @@
3
3
  require_relative "reddit_to_telegram/reddit/fetch"
4
4
  require_relative "reddit_to_telegram/store"
5
5
  require_relative "reddit_to_telegram/telegram/post"
6
+ require_relative "reddit_to_telegram/version"
6
7
 
7
8
  module RedditToTelegram
8
9
  class << self
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require_relative "../vars"
4
5
 
5
6
  module RedditToTelegram
6
7
  module Store
7
8
  class TempFile
8
- STORE_FILE_DIR = "#{Dir.pwd}/tmp/store.json".freeze
9
-
10
9
  class << self
11
10
  attr_reader :reddit_token, :posts
12
11
 
@@ -36,9 +35,9 @@ module RedditToTelegram
36
35
  end
37
36
 
38
37
  def read_file
39
- return assign_default_values unless File.exist?(STORE_FILE_DIR)
38
+ return assign_default_values unless File.exist?(temp_file_path)
40
39
 
41
- file = File.read(STORE_FILE_DIR)
40
+ file = File.read(temp_file_path)
42
41
  data = JSON.parse(file)
43
42
  @reddit_token = data["reddit_token"]
44
43
  @posts = {}
@@ -52,13 +51,17 @@ module RedditToTelegram
52
51
  @posts.each do |subreddit, values|
53
52
  data["posts_#{subreddit}".to_sym] = values
54
53
  end
55
- File.open(STORE_FILE_DIR, "w") { |f| f.write(data.to_json) }
54
+ File.open(temp_file_path, "w") { |f| f.write(data.to_json) }
56
55
  end
57
56
 
58
57
  def assign_default_values
59
58
  @reddit_token = nil
60
59
  @posts = {}
61
60
  end
61
+
62
+ def temp_file_path
63
+ "#{Vars.tmp_dir}/store.json"
64
+ end
62
65
  end
63
66
  end
64
67
  end
@@ -26,7 +26,7 @@ module RedditToTelegram
26
26
  **params(post, channel)
27
27
  )
28
28
 
29
- push({ type: :text, text: post[:text] }, channel) if post[:type] == :gallery
29
+ push({ type: :text, id: post[:id], text: post[:text] }, channel) if post[:type] == :gallery
30
30
  Video.delete_file if post[:type] == :video
31
31
  end
32
32
 
@@ -32,7 +32,7 @@ module RedditToTelegram
32
32
  return post[:media] unless post[:misc][:binary]
33
33
 
34
34
  Video.from_link(post[:media])
35
- File.open(Video::TEMP_VIDEO_PATH)
35
+ File.open(Video.temp_video_path)
36
36
  end
37
37
  end
38
38
  end
@@ -1,23 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open-uri"
4
+ require_relative "../vars"
4
5
 
5
6
  module RedditToTelegram
6
7
  module Telegram
7
8
  class Video
8
- TEMP_VIDEO_PATH = "#{Dir.pwd}/tmp/video.mp4".freeze
9
-
10
9
  class << self
11
10
  def from_link(link)
12
11
  download = URI.parse(link).open
13
- IO.copy_stream(download, TEMP_VIDEO_PATH)
12
+ File.open(temp_video_path, "w+b") do |file|
13
+ download.respond_to?(:read) ? IO.copy_stream(download, file) : file.write(download)
14
+ end
14
15
  end
15
16
 
16
17
  def delete_file
17
- f = File.open(TEMP_VIDEO_PATH, "r")
18
+ f = File.open(temp_video_path, "r")
18
19
  ensure
19
20
  f.close unless f.nil? || f.closed?
20
- File.delete(TEMP_VIDEO_PATH) if File.exist?(TEMP_VIDEO_PATH)
21
+ File.delete(temp_video_path) if File.exist?(temp_video_path)
22
+ end
23
+
24
+ def temp_video_path
25
+ "#{Vars.tmp_dir}/video.mp4"
21
26
  end
22
27
  end
23
28
  end
@@ -2,10 +2,13 @@
2
2
 
3
3
  module RedditToTelegram
4
4
  module Vars
5
+ DEFAULT_TMP_DIR = "#{Dir.pwd}/tmp".freeze
6
+
5
7
  class << self
6
8
  # rubocop:disable Metrics/ParameterLists
7
9
  def assign_values(
8
10
  max_stored_posts:,
11
+ tmp_dir:,
9
12
  aws_access_key_id:,
10
13
  aws_secret_access_key:,
11
14
  aws_region:,
@@ -15,6 +18,7 @@ module RedditToTelegram
15
18
  )
16
19
 
17
20
  @max_stored_posts = max_stored_posts
21
+ @tmp_dir = tmp_dir
18
22
  AWS.instance_variable_set(:@access_key_id, aws_access_key_id)
19
23
  AWS.instance_variable_set(:@secret_access_key, aws_secret_access_key)
20
24
  AWS.instance_variable_set(:@region, aws_region)
@@ -27,6 +31,10 @@ module RedditToTelegram
27
31
  def max_stored_posts
28
32
  @max_stored_posts ||= ENV["RTT_MAX_STORED_POSTS"].to_i || 25
29
33
  end
34
+
35
+ def tmp_dir
36
+ @tmp_dir ||= ENV["RTT_TEMP_DIR"] || DEFAULT_TMP_DIR
37
+ end
30
38
  end
31
39
 
32
40
  class AWS
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RedditToTelegram
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.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.1.1
4
+ version: 0.2.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-01-25 00:00:00.000000000 Z
11
+ date: 2024-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-simpledb
@@ -77,7 +77,6 @@ files:
77
77
  - lib/reddit_to_telegram/telegram/video.rb
78
78
  - lib/reddit_to_telegram/vars.rb
79
79
  - lib/reddit_to_telegram/version.rb
80
- - reddit-to-telegram-0.1.0.gem
81
80
  - reddit-to-telegram.gemspec
82
81
  - tmp/.keep
83
82
  homepage: https://github.com/dersnek/reddit-to-telegram
Binary file