|
require 'open-uri' |
|
require 'slack-notifier' |
|
|
|
class FlippSlacka |
|
# Slack Webhook |
|
SLACK_WEBHOOK = "https://hooks.slack.com/services/XXXX/XXXX/XXXXXXXX" |
|
SLACK_CHANNEL = "#flippa" |
|
SLACK_USERNAME = "Flippa Robot" |
|
SLACK_AVATAR = "http://www.techlivewire.com/wp-content/uploads/2014/09/Flippa-logo1.png" |
|
# Flippa Config |
|
DOMAIN = "https://flippa.com/" |
|
DEFAULT_PATH = "domains/ending-soon" |
|
# May be "nil" aswell to ignore filter |
|
MINIMAL_CHAR_COUNT = 1 |
|
MAXIMAL_CHAR_COUNT = 6 |
|
MINIMAL_PRICE = 0 |
|
MAXIMAL_PRICE = 100 |
|
|
|
def scan |
|
slack.ping "Todays Flippa offers! Yay :heart:", attachments: (parse_page.map do |domain| |
|
_end = Time.parse(domain['ends_at']).to_i.duration |
|
next if domain['property_name'].strip.include? ' ' || _end.nil? |
|
{ |
|
fallback: domain['property_name'], |
|
text: "<#{domain['html_url']}|#{domain['property_name']}> *$ #{domain['current_price']}* _(#{domain['bid_count']})_ `#{_end} left`", |
|
color: domain['bid_count'].to_i > 0 ? "warning" : "good", |
|
mrkdwn_in: ["text"] |
|
} |
|
end.compact) |
|
end |
|
|
|
def parse_page |
|
JSON.parse( |
|
open(DOMAIN+DEFAULT_PATH+domain_append).read |
|
.split('"searchResults":').last |
|
.split(',"altResults":').first |
|
).first.last['data'] |
|
end |
|
|
|
def domain_append |
|
append = "" |
|
append += "?" |
|
append += "&domain_characters_count_min=#{MINIMAL_CHAR_COUNT}" if MINIMAL_CHAR_COUNT > 1 |
|
append += "&domain_characters_count_max=#{MAXIMAL_CHAR_COUNT}" if MAXIMAL_CHAR_COUNT |
|
append += "&price_min=#{MINIMAL_PRICE}" if MINIMAL_PRICE |
|
append += "&price_max=#{MAXIMAL_PRICE}" if MAXIMAL_PRICE |
|
append += "&sort_alias=ending_soon" |
|
append.gsub('?&','?') |
|
end |
|
|
|
def slack |
|
@slack ||= Slack::Notifier.new SLACK_WEBHOOK, |
|
channel: SLACK_CHANNEL, |
|
username: SLACK_USERNAME, |
|
icon_url: SLACK_AVATAR |
|
end |
|
end |
|
|
|
class Numeric |
|
def duration |
|
secs = (Time.now.to_i - self.to_int)*-1 |
|
mins = secs / 60 |
|
hours = mins / 60 |
|
days = hours / 24 |
|
|
|
if days > 0 |
|
"#{days} days and #{hours % 24} hours" |
|
elsif hours > 0 |
|
"#{hours} hours and #{mins % 60} minutes" |
|
elsif mins > 0 |
|
"#{mins} minutes and #{secs % 60} seconds" |
|
elsif secs >= 0 |
|
"#{secs} seconds" |
|
end |
|
end |
|
end |
|
|
|
FlippSlacka.new.scan |