Env Variables:
LIST_URL POSTMARK_API_KEY FROM_EMAIL TRELLO_EMAIL TRELLO_LABEL
Run with: rake synchronize
source "https://rubygems.org" | |
ruby '2.2.2' | |
gem 'httparty' | |
gem 'nokogiri' | |
gem 'redis' | |
gem 'mail' | |
gem 'postmark' | |
gem 'rake' |
require 'httparty' | |
require 'nokogiri' | |
require 'redis' | |
require 'mail' | |
require 'postmark' | |
require 'redis' | |
require 'open-uri' | |
desc 'Synchronize youtube list with trello board' | |
task :synchronize do | |
list_page = Nokogiri::HTML.parse(HTTParty.get(ENV['LIST_URL'])) | |
videos = list_page.search('tr.pl-video.yt-uix-tile').map do |e| | |
{ | |
title: e.attribute('data-title').to_s, | |
link: 'http://www.youtube.com/' + e.search('a').first.attribute('href').text, | |
image_link: "http://" + e.search('img').first.attribute('data-thumb').text.gsub('//', '') | |
} | |
end | |
redis = Redis.new | |
videos_already_sync = redis.lrange('videos_list', 0, -1) | |
videos_to_sync = videos.select { |v| !videos_already_sync.include?(v[:link]) } | |
videos_to_sync.each do |video| | |
redis.rpush('videos_list', video[:link]) | |
email = Mail.new do | |
from ENV['FROM_EMAIL'] | |
to ENV['TRELLO_EMAIL'] | |
delivery_method Mail::Postmark, :api_token => ENV['POSTMARK_API_KEY'] | |
end | |
email.subject = "#{video[:title]} #{ENV['TRELLO_LABEL']}" | |
email.body = "Synced from Youtube to Trello. #{video[:link]}" | |
email['image.jpg'] = open(video[:image_link]) {|f| | |
File.open("whatever_file.jpg","wb") do |file| | |
file.puts f.read | |
end | |
} | |
email.deliver | |
puts "Synced #{video[:title]}" | |
end | |
end |