20
janvier
2013
bibliothèque de liens vidéos : mise à jour (C#/Vala)
janvier
2013
Un article de Rachel
Pas de commentaires
voici une nouvelle version de ma bibliothèque de liens vidéo, mise à jour suite à la modification du code source de youtube. Le code est moche certes, mais il fonctionne. Manque juste la gestion de la qualité.
la version c# nécessite json.Net, disponible ici
Le code C# :
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Media
{
public class Video
{
protected List<string> urls;
protected List<string> qualities;
protected Dictionary<string,string> table;
protected WebClient wc;
protected JObject o;
public Video(){
urls = new List<string>();
table = new Dictionary<string,string>();
qualities = new List<string>();
wc = new WebClient();
}
public Video (string url) : this()
{
o = JObject.Parse (wc.DownloadString (url));
}
public Video (string url, string param): this()
{
wc.DownloadFile (url, "temp");
StreamReader sr = new StreamReader ("temp");
string str;
while ((str = sr.ReadLine ())!=null) {
if(str.Contains (param)){
string[] tab = str.Split(new string[]{" = "},StringSplitOptions.None);
tab[1] = tab[1].Trim(';');
o = JObject.Parse (tab[1]);
break;
}
}
}
public List<string> Urls {
get{return urls;}
}
public Dictionary<string,string> Table {
get{return table;}
}
}
public class Dailymotion : Video
{
public Dailymotion (string url) : base(url,"var flashvars")
{
o = JObject.Parse (((string)o["sequence"]).HtmlDecode());
o = (JObject)o["sequence"][0]["layerList"][0]["sequenceList"][2]["layerList"][0]["param"];
this.table["ld"] = (string)o["ldURL"];
this.table["sd"] = (string)o["sdURL"];
this.table["hq"] = (string)o["hqURL"];
}
}
public class Youtube : Video
{
public Youtube(string id) : base()
{
string data = wc.DownloadString ("http://www.youtube.com/get_video_info?&video_id="+id);
data = data.Replace("%253A",":")
.Replace("%25253A","%3A")
.Replace("%252F","/")
.Replace("%253F","?")
.Replace("%253D","=")
.Replace("%3D","=")
.Replace("%2522","\"")
.Replace("%2526","&")
.Replace("%26","&")
.Replace("%2C","&")
.Replace("%25252C","%2C")
.Replace("&sig=","&signature=");
data = data.Split (new string[]{"url_encoded_fmt_stream_map="},StringSplitOptions.RemoveEmptyEntries)[1];
int pos = data.IndexOf ("=");
string prm = data.Substring (0,pos+1);
string[] table = data.Split (new string[]{prm},StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i<table.Length; i++)
table[i] = prm+table[i];
List<string> list = new List<string>(table);
list.Remove(list[0]);
table = list.ToArray ();
if(table.Length>0){
for(int z=0; z<table.Length; z++){
string itag = "";
List<string> list2 = new List<string>(table[z].Split ('&'));
while(true){
list2.Add (list2[0]);
list2.Remove (list2[0]);
if(list2[0].Contains ("url="))break;
}
string s = "";
foreach(string str in list2){
if(!str.Contains ("itag="))s += str+"&";
else itag = str;
}
s = s.Substring(0,s.Length-1).Replace ("url=","").Replace ("&&","&")+itag;
urls.Add (s);
}
}
}
}
public static class Utility
{
public static string HtmlDecode(this string raw_string){
return raw_string.Replace("%5C","\\")
.Replace("%3C","")
.Replace("%5B","[")
.Replace("%7B","{")
.Replace("%5D","]")
.Replace("%7D","}")
.Replace("%3A",":")
.Replace("%2F","/")
.Replace("%3F","?")
.Replace("%3D","=")
.Replace("%26","&")
.Replace("%22","\"")
.Replace("%2C",",")
.Replace("%3B",";")
.Replace("%25","%");
}
}
}
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Media
{
public class Video
{
protected List<string> urls;
protected List<string> qualities;
protected Dictionary<string,string> table;
protected WebClient wc;
protected JObject o;
public Video(){
urls = new List<string>();
table = new Dictionary<string,string>();
qualities = new List<string>();
wc = new WebClient();
}
public Video (string url) : this()
{
o = JObject.Parse (wc.DownloadString (url));
}
public Video (string url, string param): this()
{
wc.DownloadFile (url, "temp");
StreamReader sr = new StreamReader ("temp");
string str;
while ((str = sr.ReadLine ())!=null) {
if(str.Contains (param)){
string[] tab = str.Split(new string[]{" = "},StringSplitOptions.None);
tab[1] = tab[1].Trim(';');
o = JObject.Parse (tab[1]);
break;
}
}
}
public List<string> Urls {
get{return urls;}
}
public Dictionary<string,string> Table {
get{return table;}
}
}
public class Dailymotion : Video
{
public Dailymotion (string url) : base(url,"var flashvars")
{
o = JObject.Parse (((string)o["sequence"]).HtmlDecode());
o = (JObject)o["sequence"][0]["layerList"][0]["sequenceList"][2]["layerList"][0]["param"];
this.table["ld"] = (string)o["ldURL"];
this.table["sd"] = (string)o["sdURL"];
this.table["hq"] = (string)o["hqURL"];
}
}
public class Youtube : Video
{
public Youtube(string id) : base()
{
string data = wc.DownloadString ("http://www.youtube.com/get_video_info?&video_id="+id);
data = data.Replace("%253A",":")
.Replace("%25253A","%3A")
.Replace("%252F","/")
.Replace("%253F","?")
.Replace("%253D","=")
.Replace("%3D","=")
.Replace("%2522","\"")
.Replace("%2526","&")
.Replace("%26","&")
.Replace("%2C","&")
.Replace("%25252C","%2C")
.Replace("&sig=","&signature=");
data = data.Split (new string[]{"url_encoded_fmt_stream_map="},StringSplitOptions.RemoveEmptyEntries)[1];
int pos = data.IndexOf ("=");
string prm = data.Substring (0,pos+1);
string[] table = data.Split (new string[]{prm},StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i<table.Length; i++)
table[i] = prm+table[i];
List<string> list = new List<string>(table);
list.Remove(list[0]);
table = list.ToArray ();
if(table.Length>0){
for(int z=0; z<table.Length; z++){
string itag = "";
List<string> list2 = new List<string>(table[z].Split ('&'));
while(true){
list2.Add (list2[0]);
list2.Remove (list2[0]);
if(list2[0].Contains ("url="))break;
}
string s = "";
foreach(string str in list2){
if(!str.Contains ("itag="))s += str+"&";
else itag = str;
}
s = s.Substring(0,s.Length-1).Replace ("url=","").Replace ("&&","&")+itag;
urls.Add (s);
}
}
}
}
public static class Utility
{
public static string HtmlDecode(this string raw_string){
return raw_string.Replace("%5C","\\")
.Replace("%3C","")
.Replace("%5B","[")
.Replace("%7B","{")
.Replace("%5D","]")
.Replace("%7D","}")
.Replace("%3A",":")
.Replace("%2F","/")
.Replace("%3F","?")
.Replace("%3D","=")
.Replace("%26","&")
.Replace("%22","\"")
.Replace("%2C",",")
.Replace("%3B",";")
.Replace("%25","%");
}
}
}
Le code Vala :
using Gee;
using Soup;
using Json;
namespace Media
{
public static ArrayList<string> array_to_list(string[] array){
ArrayList<string> list = new ArrayList<string>();
foreach(string s in array)
list.add(s);
return list;
}
public static string join_list(ArrayList<string> list, string join)
{
bool itag = false;
string s = "";
foreach(string str in list){
if(!str.contains("itag="))
s += str+join;
if(str.contains("itag=")&&itag==false){
s += str+join;itag=true;
}
}
return s;
}
public class Video
{
protected ArrayList<string> _urls;
protected string data;
public ArrayList<string> urls{get{return _urls;}}
public Video()
{
_urls = new ArrayList<string>();
}
public Video.from_url(string url)
{
this();
var session = new SessionAsync();
var message = new Message("GET",url);
session.send_message(message);
data = (string)message.response_body.data;
}
}
public class Youtube : Video
{
public Youtube(string id)
{
base.from_url("http://www.youtube.com/get_video_info?&video_id="+id);
data = data.split("url_encoded_fmt_stream_map=",0)[1].split("&")[0];
data = data.replace("%253A",":")
.replace("%25253A","%3A")
.replace("%252F","/")
.replace("%253F","?")
.replace("%253D","=")
.replace("%3D","=")
.replace("%2522","\"")
.replace("%2526","&")
.replace("%26","&")
.replace("%2C","&")
.replace("%25252C","%2C")
.replace("&sig=","&signature=");
int pos = data.index_of("=");
string prm = data.substring(0,pos+1);
string[] table = data.split(prm,100);
for(int i=0; i<table.length; i++)
table[i] = prm+table[i];
ArrayList<string> list = array_to_list(table);
list.remove(list.get(0));
table = list.to_array();
if(table.length>0){
for(int z=0; z<table.length; z++){
ArrayList<string> list2 = array_to_list(table[z].split("&",100));
while(true){
list2.add(list2.get(0));
list2.remove(list2.get(0));
if(list2.get(0).contains("url="))break;
}
string s = join_list(list2,"&").replace("url=","");
s = s.substring(0,s.length-1);
_urls.add(s);
}
}
}
}
public class Dailymotion : Video
{
public Dailymotion(string url)
{
base.from_url(url);
FileUtils.set_data("temp",data.data);
Parser parser = new Parser();
FileStream fs = FileStream.open("temp","r");
string str;
while((str = fs.read_line())!=null){
if(str.contains("var flashvars")){
string[] tab = str.split(" = ",10);
tab[1] = tab[1].replace(";","");
parser.load_from_data(tab[1]);
break;
}
}
var o = parser.get_root().get_object();
str = Uri.unescape_string(o.get_string_member("sequence"));
parser.load_from_data(str);
o = parser.get_root().get_object();
o = o.get_array_member("sequence").get_object_element(0)
.get_array_member("layerList").get_object_element(0)
.get_array_member("sequenceList").get_object_element(2)
.get_array_member("layerList").get_object_element(0).get_object_member("param");
_urls.add(o.get_string_member("ldURL"));
_urls.add(o.get_string_member("sdURL"));
_urls.add(o.get_string_member("hqURL"));
}
}
public class IGN : Video
{
public IGN(string url){
base.from_url(url);
FileUtils.set_data("temp",data.data);
FileStream fs = FileStream.open("temp","r");
string str = "";
string[] t = new string[]{""};
while((str = fs.read_line())!=null){
if(str.contains("iPadVideoSource_0")){
t = str.split("\"");
_urls.add(t[3]);
}
if(str.contains("videoSource_0")){
t = str.split("\"");
_urls.add(t[3]);
}
if(str.contains("mVideoSource_0")){
t = str.split("\"");
_urls.add(t[3]);
}
}
}
}
}
using Soup;
using Json;
namespace Media
{
public static ArrayList<string> array_to_list(string[] array){
ArrayList<string> list = new ArrayList<string>();
foreach(string s in array)
list.add(s);
return list;
}
public static string join_list(ArrayList<string> list, string join)
{
bool itag = false;
string s = "";
foreach(string str in list){
if(!str.contains("itag="))
s += str+join;
if(str.contains("itag=")&&itag==false){
s += str+join;itag=true;
}
}
return s;
}
public class Video
{
protected ArrayList<string> _urls;
protected string data;
public ArrayList<string> urls{get{return _urls;}}
public Video()
{
_urls = new ArrayList<string>();
}
public Video.from_url(string url)
{
this();
var session = new SessionAsync();
var message = new Message("GET",url);
session.send_message(message);
data = (string)message.response_body.data;
}
}
public class Youtube : Video
{
public Youtube(string id)
{
base.from_url("http://www.youtube.com/get_video_info?&video_id="+id);
data = data.split("url_encoded_fmt_stream_map=",0)[1].split("&")[0];
data = data.replace("%253A",":")
.replace("%25253A","%3A")
.replace("%252F","/")
.replace("%253F","?")
.replace("%253D","=")
.replace("%3D","=")
.replace("%2522","\"")
.replace("%2526","&")
.replace("%26","&")
.replace("%2C","&")
.replace("%25252C","%2C")
.replace("&sig=","&signature=");
int pos = data.index_of("=");
string prm = data.substring(0,pos+1);
string[] table = data.split(prm,100);
for(int i=0; i<table.length; i++)
table[i] = prm+table[i];
ArrayList<string> list = array_to_list(table);
list.remove(list.get(0));
table = list.to_array();
if(table.length>0){
for(int z=0; z<table.length; z++){
ArrayList<string> list2 = array_to_list(table[z].split("&",100));
while(true){
list2.add(list2.get(0));
list2.remove(list2.get(0));
if(list2.get(0).contains("url="))break;
}
string s = join_list(list2,"&").replace("url=","");
s = s.substring(0,s.length-1);
_urls.add(s);
}
}
}
}
public class Dailymotion : Video
{
public Dailymotion(string url)
{
base.from_url(url);
FileUtils.set_data("temp",data.data);
Parser parser = new Parser();
FileStream fs = FileStream.open("temp","r");
string str;
while((str = fs.read_line())!=null){
if(str.contains("var flashvars")){
string[] tab = str.split(" = ",10);
tab[1] = tab[1].replace(";","");
parser.load_from_data(tab[1]);
break;
}
}
var o = parser.get_root().get_object();
str = Uri.unescape_string(o.get_string_member("sequence"));
parser.load_from_data(str);
o = parser.get_root().get_object();
o = o.get_array_member("sequence").get_object_element(0)
.get_array_member("layerList").get_object_element(0)
.get_array_member("sequenceList").get_object_element(2)
.get_array_member("layerList").get_object_element(0).get_object_member("param");
_urls.add(o.get_string_member("ldURL"));
_urls.add(o.get_string_member("sdURL"));
_urls.add(o.get_string_member("hqURL"));
}
}
public class IGN : Video
{
public IGN(string url){
base.from_url(url);
FileUtils.set_data("temp",data.data);
FileStream fs = FileStream.open("temp","r");
string str = "";
string[] t = new string[]{""};
while((str = fs.read_line())!=null){
if(str.contains("iPadVideoSource_0")){
t = str.split("\"");
_urls.add(t[3]);
}
if(str.contains("videoSource_0")){
t = str.split("\"");
_urls.add(t[3]);
}
if(str.contains("mVideoSource_0")){
t = str.split("\"");
_urls.add(t[3]);
}
}
}
}
}