Monday, July 7, 2014

Retrieve data from Distimo - Mobile Application

Below is the code to retrieve mobile application downloads data from Distimo API

public void Main()
{
// Returns assets in json format
string api = "https://api.distimo.com/v4/downloads";

DateTime FromDate = (DateTime)Dts.Variables["User::FromDate"].Value;
string s = FromDate.ToString("yyyy-MM-dd");
DateTime ToDate = (DateTime)Dts.Variables["User::ToDate"].Value;
string s1 = ToDate.ToString("yyyy-MM-dd");


//string query = "format=csv&from=2010-12-01&to=2010-12-31&breakdown=application,appstore";
string query = "format=csv" + "&" + "from=" + s + "&" + "to=" + s1 + "&breakdown=application,appstore";

// Necessary variables

string publicKey = (string)Dts.Variables["User::PublicKey"].Value;
string privateKey = (string)Dts.Variables["User::PrivateKey"].Value;
string userName = (string)Dts.Variables["User::UserName"].Value;
string password = (string)Dts.Variables["User::Password"].Value;

// Identify application
int time = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
string data = String.Concat(query, time);
HMACSHA1 hmac = new HMACSHA1(Encoding.ASCII.GetBytes(privateKey));
hmac.Initialize();

byte[] buffer = Encoding.ASCII.GetBytes(data);
string hash = BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();
string user = String.Concat(userName, ":", password);
string base64Login = Convert.ToBase64String(Encoding.Default.GetBytes(user));

// Make the request
string url = api + "?" + query + "&apikey=" + publicKey + "&hash=" + hash + "&t=" + time;
WebRequest request = WebRequest.Create(url);
request.Headers["Authorization"] = String.Concat("Basic ", base64Login);

//MessageBox.Show(url);

string result;
try
{
result = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
}
catch (WebException e)
{
result = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
}

System.IO.File.WriteAllText(@"D:\MobileApp_Distimo\MobileApp_Downloads.txt", result);

Dts.TaskResult = (int)ScriptResults.Success;
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.