Class: Ubiquitously::Digg::Post

Inherits:
Service::Post show all
Defined in:
lib/ubiquitously/services/digg.rb

Instance Attribute Summary

Attributes inherited from Service::Post

#captcha, #categories, #description, #downvotes, #extension, #format, #image, #kind, #must_be_unique, #privacy, #rating, #remote, #service_id, #service_url, #slug, #source, #source_url, #state, #status, #tags, #title, #token, #upvotes, #url, #user, #vote

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service::Post

#access_token, #initialize

Methods included from Loggable::Post

included

Methods included from Restful::Post

included

Methods included from Postable::Post

included

Methods included from Ownable::Post

included

Methods inherited from Base

#apply, #debug?

Methods included from SubclassableCallbacks

included, override

Constructor Details

This class inherits a constructor from Ubiquitously::Service::Post

Class Method Details

.find(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ubiquitously/services/digg.rb', line 106

def find(options = {})
  url = options[:url]
  raise ArgumentError.new("Please give #{service} a url") if url.blank?
  urls = url_permutations(options[:url])
  user = options[:user]
  records = []
  # search = "http://services.digg.com/1.0/endpoint?method=search.stories&query=#{url}"
  link = "http://services.digg.com/1.0/endpoint?method=story.getAll&link=#{url}"
  
  xml = Nokogiri::XML(user.agent.get(link).body)
  
  xml.css("story").each do |node|
    service_url = node["href"]
    service_id  = node["id"]
    url         = node["link"]
    votes       = node["diggs"].to_i
    title       = node.css("title").first.text
    description = node.css("description").first.text
    categories  = [node.css("container").first["name"]] rescue []
    categories  << node.css("topic").first["name"] rescue nil
    categories.flatten!
    record = new(
      :title => title,
      :url => url,
      :description => description,
      :categories => categories,
      :service_url => service_url,
      :service_id  => service_id,
      :upvotes => votes,
      :user => user
    )
    records << record if urls.include?(record.url)
  end
  
  records.sort! { |a, b| b.upvotes <=> a.upvotes }
  
  if options[:url]
    records.first
  else
    records
  end
end

Instance Method Details

#createObject



19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ubiquitously/services/digg.rb', line 19

def create
  # url
  page        = agent.get("http://digg.com/submit/")
  form        = page.forms.detect {|form| form.form_node["id"] == "thisform"}
  
  form["url"] = url
  form.radiobuttons_with(:name => "type").each do |checkbox|
    checkbox.check if checkbox.value.to_s == "0"
  end
  
  page = form.submit
  
  key = page.parser.css("input#key").first["value"]
  
  # http://digg.com/submit/processing?key=171a6601a6690e7105122a4f02242dd6&type=Crawl
  # redirects you to "http://digg.com/submit/details?key=#{key}"
  page = agent.get("http://digg.com/submit/dupes?key=#{key}", [], "http://digg.com/submit/processing?key=#{key}type=Crawl")
  
  form["title"] = title
  form["body"] = description # 350 chars max
  form["topic"] = "26"
  
  captcha_image = page.parser.css("img.captcha").first["src"]
  captcha_id    = page.parser.css("input#captchaid").first["value"]
  # open browser with captcha image
  # need to pass cookies
  image = agent.get("http://digg.com#{captcha_image}")
  image_path = "digg-image-#{key}.jpg"
  
  # instead of writing it, you could do a trick to
  # post a url with javascript that adds the cookie information, and then
  # redirects you to the image url:
  #   path = http://some-site.com/?code="<script>document.cookie = #{agent.cookies}</script>"
  #   system("open", path)
  File.open(image_path, "w+") { |f| f.puts image.body }
  system("open", image_path)
  
  # enter captcha response in terminal
  captcha_says = ask("Enter Captcha from Browser Image:  ") { |q| q.echo = true }
  File.delete(image_path) if File.exists?(image_path)
  
  form["captchaid"] = captcha_id
  form["captchatext"] = captcha_says
  form["thumbnail"] = image if image
  
  unless debug?
    form.action = "http://digg.com/submit/details?key=#{key}"
    page = form.submit
  end
  # has captcha, not done with this
  true
end

#updateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ubiquitously/services/digg.rb', line 72

def update
  key = nil
  page = agent.get(remote.service_url)
  page.parser.css("script").each do |script|
    if script["src"] =~ /http:\/\/digg\.com\/dynjs\/loader/
      key = agent.get(script["src"]).body.match(/tokens\.digg\.perform\s+=\s+"([^"]+)"/)[1]
    end
  end
  location = page.body.match(/D\.meta\.page\.type\s+=\s+"([^"]+)"/)[1]
  headers = {
    "X-Requested-With" => "XMLHttpRequest",
    "Accept" => "application/json, text/javascript",
    "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8",
    "Pragma" => "no-cache",
    "Cache-Control" => "no-cache",
    "Accept-Encoding" => "gzip,deflate",
    "Referer" => self.remote.service_url
  }
  params = {
    "location" => location,
    "token" => key,
    "itemid" => remote.service_id
  }
  
  begin
    page = agent.post("http://digg.com/ajax/digg/perform", params, headers)
  rescue Exception => e
    puts e.page.body
  end
  
  true
end