Class: Twitter::FriendsTimeline

Inherits:
Site
  • Object
show all
Defined in:
lib/twitter.rb

Instance Attribute Summary

Attributes inherited from Site

#posts

Instance Method Summary collapse

Methods inherited from Site

#balance, #credentials, #credentials=, #imported_links, #initialize, #save!, #to_post, #to_s, #undo_import!

Constructor Details

This class inherits a constructor from Site

Instance Method Details

#ask_for_credentialsObject



93
94
95
96
97
98
99
# File 'lib/twitter.rb', line 93

def ask_for_credentials
  puts "#{name} user name:"
  user = STDIN.gets.strip
  puts "#{name} password:"
  pass = STDIN.gets.strip
  self.credentials = {"username" => user, "password" => pass}
end

#date(post) ⇒ Object



88
89
90
91
# File 'lib/twitter.rb', line 88

def date(post)
  @date_cache ||= Hash.new { |h,k| h[k] = Time.parse(k) }
  @date_cache[post['created_at']]
end

#get_tweets(query) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/twitter.rb', line 74

def get_tweets(query)
  begin
    res = Twitter.get("/favorites.json", :query => query, :basic_auth => {:username => credentials["username"], :password => credentials["password"]})         
    raise "Error fetching timeline: '#{res['error']}'" if res.is_a?(Hash) && res['error']
    res
  rescue Crack::ParseError
    raise # TODO
  end
end

#identifier(post) ⇒ Object



84
85
86
# File 'lib/twitter.rb', line 84

def identifier(post)
  post["id"]
end

#nameObject



18
19
20
# File 'lib/twitter.rb', line 18

def name
  "twitter"
end

#to_posts(res) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/twitter.rb', line 49

def to_posts(res)
  urls = res["text"].scan(/(http:\/\/[^,()" ]+)/).flatten.map { |u| u.gsub(/[\.:]+\Z/, '') }.uniq
  ats = res["text"].scan(/@([[:alnum:]]+)/).flatten
  hashtags = res["text"].scan(/#([[:alnum:]]+)/).flatten
  retweet = res["text"] =~ /RT[^a-zA-Z]/ || res["text"] =~ /\(via @[^)]+\)/

  urls.map do |url|
    post = @gourmand.bookmark_for(url)
    post.tags = [
      "via:twitter",
      Post::NEW_MARKER,
      ats.map{|a| "to:" + a}.sort,
      "from:#{res["user"]["screen_name"]}",
      hashtags,        
      ("retweet" if retweet),
      post.tags
    ].compact.flatten.join(" ").strip

    post.extended = "@#{res["user"]["screen_name"]}: \"#{res["text"]}\" \n (from http://twitter.com/#{res["user"]["screen_name"]}/status/#{res["id"]})"
    post.dt = date(res).strftime("%Y-%m-%dT%H:%M:%SZ")

    post
  end
end

#update!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/twitter.rb', line 22

def update!
  puts "Updating twitter"
  balance

  results = {}

  new_tweets = nil

  page = 0 

  query = {:count => 200 }

  while !(new_tweets = get_tweets(query)).empty?
    new_tweets.reject!{|t| @ids.include? identifier(t) }
    break if new_tweets.empty?
    new_tweets.each { |t| results[identifier(t)] = t }
    puts "importing twitter page #{query[:page] || 0}"
    query[:page] = (query[:page] || 0) + 1
  end 

  @posts += results.values

  results.values.map do |res|              
    to_posts(res)      
  end.flatten
end