sifokl
sifokl

Reputation: 3

JAVA : how to extract youtube URL from facebook.com/l.php?u=

I m developing an Android app , and , in this step , I want to feed a database with a "Facebook user youtube video request", when a user click in a Youtube video from facebook , an intent catch the URL , but i found that URL is like that : https://m.facebook.com/l.php?u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DD148dv1G3E4&h=KAQGHuQwG&s=1&enc=AZNGSlqUPqFIDzzjZHddvdQlAkwGkAHJy7YxLMEX7Bfi7-1PE97FOtxHPq73XJ_mKf_Dh50D_YHBxrIiIJ1HnWCbesQO4f19EVtaV-ovXqHnXw

For this original one : https://www.youtube.com/watch?v=D148dv1G3E4

How can I transform with JAVA (regex or other tool) the facebook.com/l.php?u= URL to the www.youtube.com/watch?v= one ? Thanks

Upvotes: 0

Views: 1264

Answers (1)

Manuel Allenspach
Manuel Allenspach

Reputation: 12745

In this simple example, you could to something like this:

String oldUrl = "https://m.facebook.com/l.php?u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DD148dv1G3E4&h=KAQGHuQwG&s=1&enc=AZNGSlqUPqFIDzzjZHddvdQlAkwGkAHJy7YxLMEX7Bfi7-1PE97FOtxHPq73XJ_mKf_Dh50D_YHBxrIiIJ1HnWCbesQO4f19EVtaV-ovXqHnXw";
String newUrl;
List args= URLEncodedUtils.parse(oldUrl , Charset.defaultCharset());
for (NameValuePair arg:args) {
    if (arg.getName().equals("u")) {
        newUrl = URLDecoder.decode(arg.getValue(), "UTF-8");
    }
}

First, get the encoded youtube url, then decode it and store it in newUrl.

Upvotes: 1

Related Questions