Skip to content

Instantly share code, notes, and snippets.

@igrep
Created April 19, 2011 07:26
Show Gist options
  • Save igrep/926962 to your computer and use it in GitHub Desktop.
Save igrep/926962 to your computer and use it in GitHub Desktop.
My twitter utils
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
module MyTwitterFavUtils
FORMAT = 'tsv'
SEP = "\t"
FILE = 'fav-tweets.' + FORMAT
BACKUP_SUFFIX = '~'
TRUNCATE_LEN = 20
end
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
$VERBOSE = true
=begin
Destroy all you favs on twitter.
=end
require 'rubygems'
require 'faster_csv'
require 'twitter'
require File.join(File.dirname(__FILE__), 'config')
include MyTwitterFavUtils
class String
PACK_UTF8 = 'U*'
def truncate_utf8 len
self.unpack( PACK_UTF8 )[0, len].pack PACK_UTF8
end
end
#Get oauth tokens. Change as you like.
Twitter.configure {|cfg|
ckey = File.readlines( 'ctoken.txt' ).map(&:chomp)
cfg.consumer_key, cfg.consumer_secret = ckey
username = ARGV[0]
akey = File.readlines( "atoken-#{username}.txt").map(&:chomp)
cfg.oauth_token, cfg.oauth_token_secret = akey
}
FasterCSV.foreach( FILE, :col_sep => SEP ){|row|
status_id = row[0].to_i
next if status_id < 1 #Maybe non-existing tweet.
begin
Twitter.favorite_destroy status_id
rescue Twitter::Error => err
warn err.message
else
puts "Successfully destroyed fav: '#{row[2]}: #{row[4].truncate_utf8 TRUNCATE_LEN}'"
end
}
#!/usr/bin/ruby -wKu
# vim: set fileencoding=utf-8 :
=begin
Download your favorite tweets.
=end
require 'pp'
require 'fileutils'
require 'rubygems'
require 'faster_csv'
require 'twitter'
require File.join(File.dirname(__FILE__), 'config')
include MyTwitterFavUtils
#constants
CRLF = "\x0d\x0a"
NEW_LINE = "\n"
AT = 'created_at'
USER = 'user'
NAME = 'screen_name'
ID = 'id_str'
WHAT = 'text'
username = ARGV[0]
pagenum = 1
#prepare the output file
puts "Creating a #{format} to output..."
begin
FileUtils.copy FILE, FILE + BACKUP_SUFFIX
puts "Backuped #{file}."
rescue Errno::ENOENT
puts "File #{file} doesn't exist... Creating."
end
out = FasterCSV.open file, 'w', :col_sep => SEP
END{ out.close }
out << [ 'status_' + ID, AT, NAME, 'user_' + ID, WHAT ]
loop do
favs = Twitter.favorites( username, :page => pagenum )
break if favs.empty?
favs.each do|tweet|
id = tweet[ID]
at = tweet[AT]
by = tweet[USER][NAME]
by_id = tweet[USER][ID]
what = tweet[WHAT].gsub( CRLF, NEW_LINE)
out << [ id, at, by, by_id, what ]
end
pagenum += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment