views:

295

answers:

3

Hi There,

I've started working with the rapidshare.com API. I'm just wondering what is the best way to read the reply from and API call.

To be honest I think the API is all over the place. Some replies are comma delimited which is fine. I'm having with problems with the account info response. This is not comma delimited and the fields may not always be in the same order.

Here is a sample response: accountid=123456 type=prem servertime=1260968445 addtime=1230841165 validuntil=1262377165 username=DOWNLOADER directstart=1 protectfiles=0 rsantihack=0 plustrafficmode=0 mirrors= jsconfig=1 [email protected] lots=0 fpoints=12071 ppoints=10 curfiles=150 curspace=800426795 bodkb=5000000 premkbleft=23394289 ppointrate=93

I'm thinking that regex is the way to go here. Here is my expression that seems to cath all responses that contain values: (accountid|type|servertime|addtime|validuntil|username|directstart|protectfiles|rsantihack|plustrafficmode|mirrors|jsconfig|email|lots|fpoints|ppoints|curfiles|curspace|bodkb|premkbleft|ppointrate|refstring|cookie)\=[\w._@]+

If the order of data is to be considered random then how do I determine which value is which?

I'm just curious as to how everybody else is working with this.

Thanks,

Conor

A: 

i assume c#.

string[] s = @"accountid=123456 type=prem servertime=1260968445 addtime=1230841165 validuntil=1262377165 username=DOWNLOADER directstart=1 protectfiles=0 rsantihack=0 plustrafficmode=0 mirrors= jsconfig=1 [email protected] lots=0 fpoints=12071 ppoints=10 curfiles=150 curspace=800426795 bodkb=5000000 premkbleft=23394289 ppointrate=93".Split(" ");
var params = new Dictionary<string, string>();
foreach(var l in s)
{
 var tmp = l.Split("=");
 params[tmp[0]] = params[tmp[1]];
}

(it may contain bugs.. but the idea is obvious?)

Yossarian
I am new to the dictionary idea but it looks pretty cool. I'll give it a go now.Thanks Guys!
Conor H
A: 

You probably want to split this up into a Dictionary object of some kind, so that you can access the value by a key.

Here's an example of a C# console application that works with .NET 3.5:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace SO
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = @"accountid=123456 type=prem servertime=1260968445";

            string pattern = @"(?<Key>[^ =]+)(?:\=)(?<Value>[^ ]+)(?=\ ?)";

            Dictionary<string, string> fields = 
                (from Match m in Regex.Matches(input, pattern)
                   select new
                   {
                       key = m.Groups["Key"].Value,
                       value = m.Groups["Value"].Value
                   }
                ).ToDictionary(p => p.key, p => p.value);

            //iterate over all fields
            foreach (KeyValuePair<string, string> field in fields)
            {
                Console.WriteLine(
                    string.Format("{0} : {1}", field.Key, field.Value)
                );
            }

            //get value from a key
            Console.WriteLine(
                string.Format("{0} : {1}", "type", fields["type"])
            );

        }
    }
}

Link to another example in PHP:

http://stackoverflow.com/questions/1094889/how-to-use-rapidshare-api-to-get-account-details-php-question

Even Mien
A: 

Here is what I have done.

It's basically just a working version the code from Yossarian.

            // Command to send to API
        String command = "sub=getaccountdetails_v1&type=prem&login="+Globals.username+"&password="+Globals.password;

        // This will return the response from rapidshare API request.
        // It just performs @ webrequest and returs the raw text/html. It's only a few lines. Sorry I haven't included it here.
        String input  = executeRequest(command);

        input = input.Trim();
        string[] s = input.Split('\n');

        Dictionary<string,string> terms = new Dictionary<string, string>();
        foreach(var l in s)
        {
             String[] tmp = l.Split('=');
             terms.Add(tmp[0], tmp[1]);
        }
        foreach (KeyValuePair<String, String> term in terms)
        {
            txtOutput.Text += term.Key + " :: " + term.Value+"\n";
        }

Thanks for your help guys.

Conor H