Skip to content

Commit

Permalink
reddit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
amancevice committed Feb 14, 2025
1 parent 9fd2d22 commit bff88ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
24 changes: 24 additions & 0 deletions blue/reddit/pop/lib/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@

{ QueueSize: queue.size, NextPost: post&.to_item }.compact
end

############################
# RE-AUTHORIZING STEPS #
############################
#
# https://www.reddit.com/prefs/apps
#
# client_id=<client_id>
# client_secret=<client_secret>
# state=Brutalismbot
# redirect_uri=https://www.brutalismbot.com/
# duration=permanent
# scope=read
#
# open "https://www.reddit.com/api/v1/authorize?client_id=$client_id&response_type=code&state=$state&redirect_uri=$redirect_uri&duration=$duration&scope=$scope"
#
# code=<copy from redirect URL>
#
# curl -X POST https://www.reddit.com/api/v1/access_token \
# -H 'content-type: application/x-www-form-urlencoded' \
# -A "$state" \
# -u "$client_id:$client_secret" \
# -d "grant_type=authorization_code&code=$code&redirect_uri=$redirect_uri" \
# | jq -r '.refresh_token'
32 changes: 15 additions & 17 deletions blue/reddit/pop/lib/lib/reddit/brutalism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def initialize(resource = nil, path = nil)
@ssm = Aws::SSM::Client.new
end

def client_id = params.CLIENT_ID
def client_secret = params.CLIENT_SECRET
def refresh_token = params.REFRESH_TOKEN
def client_id = params.OAUTH_CLIENT_ID
def client_secret = params.OAUTH_CLIENT_SECRET
def refresh_token = params.OAUTH_REFRESH_TOKEN
def user_agent = params.USER_AGENT

def each
Expand All @@ -41,14 +41,9 @@ def each
http.request(req)
end

begin
res.body.to_h_from_json.symbolize_names.dig(:data, :children).each do |child|
post = Post.new child[:data]
yield post if post.media_urls.any?
end
rescue
logger.error(res.body)
raise
res.body.to_h_from_json.symbolize_names.dig(:data, :children).each do |child|
post = Post.new child[:data]
yield post if post.media_urls.any?
end
end

Expand Down Expand Up @@ -113,17 +108,20 @@ def fetch_authorization
http.request(req, body).body.to_h_from_json
end

"Bearer #{ res['access_token'] }"
"Bearer #{ res.fetch('access_token') }"
end

def fetch_params
params = { path: @path, with_decryption: true }
logger.info "SSM:GetParametersByPath #{params.to_json}"
result = @ssm.get_parameters_by_path(**params).map(&:parameters).flatten.map do |param|
{ File.basename(param.name) => param.value }
end.reduce(&:merge)

OpenStruct.new(result)
keyval = ->(param) { { File.basename(param.name) => param.value } }
result = @ssm.get_parameters_by_path(**params)
.flat_map(&:parameters)
.map(&keyval)
.reduce(&:merge)
.transform_keys(&:to_sym)

Struct.new(*result.keys).new(*result.values)
end
end
end

0 comments on commit bff88ea

Please sign in to comment.