reddit_bot 1.1.1 → 1.1.2
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/examples/oneplus/Gemfile.lock +1 -1
- data/lib/reddit_bot.rb +13 -10
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6f403237693827c791d79ee8904c9cfb9dc5a5a7
         | 
| 4 | 
            +
              data.tar.gz: a3804dca179ba6cb72455d65f6b50cd1cecd3939
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 601b5f77064340aedbc9264a0f96483705c49302f913af46db8fc3d347ade2222969516804976d6af55dac7cbc3b041e46918c5170b141ebc1b6cf31d05a7d59
         | 
| 7 | 
            +
              data.tar.gz: 906036dc78a34315d0df25958c094e042b4d66ed68f8198004f530eea20df76a8013a33ee7ef67108bf8299a81602dd5ea7d774f92742ecac0b5911353a446db
         | 
    
        data/lib/reddit_bot.rb
    CHANGED
    
    | @@ -8,7 +8,7 @@ require "json" | |
| 8 8 |  | 
| 9 9 |  | 
| 10 10 | 
             
            module RedditBot
         | 
| 11 | 
            -
              VERSION = "1.1. | 
| 11 | 
            +
              VERSION = "1.1.2"
         | 
| 12 12 |  | 
| 13 13 | 
             
              class Bot
         | 
| 14 14 |  | 
| @@ -40,7 +40,7 @@ module RedditBot | |
| 40 40 | 
             
                          iden: @iden_and_captcha[0],
         | 
| 41 41 | 
             
                          captcha: @iden_and_captcha[1],
         | 
| 42 42 | 
             
                        } ) unless @ignore_captcha
         | 
| 43 | 
            -
                      else ;  | 
| 43 | 
            +
                      else ; fail error
         | 
| 44 44 | 
             
                      end
         | 
| 45 45 | 
             
                    end
         | 
| 46 46 | 
             
                  end
         | 
| @@ -91,22 +91,25 @@ module RedditBot | |
| 91 91 | 
             
                    text: link_flair_text,
         | 
| 92 92 | 
             
                    flair_template_id: (@flairselector_choices ||=
         | 
| 93 93 | 
             
                      json :post, "/r/#{@subreddit}/api/flairselector", link: post["name"]
         | 
| 94 | 
            -
                    )["choices"].find{ |i| i["flair_css_class"] == link_flair_css_class } | 
| 94 | 
            +
                    )["choices"].find{ |i| i["flair_css_class"] == link_flair_css_class }.tap{ |flair|
         | 
| 95 | 
            +
                      fail "can't find '#{link_flair_css_class}' flair class at https://www.reddit.com/r/#{@subreddit}/about/flair/#link_templates" unless flair
         | 
| 96 | 
            +
                    }["flair_template_id"]
         | 
| 95 97 | 
             
                end
         | 
| 96 98 |  | 
| 97 99 | 
             
                private
         | 
| 98 100 |  | 
| 99 101 | 
             
                def token
         | 
| 100 102 | 
             
                  return @token_cached if @token_cached
         | 
| 101 | 
            -
                  response = JSON.parse | 
| 103 | 
            +
                  response = JSON.parse reddit_resp :post,
         | 
| 102 104 | 
             
                    "https://www.reddit.com/api/v1/access_token", {
         | 
| 103 105 | 
             
                      grant_type: "password",
         | 
| 104 106 | 
             
                      username: @username = @secrets[3],
         | 
| 105 107 | 
             
                      password: @secrets[2],
         | 
| 106 | 
            -
                    }, {},  | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 108 | 
            +
                    }, {}, [@secrets[0], @secrets[1]]
         | 
| 109 | 
            +
                  unless @token_cached = response["access_token"]
         | 
| 110 | 
            +
                    fail "bot isn't a 'developer' of app at https://www.reddit.com/prefs/apps/" if response == {"error"=>"invalid_grant"}
         | 
| 111 | 
            +
                    fail response.inspect
         | 
| 112 | 
            +
                  end
         | 
| 110 113 | 
             
                  puts "new token is: #{@token_cached}"
         | 
| 111 114 | 
             
                  update_captcha if "true" == resp_with_token(:get, "/api/needs_captcha", {})
         | 
| 112 115 | 
             
                  @token_cached
         | 
| @@ -123,7 +126,7 @@ module RedditBot | |
| 123 126 | 
             
                end
         | 
| 124 127 |  | 
| 125 128 | 
             
                def resp_with_token mtd, url, form
         | 
| 126 | 
            -
                   | 
| 129 | 
            +
                  nil until _ = catch(:"401") do
         | 
| 127 130 | 
             
                    reddit_resp mtd, "https://oauth.reddit.com" + url, form, [
         | 
| 128 131 | 
             
                      ["Authorization", "bearer #{token}"],
         | 
| 129 132 | 
             
                      ["User-Agent", "bot/#{@username}/0.0.0 by /u/nakilon"],
         | 
| @@ -134,7 +137,7 @@ module RedditBot | |
| 134 137 |  | 
| 135 138 | 
             
                def reddit_resp *args
         | 
| 136 139 | 
             
                  response = nil
         | 
| 137 | 
            -
                   | 
| 140 | 
            +
                  tap do
         | 
| 138 141 | 
             
                    response = _resp *args
         | 
| 139 142 | 
             
                    case response.code
         | 
| 140 143 | 
             
                    when "502", "503", "520", "500", "521", "504", "400", "522"
         |