reddit-to-telegram 0.5.1 → 0.6.1
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 +4 -4
- data/README.md +32 -12
- data/lib/reddit_to_telegram/post.rb +7 -6
- data/lib/reddit_to_telegram/reddit/fetch.rb +5 -2
- data/lib/reddit_to_telegram/reddit/output/imgur.rb +4 -0
- data/lib/reddit_to_telegram/services/translate.rb +47 -0
- data/lib/reddit_to_telegram/telegram/post.rb +4 -4
- data/lib/reddit_to_telegram/telegram/prepare_request.rb +22 -8
- data/lib/reddit_to_telegram/variables.rb +19 -1
- data/lib/reddit_to_telegram/version.rb +1 -1
- metadata +3 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: ecdbf9518073fb5abee3426d73fd77d6b13348402f5d5956e0016af2b0caad17
         | 
| 4 | 
            +
              data.tar.gz: fb2f9c1c27460bd39155056e18fd82014fc351493b8ea7c3e9fe8d96aa2c9bd3
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 20b41766ff2cc135d314299275617faa21374064cf3fa3ce583ac39c595ef6008a84d4267514be0e020fdce89cb8f98cb191d211d8cefe8b551e4f941afffb32
         | 
| 7 | 
            +
              data.tar.gz: 5743d4057fc405dcf43758b53fe4e6de1cfda2758af603e9a3eeea9cafa070ebf98e1835f2b710d5a44f4d674956b289a6e47bed7ce3204abb1e3d0d8ad9c74c
         | 
    
        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 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 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 | 
            -
                         | 
| 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
         | 
| @@ -9,6 +9,8 @@ module RedditToTelegram | |
| 9 9 | 
             
                    class << self
         | 
| 10 10 | 
             
                      def try_extract(data)
         | 
| 11 11 | 
             
                        full_url = decode_imgur_url(data)
         | 
| 12 | 
            +
                        return unless full_url
         | 
| 13 | 
            +
             | 
| 12 14 | 
             
                        video_url = extract_video_url(full_url)
         | 
| 13 15 | 
             
                        width = extract_video_width(full_url)
         | 
| 14 16 | 
             
                        return if video_url.nil? || width.nil?
         | 
| @@ -23,6 +25,8 @@ module RedditToTelegram | |
| 23 25 | 
             
                                      .dig("media_embed", "content")
         | 
| 24 26 | 
             
                                      &.match(/src=\"\S+schema=imgur\"/)&.to_s
         | 
| 25 27 | 
             
                                      &.gsub(/src=\"|\"/, 'src=\"' => "", '\"' => "")
         | 
| 28 | 
            +
                        return unless encoded_url
         | 
| 29 | 
            +
             | 
| 26 30 | 
             
                        CGI.unescape(encoded_url)
         | 
| 27 31 | 
             
                      end
         | 
| 28 32 |  | 
| @@ -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
         | 
| @@ -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 | 
            -
                       | 
| 32 | 
            -
             | 
| 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
         | 
    
        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 | 
            +
              version: 0.6.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-03- | 
| 11 | 
            +
            date: 2024-03-12 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
         |