How to get URL of mp3 when it is playing - java

We can play mp3 files from browsers, Social Networks, Apps... I want to get URL of mp3 when it's playing. One way here is to create an extension in the browser. But in other apps we can't. Here is an example:
I go to some site and open some mp3:
If you can see the music is playing and it has a URL. I want to get this URL when music starts playing. How can I do that? And how can I get URL of music which is playing in some app? Is it possible?

If you can see the music is playing and it has a URL. I want to get
this URL when music starts playing. How can I do that?
If you are loading the website into a WebView, the easiest possible way you can get the URL by intercept the requests through WebViewClient and check if the URL contains '.mp3'. See the following example,
public class YourWebViewClient extends WebViewClient {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.contains(".mp3")) {
Log.d(TAG, "MP3 url = [" + url + "]");
}
return super.shouldInterceptRequest(view, request);
}
}
And how can I get URL of music which is playing in some app? Is it
possible?
Yes possible but not too easy especially if you are going to get it from SSL traffic. To sniff other app's network traffic you need to perform MITM. There are a couple of apps in the play store which are doing exactly the same thing. You can look into HttpCanary app for reference. Basically you need to perform the following steps -
Set up a proxy server
Configure your device to pass its network traffic to that proxy server
Ask the proxy server to give you the network data
Analyze the network data to see if it contains 'mp3' URL
If you need to intercept https traffic, you need to generate and install an SSL certificate.

First import the JSOUP library from maven
compile 'org.jsoup:jsoup:1.12.1'
after that use this code
Document doc = Jsoup.connect(url).get();
System.out.println(doc.title());
Elements h1s = doc.select(".jp-type-single");
System.out.println("Number of results: " + h1s.size());
for (Element element : h1s) {
String mp3Url = element.attr("data-xc-filepath");
System.out.println("mp3 url: " + mp3Url);
file_num++;
URLConnection conn = new URL(mp3Url).openConnection();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(new
File("/users/pelican/downloads/"+file_num+"file.mp3"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
now let me explain this
JSOUP fetched the webpage using your HTML URL
after that, it converts into doc
and after that, it selects the element by using a select method and here we are getting the first audio tag that we want to search.

Related

Upload image from android app gallery to local spring server

Last 3 days I started to learn Spring. I want to send a image from the phone gallery to the spring server. I want to mention that the server is local so I'm using localhost. I saw a tutorial that if I want to send stuff to a local server, the server address is my laptop address + the port (ex. 8080) and I have to connect the phone to the same Wi-Fi as the laptop.
I know how to get the image from the gallery but I don't know how to send it. Many solutions from stackoverflow are old and some classes got deprecated and I can't try their method.
Also, what should I do in the spring controller to receive the image?
You'll have use MultipartFile to upload an image using spring. Please go through following example.
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public String fileUpload(#RequestParam("file") MultipartFile file) {
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
//save file in server - you may need an another scenario
Path path = Paths.get("/" + file.getOriginalFilename());
Files.write(path, bytes);
} catch (IOException e) {
e.printStackTrace();
}
//redirect to an another url end point
return "redirect:/upload-status";
}
Please make sure you can reach to your computer through your mobile device. I believe you may know that Android requires additional privilege to use network connections. So make sure you have permitted your app to access network.
EDIT:
You can use HttpClient to upload file from your mobile app. Please try following code.
HttpClient httpClient = AndroidHttpClient.newInstance("App");
HttpPost httpPost = new HttpPost("http://your-server-url");
httpPost.setEntity(new FileEntity(new File("your-file-path"), "application/octet-stream"));
HttpResponse response = httpClient.execute(httpPost);

How can I login to a website using Java and stay logged in?

So what i am trying to do is log into a a web app that is used by our company, I need to download multiple graphs(45 to be exact) I have a program that will do this exactly how i want it to, However the way my code works it has to log in each time. I'm not sure if this is a problem( Might look suspicious to the admin of the web app) or not but it seems a little inefficient. Ideally I would like to log into the site once and them move to whatever Urls I need to download the images. Any help you guys could offer would be great.
for (int i = 1; i <= 45; i++) {
URL url;
if(i<10) {
url = new URL("http://127.0.0.1:3333/website/image0"+i);
}
else{
url = new URL("http://127.0.0.1:3333/website/image"+i);
}
String loginPassword = "usrName" + ":" + "PassWrd";
String encoded = new sun.misc.BASE64Encoder().encode(loginPassword.getBytes());
URLConnection conn = url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + encoded);
String destName = "C:\\Users\\Name\\Documents\\Report\\p"+i+".png";
InputStream in = new BufferedInputStream(conn.getInputStream());
OutputStream os = new FileOutputStream(destName);
byte[] b = new byte[2048];
int length;
while ((length = in.read(b)) != -1) {
os.write(b, 0, length);
}
in.close();
os.close();
}
This will largely depend on the website you're trying to get the images off of. Most of the time, the website will use Cookies to keep track of the fact that you're logged in.
Java provides a class specifically for this purpose to be used with HttpUrlConnection: The CookieHandler
Using that class to set the default handler to a CookieManager will probably be enough for you to stay logged in. Something like this:
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
// null for the CookieStore means CookieManager will use an in-memory implementation
// And CookiePolicy.ACCEPT_ALL means that this CookieHandler will accept
// all cookies
In case your website uses something else to identify its users, you will have to reverse engineer the process and implement it in your program. Generally though, everything that can be done with a browser can be done with Java as well so it should always be possible.
Edit: After writing this whole thing I just realized that you're website uses HTTP Authentication. In this case Cookies won't actually help, since you have reauthorize every request anyway. HTTP does not have a state by itself, so the server won't remember at all that you were already logged in. If this is the only way that a user can get access to those images then there will be nothing suspicious about it, since a browser has to resend the username and the password with every request as well.
I would recommend saving the encoded String somewhere just to not have it being regenerated every time.
I'll still leave the Cookie thing up, since there might still be some use for it in case the authentication mechanisms are different.

Download file programmatically

I am trying to download an vcalendar using a java application, but I can't download from a specific link.
My code is:
URL uri = new URL("http://codebits.eu/s/calendar.ics");
InputStream in = uri.openStream();
int r = in.read();
while(r != -1) {
System.out.print((char)r);
r = in.read();
}
When I try to download from another link it works (ex: http://www.mysportscal.com/Files_iCal_CSV/iCal_AUTO_2011/f1_2011.ics). Something don't allow me to download and I can't figure out why, when I try with the browser it works.
I'd follow this example. Basically, get the response code for the connection. If it's a redirect (e.g. 301 in this case), retrieve the header location and attempt to access the file using that.
Simplistic Example:
URL uri = new URL("http://codebits.eu/s/calendar.ics");
HttpURLConnection con = (HttpURLConnection)uri.openConnection();
System.out.println(con.getResponseCode());
System.out.println(con.getHeaderField("Location"));
uri = new URL(con.getHeaderField("Location"));
con = (HttpURLConnection)uri.openConnection();
InputStream in = con.getInputStream();
You should check what that link actually provides. For example, it might be a page that has moved, which gives you back an HTTP 301 code. Your browser will automatically know to go and fetch it from the new URL, but your program won't.
You might want to try, for example, wireshark to sniff the actual traffic when you do the browser request.
I think too that there is a redirect. The browser downloads from ssl secured https://codebits.eu/s/calendar.ics. Try using a HttpURLConnection, it should follow redirects automatically:
HttpURLConnection con = (HttpURLConnection)uri.openConnection();
InputStream in = con.getInputStream();

how do I search a word in a webpage

how do I search for existence of a word in a webpage given its url say "www.microsoft.com". Do I need to download this webpage to perform this search ?
You just need to make http request on web page and grab all its content after that you can search necessary words in it, below code might help you to do so.
public static void main(String[] args) {
try {
URL url;
URLConnection urlConnection;
DataOutputStream outStream;
DataInputStream inStream;
// Build request body
String body =
"fName=" + URLEncoder.encode("Atli", "UTF-8") +
"&lName=" + URLEncoder.encode("Þór", "UTF-8");
// Create connection
url = new URL("http://www.example.com");
urlConnection = url.openConnection();
((HttpURLConnection)urlConnection).setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", ""+ body.length());
// Create I/O streams
outStream = new DataOutputStream(urlConnection.getOutputStream());
inStream = new DataInputStream(urlConnection.getInputStream());
// Send request
outStream.writeBytes(body);
outStream.flush();
outStream.close();
// Get Response
// - For debugging purposes only!
String buffer;
while((buffer = inStream.readLine()) != null) {
System.out.println(buffer);
}
// Close I/O streams
inStream.close();
outStream.close();
}
catch(Exception ex) {
System.out.println("Exception cought:\n"+ ex.toString());
}
}
i know how i would do this in theory - use cURL or some application to download it, store the contents into a variable, then parse it for whatever you need
Yes, you need to download page content and search inside it for what you want. And if it happens that you want to search the whole microsoft.com website then you should either write your own web crawler, use an existing crawler or use some search engine API like Google's.
Yes, you'll have to download the page, and, to make sure to get the complete content, you'll want to execute scripts and include dynamic content - just like a browser.
We can't "search" something on a remote resource, that is not controlled by us and no webservers offers a "scan my content" method by default.
Most probably you'll want to load the page with a browser engine (webkit or something else) and perform the search on the internal DOM structure of that engine.
If you want to do the search yourself, then obviously you have to download the page.
If you're planning on this approach, i recommend Lucene (unless you want a simple substring search)
Or you could have a webservice that does it for you. You could request the webservice to grep the url and post back its results.
You could use a search engine's API. I believe Google and Bing (http://msdn.microsoft.com/en-us/library/dd251056.aspx) have ones you can use.

Download dynamic file with GWT

I have a GWT page where user enter data (start date, end date, etc.), then this data goes to the server via RPC call. On the server I want to generate Excel report with POI and let user save that file on their local machine.
This is my test code to stream file back to the client but for some reason I think it does not know how to stream file to the client when I'm using RPC:
public class ReportsServiceImpl extends RemoteServiceServlet implements ReportsService {
public String myMethod(String s) {
File f = new File("/excelTestFile.xls");
String filename = f.getName();
int length = 0;
try {
HttpServletResponse resp = getThreadLocalResponse();
ServletOutputStream op = resp.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
resp.setContentType("application/octet-stream");
resp.setContentLength((int) f.length());
resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
op.write(bbuf, 0, length);
}
in.close();
op.flush();
op.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
return "Server says: " + filename;
}
}
I've read somewhere on internet that you can't do file stream with RPC and I have to use Servlet for that. Is there any example of how to use Servlet and how to call that servlet from ReportsServiceImpl. Do I really need to make a servlet or it is possible to stream it back with my RPC?
You have to make a regular Servlet, you cannot stream binary data from ReportsServiceImpl. Also, there is no way to call the servlet from ReportsServiceImpl - your client code has to directly invoke the servlet.
On the client side, you'd have to create a normal anchor link with the parameters passed via the query string. Something like <a href="http://myserver.com/myservlet?parm1=value1&.."</a>.
On the server side, move your code to a standard Servlet, one that does NOT inherit from RemoteServiceServlet. Read the parameters from the request object, create the excel and send it back to the client. The browser will automatically popup the file download dialog box.
You can do that just using GWT RPC and Data URIs:
In your example, make your myMethod return the file content.
On the client side, format a Data URI with the file content received.
Use Window.open to open a file save dialog passing the formatted DataURI.
Take a look at this reference, to understand the Data URI usage:
Export to csv in jQuery
It's possible to get the binary data you want back through the RPC channel in a number of ways... uuencode, for instance. However, you would still have to get the browser to handle the file as a download.
And, based on your code, it appears that you are trying to trigger the standard browser mechanism for handling the given mime-type by modifying the response in the server so the browser will recognize it as a download... open a save dialog, for instance. To do that, you need to get the browser to make the request for you and you need the servlet there to handle the request. It can be done with rest urls, but ultimately you will need a serviet to do even that.
You need, in effect, to set a browser window URL to the URL that sends back the modified response object.
So this question (about streaming) is not really compatible with the code sample. One or the other (communication protocols or server-modified response object) approach has to be adjusted.
The easiest one to adjust is the communication method.

Categories