Class: Ubiquitously::Dzone::Post

Inherits:
Service::Post show all
Defined in:
lib/ubiquitously/services/dzone.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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ubiquitously/services/dzone.rb', line 87

def find(options = {})
  url = options[:url]
  user = options[:user]
  urls = url_permutations(url)
  records = []
  
  for page in 1..10
    search = "http://dzone.com/links/search.html?query=#{URI.parse(url).host}&type=html&p=#{page}"
    html = user.agent.get(search).parser
    
    html.css(".linkblock").each do |node|
      service_id = node["id"].match(/(\d+)/)[1]
      link = node.css(".thumb a").first["href"]
      header = node.css(".details h3 a").first
      title = header.text
      upvotes = node.css(".vwidget .upcount").first.text.to_i
      downvotes = node.css(".vwidget .downcount").first.text.to_i
      service_url = "http://www.dzone.com#{header["href"]}"
      record = new(
        :url => link,
        :title => title,
        :upvotes => upvotes,
        :downvotes => downvotes,
        :service_url => service_url,
        :service_id => service_id,
        :user => user
      )
      if urls.include?(record.url)
        records << record
        break
      end
    end
    
    break if records.length > 0
  end
  
  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
# File 'lib/ubiquitously/services/dzone.rb', line 19

def create
  # url
  page        = agent.get("http://www.dzone.com/links/add.html")
  form        = page.form_with(:action => "/links/add.html")
  
  accepted_tags = []
  
  form.checkboxes_with(:name => "tags").each do |tag|
    accepted_tags << tag.value.to_s
  end
  
  unaccepted_tags = token[:categories].select { |tag| !accepted_tags.include?(tag) }
  
  if unaccepted_tags.length > 0
    raise ArgumentError.new("DZone doesn't accept these tags: #{unaccepted_tags.inspect}, they want these:\n#{accepted_tags.inspect}")
  end
  
  form["title"] = title
  form["url"] = url
  form["description"] = description
  
  form.checkboxes_with(:name => "tags").each do |checkbox|
    checkbox.check if token[:categories].include?(checkbox.value)
  end
  
  unless debug?
    page = form.submit
  end
  
  true
end

#updateObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ubiquitously/services/dzone.rb', line 51

def update
  page = agent.get(remote.service_url)
  params = "\ncallCount=1\n"
  params << "c0-scriptName=LinkManager\n"
  params << "c0-methodName=incrementVoteCount\n"
  # var random = Math.floor(Math.random() * 10001);
  # var id = (random + "_" + new Date().getTime()).toString();
  # => 9031_1281646074042
  js_time = (Time.now.utc - Time.utc(1970, 1, 1)).floor * 1000
  params << "c0-id=#{rand(10001).to_s}_#{js_time.to_s}\n"
  params << "c0-param0=number:#{remote.service_id}\n"
  params << "c0-param1=number:1\n"
  params << "c0-param2=boolean:false\n"
  params << "xml=true\n"
  headers = {
    "Content-Type" => "text/plain; charset=UTF-8",
    "Referer" => remote.service_url,
    "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Encoding" => "gzip,deflate",
    "X-Requested-With" => "XMLHttpRequest",
    "Pragma" => "no-cache",
    "Cache-Control" => "no-cache"
  }
  result = agent.post("http://www.dzone.com/links/dwr/exec/LinkManager.incrementVoteCount.dwr", params, headers)
  
  # now add comment
  form = page.form_with(:name => "addlinkform")
  form["body"] = description
  
  form.submit
  
  true
end