Using TumblrJ to read
With the current implementation of TumblrJ working properly for straight reads of different types, I figured I’d show a quick demo of the api usage (without Spring – I’ll post something about that later).
The api revolves around a single class called TumblrService. The class pretty much acts like a facade hiding the implementation details. The TumblrService needs a couple things to work properly:
- A TumblrJProperties instance
- An implementation of the ITumblrReader interface. The api includes the only one possible at this point which is an HTTP specific implementation.
Configuration
The TumblrJProperties class automatically initializes itself by looking for a file called tumblrj-config.properties in the classpath. You can override the name by using the 1 arg constructor. For now, the file needs to be found in the classpath.
The file contains basic information about paths, url’s, … The contents should look something exactly like:
base.url=http://{0}.tumblr.com
path.read=/api/read
path.authenticate=/api/authenticate
path.delete=/api/delete
Since most of these properties are pretty much standard, I’ll implement a defaults mechanism so that they aren’t mandatory.
Connection options
The other object needed to use the TumblrService and TumblrHttpReader classes is a TumblrConnectionOptions instance. At a very minimum, you’ll need to enter the name of your tumblr log (usualy, the first part in your tumblr.com url). If you want to access private posts, you’ll want to put in your email and password.
And for those who want fine grained control over the connection options, the object can also accept a org.apache.commons.httpclient.params.HttpClientParams instance which will be used on every http call.
Full Example
TumblrConnectionOptions connectionOptions = new TumblrConnectionOptions();
connectionOptions.setName("lama");
TumblrHttpReader reader = new TumblrHttpReader();
TumblrJProperties properties = new TumblrJProperties();
reader.setProperties(properties);
reader.setTumblrConnectionOptions(connectionOptions);
TumblrService service = new TumblrService();
service.setReader(reader);
TumblrReadOptions readOptions = new TumblrReadOptions();
readOptions.setStart(1);
readOptions.setNum(1);
TumbleLog log = service.read(readOptions);
Explanation
Lines 1 through 11 were pretty much explaines. Lines 12, 13 and 14 are interesting in that they let you configure the read options by letting you filter the data. The available options include:
- Wether or not to include private posts
- The start and number of posts to return (for paging and limiting results)
- The id of the post
- The type of post
Check out the TumblrReadOptions class for details.
Line 13 is the line where the read is executed and a TumbleLog object returned. That object will include a List of TumblePost objects as well as a gamut of other information concerning the returned results. As mentionned before, the test package contains a lot of usefull unit tests showing off how to use the different read options.
| Print article |
about 1 year ago
Hi,
How can I configure to read data from multiple tumbler site using the API.
about 1 year ago
Hi Mano,
You could simply create 2 (or more) different TumblrJService instances. For each service, you’d need to manually set 1) the blog name 2) The TumblrHttpReader instance.
Each TumblrHttpReader instance would then simply point to a different properties file for it’s configuration.
about 1 year ago
Hi;
Thank you very much for providing answer.
But I need to read data from various(more than 100) tumbler sites using this API. How can I achieve this.
In tmblerj property file I can only set one base url for one site at a time.
The option you provided is only feasible for some couple of sites.My site list will increase day by day, so the option you provided is not feasible for my requirement.Is there any other way to do this ? Or is the jar changed to adhere to this specific requirement in future ?
about 1 year ago
Mano,
You could simply use the TumblrHttpReader class directly and avoid any auto-configuration. Therefore, avoid the TumblrJService class.
1) When you instantiate a new instance of TumblHttpReader, it will automatically instantiate itself using a default properties file.
2) You’ll need to set your connection options manually (it can’t be null. Therefore a call to reader.setTumblrConnectionOptions() is necessary.
3) From there, just call up the read() method in your reader instance passing in a manually built Credentials object as well as a manually built read options object.
Obviously, it’s more work, but not much. Instead of being abstracted away using the TumblrJService class (which after all is just a convenience class), you’ll be coding to the reader class directly.
about 1 year ago
Thanks for your sugesstion.
Then I need to use TumblrConnectionOptions.setName(“XXX”) for each and every tumblr sites as I have multiple sites. Right ?
Do we have any java API documentation.
Is there any way to handle moer than one tumbler site in batch mode ?
about 1 year ago
That’s right. You’ll have one instance of the connection options and reader for each site you want to access.