Skip to content

Instantly share code, notes, and snippets.

@MadBomber
Created December 4, 2011 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadBomber/1428645 to your computer and use it in GitHub Desktop.
Save MadBomber/1428645 to your computer and use it in GitHub Desktop.
A way to manage RubyGems worthy of investigation using Twitter's Favorite tweets
#!/bin/env ruby
###############################################
## extract_fav_gems.rb
## An html file obtained from twitter.com is passed
## on the command line. The file is the favorite tweets
## of a specific user. Tweets from RubyGems are reviewed
## abd the gem name extracted. If the gem is not currently
## installed it will be nominated for installation. Gems
## are not installed automatically. The gem install command
## line containing the nominated gems is printed to stdout
## so that the user can copy/paste as a command if desired.
usage_msg = <<EOS
Extract favorite tweets of nominated Ruby gems worth investigation
Usage: #{$0} <filename>
where <filename> is the complete path to an HTML file obtained
twitter.com of the list of favorite tweets.
EOS
if ARGV.empty?
puts usage_msg
exit
end
require 'pathname'
require 'systemu'
signal_line = 'rubygems</a>'
favorites_file = Pathname.new ARGV[0]
unless favorites_file.exist?
puts "\nERROR: #{favorites_file} does not exist"
puts usage_msg
exit(-1)
end
fav_gems = Array.new
extract_next_line = 0
puts "Extracting RubyGem Names from Webpage"
favorites_file.readlines.each do |a_line|
extract_next_line -= 1
if 0 == extract_next_line
gem_name = a_line.split()[0]
fav_gems << gem_name unless fav_gems.include? gem_name
elsif a_line.chomp == signal_line
# MAGIC: 3 is how many lines to skip after finding the signal line before
# the line that contains the gem name is found.
extract_next_line = 3
end
end
puts "Obtaining Installed Gem Names"
a,gem_list,c = systemu 'gem list'
installed_gems = Array.new
gem_list.split("\n").each do |a_line|
installed_gems << a_line.split[0]
end
gem_install_command = "gem install"
fav_gems.each do |gem|
gem_install_command += " " + gem unless installed_gems.include? gem
end
# MAGIC: -2 accounts for the constant 'gem' and 'install' tokens
puts "Done. Found #{gem_install_command.split.length - 2} new gems worth investigating."
puts
puts gem_install_command
puts
#!/bin/bash
# fav_tweets
# adapted by @MadBomber from
# Twitter status update bot by http://360percents.com
# Author: Luka Pusic <pusic93@gmail.com>
# Limitations: only gets the first page of favorite tweets
# https://mobile.twitter.com/favorites
#REQUIRED PARAMS
username="your_accout@email.com" # your eMail account known to twitter
password="mysecretpassword" # your secret password used to login to twitter
temp_favorites_file=$HOME/tmp/favorites.html
#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests if necessary
touch "cookie.txt" #create a temp. cookie file
#INITIAL PAGE
echo "[+] Fetching twitter.com..." && sleep $sleeptime
initpage=`curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new"`
token=`echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//'`
#LOGIN
echo "[+] Submitting the login form..." && sleep $sleeptime
loginpage=`curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session"`
#HOME PAGE
echo "[+] Getting your twitter home page..." && sleep $sleeptime
homepage=`curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "http://mobile.twitter.com/"`
#Retrieve
echo "[+] Getting favorites..." && sleep $sleeptime
tweettoken=`echo "$homepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1`
favorites=`curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" -d "authenticity_token=$tweettoken" "https://mobile.twitter.com/favorites"`
echo $favorites | sed 's/>/>\n/g' - > $temp_favorites_file
#LOGOUT
echo "[+] Logging out..."
logout=`curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "http://mobile.twitter.com/session/destroy"`
rm "cookie.txt"
# the extract_fav_gems.rb file must be in PATH and executable
extract_fav_gems.rb $temp_favorites_file
rm $temp_favorites_file
@MadBomber
Copy link
Author

On twitter I follow @rubygems inorder to watch for updated and newly published gems which might be worthy of investigation. If a description catches my eye, I will review the documentation and if its worthy I will mark the tweet as a "favorite" within twitter. Having done that, at some time in the future when I feel like playing, I will install these gems using the scripts in this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment