Pages

Thursday, 20 February 2014

ubiquitously-自动提交博客帖子到书签网站

Be everywhere at once

There are 100's of Social Bookmarking sites out there. And while most of them are not worth your time, and even less are actually in the field you blog about, there's still probably 5 or 10 good social bookmarking services that you want to post your content to.
The problem is, it seems like most of the social bookmarking services do not have API's. Understandably so. If they did, they might get a lot of spam. But what about for us people that don't want to spam but still want automation? That's where Ubiquitously comes in.
yuˈbɪkwɪtəs: existing or being everywhere at once
In a matter of seconds, I published this post to 5 services by running rake ubiquitously, check them out:

Usage

Install

1
sudo gem install ubiquitously

Setup Social Bookmarking Configuration

Because this is done with web scraping, it's basically just mimicking user actions. So you need to tell Ubiquitously your username and password for all the services you'll use. The simplest way is to create a YAML file like this:
1
2
3
4
5
6
7
dzone:
  key: viatropos
  secret: asdfasdf
reddit:
  key: viatropos
  secret: asdfasdf
# ...
And plug that into Ubiquitously like this:
1
Ubiquitously.configure("path/to/config.yml")
You can also pass it a Hash, which is the end result:
1
Ubiquitously.configure(:dzone => {:key => "viatropos", :secret => "asdfasdf"}) #...

Automatically Post to Social Bookmarking Services

Now that you've configured your social bookmarking services with Ubiquitously, you can easily create Posts for them (currently 7). They all have a different set of forms and names for the Post attributes you are specifying, but in the end it boils down to url, title, description, tags, and categories, and some have all of them while others don't.
Below is a snippet for posting to Dzone, Faves, Mixx, Newsvine, Propeller, Reddit, and StumbleUpon, all of which don't have an API. Whenever you call Post.create for a service, Ubiquitously creates a background User object and logs them in so it looks like you. It then uses the attributes you passed in to fill out the forms.
require 'rubygems'
require 'ubiquitously'

post = {
  :title       => "A Ubiquitous Post",
  :description => "I'll be in 10 places at once",
  :tags        => ["ubiquitous", "omnipresent", "ruby"],
  :url         => "http://ubiquitously.me/a-ubiquitous-post"
}

Ubiquitously::Dzone::Post.create(post)
Ubiquitously::Faves::Post.create(post)
Ubiquitously::Mixx::Post.create(post)
Ubiquitously::Newsvine::Post.create(post)
Ubiquitously::Propeller::Post.create(post)
Ubiquitously::Reddit::Post.create(post)
Ubiquitously::StumbleUpon::Post.create(post)

Caveats

There's a few gotchas currently.
First is that Mixx requires you to fill out a captcha. This is solved in a neat way, try it out. Captchas are pretty easy to get around.
Second, if you login too many times, which it's currently doing, some services will say "Max Login Failure". So Ubiquitously needs to store cookies in a cookie jar (mechanize), a.k.a a YAML file.
Third, sometimes the services may not be responsive, so you have to watch for timeouts. Only on the lesser services.

How it works

Everything is built around Mechanize and Nokogiri, both led by Aaron Patterson.
Many social bookmarking services do not have API's in order to prevent spammers from ruining their system. But what about those of us that actually create content several times a day and want automation? We're out of luck.
Ubiquitously solves that problem by creating a simple ActiveRecord-like API around some services I need to publish to now, and hopefully it will grow as you guys need more. The goal is to be semi-low-level and not to provide a full featured api to a service, as some services already have very well-done API's in Ruby。

from http://code.lancepollard.com/introducing-ubiquitously-for-ruby