package com.ved.musicmapapp.providers;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Pair;
import com.ved.musicmapapp.utils.Statics;
import com.ved.musicmapapp.utils.XMLParse;
public abstract class GetTrackOauthDetails extends
AsyncTask<Void, Void, Pair<String, String>> {
public static final String _7DIGITAL_API_LINK = "http://previews.7digital.com/clip/";
public static final String _7DIGITAL_API_TRACK_SEARCH = "track/search";
public static final String _7DIGITAL_API_KEY = "7dvgzk7twy2f";
public static final String _7DIGITAL_API_SECRET = "6f3kb7mw2uvzsyty";
public static final String _7DIGITAL_API_TRACK_VIEW = "track/preview";
public static final String _7DIGITAL_API_TRACK_DETAIL = "track/details";
private String mError;
private ProgressDialog mProgressDialog;
private Context context;
private String track;
private String artist;
private boolean showDialog;
public GetTrackOauthDetails(Activity context, String track, String artist,boolean showDialog) {
Log.i("check","In GetTrackOauthDetails");
mError = null;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Getting track link...");
mProgressDialog.setCancelable(true);
mProgressDialog
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
GetTrackOauthDetails.this.cancel(true);
}
});
this.context = context;
this.track = track;
this.artist = artist;
this.showDialog = showDialog;
}
@Override
protected Pair<String, String> doInBackground(Void... params) {
try {
/* StringBuilder url = new StringBuilder(_7DIGITAL_API_LINK);
url.append(_7DIGITAL_API_TRACK_SEARCH);
url.append("?pageSize=50&q=" + URLEncoder.encode(track.trim(), "UTF-8") + "-" + URLEncoder.encode(artist.trim(), "UTF-8"));
url.append("&oauth_consumer_key=" + _7DIGITAL_API_KEY);
url.append(_7DIGITAL_API_TRACK_SEARCH);
url.append("?pageSize=50&q=" + URLEncoder.encode(track.trim(), "UTF-8")) ;
url.append("&oauth_consumer_key=" + _7DIGITAL_API_KEY);
String country = context.getResources().getConfiguration().locale
.getCountry();
if (country != null && country.length() > 0)
url.append("&country=" + country);
Log.e("check","url for trackID:/// "+url);
*/
// create a consumer object and configure it with the access
// token and token secret obtained from the service provider
OAuthConsumer consumer = new DefaultOAuthConsumer(_7DIGITAL_API_KEY,
_7DIGITAL_API_SECRET);
// consumer.setTokenWithSecret(ACCESS_TOKEN, TOKEN_SECRET);
// create an HTTP request to a protected resource
URL url = new URL(_7DIGITAL_API_LINK+"/"+track);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
// sign the request
consumer.sign(request);
// send the request
request.connect();
System.out.println("Response: " + request.getResponseCode() + " "
+ request.getResponseMessage());
Pair<String, String> trackDetail = getTrackId("");
if (trackDetail == null) {
mError = "Track not found";
return null;
}
return trackDetail;
} catch (Exception e) {
e.printStackTrace();
mError = "Network unavailable!";
}
return null;
}
@Override
protected void onPreExecute() {
if (showDialog) {
mProgressDialog.show();
}
super.onPreExecute();
}
@Override
protected void onPostExecute(Pair<String, String> result) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
// Error
if (result == null) {
onError(mError);
} else {
onSucess(result);
}
super.onPostExecute(result);
}
public abstract void onSucess(Pair<String, String> trackDetail);
public abstract void onError(String error);
private Pair<String, String> getTrackId(String resultXml) {
Document doc = XMLParse.getDomElement(resultXml);
if (doc == null) {
return null;
}
String trackId = null, linkToBuySong = null;
boolean isFound = false;
Node results = doc.getFirstChild().getFirstChild();
NodeList childNodes = results.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeName().equals("totalItems")) {
int itemCount = Integer.parseInt(childNodes.item(i)
.getTextContent());
if (itemCount == 0) {
return null;
}
} else if (childNodes.item(i).getNodeName().equals("searchResult")) {
NodeList resultNodes = childNodes.item(i).getChildNodes();
for (int j = 0; j < resultNodes.getLength(); j++) {
if (resultNodes.item(j).getNodeName().equals("track")) {
trackId = resultNodes.item(j).getAttributes().item(0)
.getTextContent();
NodeList trackNodes = resultNodes.item(j)
.getChildNodes();
String title = "";
for (int k = 0; k < trackNodes.getLength(); k++) {
if (trackNodes.item(k).getNodeName()
.equals("title")) {
title = trackNodes.item(k).getTextContent();
if (!trackNodes.item(k).getTextContent()
.toLowerCase()
.contains(track.toLowerCase())
&& Statics.similarText(
trackNodes.item(k)
.getTextContent(),
track) < 75) {
break;
}
} else if (trackNodes.item(k).getNodeName()
.equals("artist")) {
String artistName = trackNodes.item(k)
.getChildNodes().item(0)
.getTextContent();
if (artistName.toLowerCase().contains(
artist.toLowerCase())
|| Statics.similarText(artistName,
artist) >= 75) {
Log.d("Track", title + " - " + artistName);
isFound = true;
}
} else if (trackNodes.item(k).getNodeName()
.equals("url")) {
linkToBuySong = trackNodes.item(k)
.getTextContent();
}
}
}
if (isFound) {
break;
}
}
}
if (isFound) {
break;
}
}
if (!isFound) {
return null;
}
return new Pair<String, String>(trackId, linkToBuySong);
}
@Override
protected void onCancelled() {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
super.onCancelled();
}
}