-
-
Save ericboehs/c827ca408b7a0dd55dcb33ba3b5b3ca8 to your computer and use it in GitHub Desktop.
Raspberry Pi Locator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'rss' | |
require 'open-uri' | |
SLEEP = 30 | |
url = 'https://rpilocator.com/feed.rss' | |
db = nil | |
# Print cpu/memory usage in bytes when receiving SIGINFO | |
trap 'INFO' do | |
current_time = Time.now.strftime '%Y-%m-%dT%H:%M:%S' | |
cpu, mem = `ps -o %cpu,rss #{Process.pid} | tail -1 | awk '{print $1,$2}'`.chomp.split | |
puts "#{current_time} %CPU: #{cpu}; MEM_RSS:#{mem}" | |
end | |
loop.with_index do |_, i| | |
begin | |
URI.open(url) do |rss| | |
feed = RSS::Parser.parse rss | |
db_items = db&.items&.map { |item| { title: item.title, pubDate: item.pubDate } } || [] | |
feed_items = feed.items.map { |item| { title: item.title, pubDate: item.pubDate } } | |
new_items = feed_items - db_items | |
new_titles = new_items&.map { |item| item[:title] } | |
skip_notification = true unless db | |
db = feed | |
if new_titles.any? { |title| title.include?('Stock Alert (US)') } | |
next if skip_notification | |
message = new_items.map do |item| | |
"<https://rpilocator.com/?country=US|#{item[:title]}> - #{item[:pubDate]}" | |
end.join("\n\n") | |
`slack-noti "#{message}"` | |
end | |
end | |
rescue StandardError | |
end | |
# Print memory usage every 15 minutes (using SIGINFO) | |
fifteen_minutes_elapsed = i % (15 * 60 / SLEEP) == 0 | |
Process.kill 'INFO', Process.pid if fifteen_minutes_elapsed | |
sleep SLEEP | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install and run:
Recommended: slack-noti.