16
octobre
2012
intégrer une vidéo youtube ou dailymotion dans son application
octobre
2012
Un article de Rachel
Pas de commentaires
Si vous désirez lire des vidéos de ces deux plate-formes ou tout autre chose (:°), vous pouvez utiliser cette bibliothèque permettant d’obtenir les liens de ces vidéos. Elle intègre en plus des fonctions de recherche sur ces deux sites :
- en C# :
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace Media
{
public class Video
{
WebClient wc;
string url;
string data;
List<string> links;
List<string> qualities;
List<string> types;
public Video(){wc = new WebClient();}
public Video(string url){
if(File.Exists("temp.html"))
File.Delete("temp.html");
wc = new WebClient();wc.DownloadFile(new Uri(url),"temp.html");}
public string Url {
get{
return url;
}
set{
if(File.Exists("temp.html"))
File.Delete("temp.html");
wc.DownloadFile(new Uri(value),"temp.html");
url = value;
}
}
public string Data {
get{
StreamReader sr = new StreamReader("temp.html");
return sr.ReadToEnd ();
}
}
public List<string> Links {
get{return links;}
set{links = value;}
}
public List<string> Qualities {
set{qualities = value;}
get{return qualities;}
}
public List<string> Types {
set{types = value;}
get{return types;}
}
}
public class Youtube : Video
{
string contents;
public Youtube (string url) : base(url)
{
contents = base.Data;
this.Qualities = new List<string>();
this.Links = new List<string> ();
this.Types = new List<string> ();
string[] data = contents.Split (new char[]{'"'});
for (int i=0; i<data.Length; i++) {
if (data [i] == "url_encoded_fmt_stream_map") {
contents = data [i + 2];
break;
}
}
contents = contents.Replace ("%3A", ":");
contents = contents.Replace ("%2F", "/");
contents = contents.Replace ("%26", "&");
contents = contents.Replace ("%3F", "?");
contents = contents.Replace ("%3D", "=");
contents = contents.Replace ("\\u0026", "&");
contents = contents.Replace ("%252C", "%2C");
contents = contents.Replace ("%253A", "%3A");
contents = contents.Replace ("sig", "signature");
data = contents.Split (new string[]{"&url="},StringSplitOptions.RemoveEmptyEntries);
for (int i=0; i+1<data.Length; i++) {
this.Links.Add(data[i+1]);
string[] tab = data[i+1].Split(new char[]{'&'});
foreach(string str in tab){
string[] tab2 = str.Split(new char[]{'='});
if(tab2[0]=="quality"){
this.Qualities.Add(tab2[1].Split(new char[]{','})[0]);
}
if(tab2[0]=="type"){
this.Qualities.Add(tab2[1].Split(new string[]{"%3B"},StringSplitOptions.RemoveEmptyEntries)[0]);
}
}
}
}
}
public class Dailymotion : Video
{
string contents;
public Dailymotion (string url) : base(url)
{
contents = base.Data;
this.Qualities = new List<string>();
this.Links = new List<string>();
this.Types = new List<string>();
contents = contents.Replace("%3A",":");
contents = contents.Replace("%22","\"");
string[] data = contents.Split(new char[]{'"'});
for(int i=0;i+2<data.Length;i++)
if(data[i]=="ldURL"||data[i]=="hqURL"||data[i]=="sdURL"||data[i]=="video_url"){
string str = data[i+2];
if(data[i]=="ldURL")this.Qualities.Add("ld");
if(data[i]=="sdURL")this.Qualities.Add("sd");
if(data[i]=="hqURL")this.Qualities.Add("hq");
if(data[i]=="video_url")this.Qualities.Add("current");
str = str.Replace("%253A",":");
str = str.Replace("%252F","/");
str = str.Replace("%253F","?");
str = str.Replace("%3F","?");
str = str.Replace("%253D","=");
str = str.Replace("%3D","=");
str = str.Replace("%5C%2F","/");
this.Links.Add(str);
}
}
}
public class Search
{
WebClient wc;
List<string> results;
List<string> links;
string temp;
public Search(string query)
{
wc = new WebClient();
results = new List<string>();
links = new List<string>();
YoutubeSearch(query);
DailymotionSearch(query);
}
public List<string> Links {
get{return links;}
}
public List<string> Results {
get{return results;}
}
void DailymotionSearch (string query)
{
if(File.Exists("temp.html"))
File.Delete("temp.html");
wc.DownloadFile (new Uri ("http://www.dailymotion.com/relevance/search/" + query.Replace (" ", "+")), "temp.html");
StreamReader sr = new StreamReader ("temp.html");
temp = sr.ReadToEnd ();
string[] tab = temp.Split (new char[]{'"'});
for (int i = 4; i+3 < tab.Length; i++) {
if(tab[i]=="dmco_simplelink video_title id_ dmco_link with_context not_ajax"){
results.Add(tab[i-4]);
links.Add("http://www.dailymotion.com"+tab[i-2]);
}
}
}
void YoutubeSearch (string query)
{
if(File.Exists("temp.html"))
File.Delete("temp.html");
wc.DownloadFile (new Uri ("http://www.youtube.com/results?search_query=" + query), "temp.html");
StreamReader sr = new StreamReader ("temp.html");
temp = sr.ReadToEnd ();
string[] tab = temp.Split (new char[]{'"'});
for (int i = 0; i+3 < tab.Length; i++) {
if (tab [i] == "data-sessionlink=") {
results.Add (tab [i - 1]);
links.Add ("http://www.youtube.com" + tab [i + 3]);
}
}
}
}
} - en Vala :
namespace GLib
{
public class WebClient
{
string s;
int i;
public WebClient(){}
public void download_file(string uri, string filename)
{
Process.spawn_command_line_sync("wget "+uri+" -O "+filename,null,out s,out i);
}
}
}
namespace Media
{
public class Search
{
WebClient wc;
List<string> results;
List<string> links;
string temp;
size_t size;
public Search(string query){
wc = new WebClient();
results = new List<string>();
links = new List<string>();
youtube_search(query);
dailymotion_search(query);
}
public List<string> Results {get{return results;}}
public List<string> Links {get{return links;}}
void dailymotion_search(string query){
wc.download_file("http://www.dailymotion.com/relevance/search/"+query.replace(" ","+"),"temp.html");
FileUtils.get_contents("temp.html",out temp,out size);
string[] tab = temp.split("\"",0);
for(int i = 4; i+3 < tab.length; i++){
if(tab[i]=="dmco_simplelink video_title id_ dmco_link with_context not_ajax"){
results.append(tab[i-4]);
links.append("http://www.dailymotion.com"+tab[i-2]);
}
}
foreach(string str in links)
stdout.printf(str+"\n");
}
void youtube_search(string query){
wc.download_file("http://www.youtube.com/results?search_query="+query,"temp.html");
FileUtils.get_contents("temp.html",out temp,out size);
string[] tab = temp.split("\"",0);
for(int i = 0; i+3 < tab.length; i++){
if(tab[i]=="data-sessionlink="){
results.append(tab[i-1]);
links.append("http://www.youtube.com"+tab[i+3]);
}
}
}
}
public class Video
{
WebClient wc;
public Video(){wc = new WebClient();}
public Video.from_url(string url){wc = new WebClient();wc.download_file(url,"temp.html");}
public void set_url(string url){wc.download_file(url,"temp.html");}
public string get_data(){
string str;
size_t size;
FileUtils.get_contents("temp.html",out str,out size);
return str;
}
}
public class Dailymotion : Video
{
string contents;
List<string> _urls;
List<string> _qualities;
string[] qualities;
string[] urls;
public string[] Urls {get{return urls;}}
public string[] Qualities {get{return qualities;}}
public Dailymotion(string url){base.from_url(url);data();}
void data(){
/*
for(int i=0;i+2<data.length;i++)
if(data[i]=="URL")stdout.printf(data[i+2]+"\n");
*/
contents = base.get_data();
contents = contents.replace("%3A",":");
contents = contents.replace("%22","\"");
string[] data = contents.split("\"",0);
_urls = new List<string>();
_qualities = new List<string>();
for(int i=0;i+2<data.length;i++)
if(data[i]=="ldURL"||data[i]=="hqURL"||data[i]=="sdURL"||data[i]=="video_url"){
string str = data[i+2];
if(data[i]=="ldURL")_qualities.append("ld");
if(data[i]=="sdURL")_qualities.append("sd");
if(data[i]=="hqURL")_qualities.append("hq");
if(data[i]=="video_url")_qualities.append("current");
str = str.replace("%253A",":");
str = str.replace("%252F","/");
str = str.replace("%253F","?");
str = str.replace("%3F","?");
str = str.replace("%253D","=");
str = str.replace("%3D","=");
str = str.replace("%5C%2F","/");
_urls.append(str);
}
urls = new string[_urls.length()];
qualities = new string[_qualities.length()];
for(int i=0; i<urls.length; i++)
{
urls[i] = _urls.nth_data(i);
qualities[i] = _qualities.nth_data(i);
}
}
}
public class Youtube : Video
{
string contents;
size_t size;
string[] urls;
List<string> qualities;
List<string> types;
public string[] Urls {get{return urls;}}
public List<string> Qualities {get{return qualities;}}
public List<string> Types {get{return types;}}
public Youtube(string url){base.from_url(url);data();}
void data(){
contents = base.get_data();
string[] data = contents.split("\"",0);
for(int i=0; i<data.length; i++){if(data[i]=="url_encoded_fmt_stream_map"){contents=data[i+2];break;}}
contents = contents.replace("%3A",":");
contents = contents.replace("%2F","/");
contents = contents.replace("%26","&");
contents = contents.replace("%3F","?");
contents = contents.replace("%3D","=");
contents = contents.replace("\\u0026","&");
contents = contents.replace("%252C","%2C");
contents = contents.replace("%253A","%3A");
contents = contents.replace("sig","signature");
data = contents.split("&url=",0);
urls = new string[data.length-1];
qualities = new List<string>();
types = new List<string>();
for(int i=0; i<urls.length;i++)
{
urls[i] = data[i+1];
string[] tab = urls[i].split("&",0);
foreach(string str in tab)
{
string[] tab2 = str.split("=",0);
if(tab2[0]=="quality")
{
tab2[1] = tab2[1].split(",",0)[0];
qualities.append(tab2[1]);
}
if(tab2[0]=="type")
{
tab2[1] = tab2[1].split("%3B",0)[0];
types.append(tab2[1]);
}
}
}
}
}
}