Class: Ubiquitously::Digg::Post
Instance Attribute Summary
#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
#access_token, #initialize
included
included
included
included
Methods inherited from Base
#apply, #debug?
included, override
Class Method Details
.find(options = {}) ⇒ Object
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 = []
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
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
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"]
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 form["topic"] = "26"
captcha_image = page.parser.css("img.captcha").first["src"]
captcha_id = page.parser.css("input#captchaid").first["value"]
image = agent.get("http://digg.com#{captcha_image}")
image_path = "digg-image-#{key}.jpg"
File.open(image_path, "w+") { |f| f.puts image.body }
system("open", image_path)
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
true
end
|
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]
= {
"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, )
rescue Exception => e
puts e.page.body
end
true
end
|