Skip to content

Instantly share code, notes, and snippets.

@fcingolani
Created November 20, 2011 02:44
Show Gist options
  • Save fcingolani/1379738 to your computer and use it in GitHub Desktop.
Save fcingolani/1379738 to your computer and use it in GitHub Desktop.
Ruby #nowplaying for Twitter: MPRIS & Web Intents.
#!/usr/bin/ruby
require 'rubygems'
require 'mpris'
require 'cgi'
require 'launchy'
require 'gdata'
#obtenemos la metadata
md = MPRIS.new.player.metadata
#si hay metadata
if not md.empty? then
#armamos el mensaje:
# #nowplaying TITULO by ARTISTA from ALBUM #GENERO
text = "#nowplaying \"#{md['title']}\" by #{md['artist']} from \"#{md['album']}\" ##{md['genre'].downcase}"
#ahora a buscar el video en Youtube
#iniciamos el cliente
youtube_client = GData::Client::YouTube.new
#armamos la query para la búsqueda
video_query = "#{md['artist']} - #{md['title']}"
#consultamos a Youtube y convertimos el resultado en XML
video_feed = youtube_client.get("http://gdata.youtube.com/feeds/api/videos?max-results=1&q=#{CGI::escape(video_query)}").to_xml
#buscamos el primer "link" tag con atributo "rel" valuado "alternate"
video_link = video_feed.get_elements("entry/link[@rel='alternate']").first;
#si encontramos un link
if video_link then
#obtenemos la url
video_url = video_link.attribute('href').value
#abrimos un navegador para preview
Launchy.open(video_url)
#agregamos el link de Youtube al Tweet
text += " #{video_url}"
end
#abrimos el Web Intent de Twitter
Launchy.open("https://twitter.com/intent/tweet?text=#{CGI::escape(text)}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment