Tag Archive > api

Downloading Facebook Albums with C#

» 30 March 2011 » In C#, Open-Source, Programming, Releases » 3 Comments

If you can remember, I already posted a script that does the same job (downloading facebook albums) written in Perl. Sadly, the script doesn’t work anymore, and because of the bad practices I have applied to it, I felt lazy updating. So instead, I created another project. This time, it’s on C#. So the project has a GUI, which most of the users will love.

I actually started this 20 days ago. I didn’t have the chance to work on this until yesterday night. What it does is pretty simple. It uses the Facebook Graph API, so there would be a great, great chance that this will not fail. So let us take a look at the process.

ADown Flowchart

ADown Flowchart

To be honest, this is the first time I used a flowchart on my projects. At first I was not convinced by this, but then I just realized this is one of the best ways to explain how a process takes action.

I also used an external JSON parser (since most of what I found are serializers). Credits goes here for the parser.

Here’s the screenshot of the GUI:

ADown Screenshot

ADown Screenshot

There are some notes I have to mention:

  • Downloading speed will depend on the connection speed
  • The download folder by default is in the Documents folder named ‘ADown’
  • Each picture will be named based on the Photo ID in the Graph API

And of course, the features:

  • Threaded
  • Verbose
  • Uses Facebook Graph API
  • Able to change download folder
  • Creates a folder for every album
  • Accepts the Album URL (easy copy-paste)

If you have no permission in that album (although it’s visible to you in Facebook) you won’t be able to access it in the Graph API. An example would be a public album of someone who’s not in your friend list.

The source code of course is freely available at github and released under BSD 3-clause license. Help me maintain it, by forking. :)

The executable file is also available, that is if you do not have a VS2010 installed. You need to install .NET Framework 4 first. Download the executable file here.

That’s all for this project, please leave comments if you have something in mind. Thank you.

Continue reading...

Tags: , , , , , , , ,

Upscreen: A Screenshot Utility using the Min.us API

» 31 December 2010 » In C/C++, Open-Source, Programming, Releases » 3 Comments

It’s been a long time since I posted something, again. Well actually I was hospitalized for 6 days, from December 18 to 24. I nearly spent Christmas the hospital. And after the hospital days, I worked on 2 projects, one is paid, and the other one is this.

The Story

Min.us is a great image hosting website. And when the time they sent me their API (I asked; their API wasn’t public yet that time) I came up with an idea, and yes basically the idea is to upload the screenshot taken in a computer to the min.us image hosting. When I finished the first (paid) project, I switched to this one. And I started with finding a way to execute code when the user pressed the PrintScreen key. I thought of using Windows Hooks but later on, I found out about RegisterHotKey and used it instead. But there’s a little problem, when I registered PrintScreen as a hotkey. Just like:

RegisterHotKey(NULL, 1, NULL, 0x2C);

The normal function of the key is disabled. This is bad, and I probably really need to use a keyboard hook instead. But no, instead I changed the hotkey and set it to Win + PrintScreen.

RegisterHotKey(NULL, 1, MOD_WIN | MOD_NOREPEAT, 0x2C);

So yes, it’s working great now. Though the PrintScreen key must be pressed first to load the image to the clipboard. So if you’re going to upload a screenshot, Press PrintScreen, then Win + PrintScreen. The good thing caused by this is, you can choose what to upload, and just place it in your clipboard. Say you need to upload an image from the internet, copy it, and then press Win + PrintScreen. Sweet.

Next, I need to get the data from the clipboard, and that was easy using GetClipboardData. Then, I need to save the bitmap file from the clipboard. I had some trouble finding a way to do this, so I used a function named CreateBMPFile and CreateBitmapInfoStruct. Success, now originally I was planning to use the BMP image and upload it right away. Then I just realized, the BMP file generated was uncompressed. So I think I need to use PNG instead (No not JPG, never). For this conversion I did some research. I ended up on using the ATL image header, and the CImage class. Well basically, converting an image is just 3 lines:

void Upscreen::ConvertToPNG(LPTSTR &sourceName, LPTSTR &destName) {
	CImage img;
	img.Load(sourceName);
	img.Save(destName);
}

Now that I have my PNG image, its time to upload it. By this time, Min.us have their API published and you can view it here. I only used two methods, CreateGallery and UploadItem. And I created a C++ class for the API:

class Minus {
public:
	std::string editorId, id, readerId;

	Minus();
	virtual ~Minus();
	void CreateGallery();
	void UploadItem(std::string&);

private:
	std::string key;
};

It’s incomplete, because the other two methods wasn’t included. So basically we need to get the editor_id, reader_id, and key from the first method then use these variables on the second method. To get these variables, an HTTP GET request must be accomplished. I do not want to use too much external libraries, so I just used Winsock2. I didn’t have too much trouble performing this, and the only issue I had was separating the HTTP headers from the response body. And later on, I found out its very easy:

response = response.substr(response.find("\r\n\r\n"));

The API’s response content type is JSON. I found it hard finding a good JSON parser in C++. And I ended using JSON Spirit . The downside is, this uses Boost Headers, so both must be downloaded.

I got the variables I need, now to the HTTP POST request. This is the bloodiest part of the program. I had trouble reading the the PNG file, and used ReadFile instead. Then, I didn’t know std::string is not suitable for storing binary data. So after switching many data types, I finally decided to use just a char * pointer to hold the data. And to pass the data, I put the POST headers and the data to a std::vector variable. All went well, but I’m a having 404 error in the POST request, and the data is incomplete. I just figured out the example URL in the Min.us API documentation is wrong. The trailing slash after UploadItem is causing the 404 error, and removing it fixed the problem. The other one was caused by the Content-Length parameter. I used WireShark and LiveHTTPHeaders to trace the packets and again, some bloody debugging. Later on, I found out the number I’ve been passing exceeded by one. After this part, all went so easy. The only thing I have to deal with is validation. Whenever the clipboard has data other than an image, it crashes. So I just placed an if condition to check if the PNG data exist. Because no PNG data will be saved if there’s no valid bitmap file present.

After uploading, I used the editor_id variable and attached it to the min.us URL. And made the program open it at the default browser using ShellExecute.

Header Files

The header files I used in this project are:

#include <WinSock2.h>
#include <windows.h>
#include <atlimage.h>
#include <string>
#include <json_spirit\json_spirit_reader_template.h>

Source Code

The source code is released under the GNU GPL v3. Get it here.

Year Ends

So this became my last post for 2010. Very timely, isn’t it? Anyway, Happy New Year!

Continue reading...

Tags: , , , , , ,