通过Fiddler2 Customize Rule下载mp3

    平时上网时总是习惯开着Google Music随便选些歌听,但是现在的办公地点不能访问互联网,以前只好把想听的mp3都逐一下载到电脑中,每次更新歌曲库都很麻烦,想分析Google Music中提供的下载mp3流程单独写段自动下载的程序又没有什么好的思路,就想到可以通过Fiddler2 Customize Rule拦截Http请求,通过设置过滤条件当发现在线听歌过程中请求到某个mp3文件时就自动下载到指定目录中。

    实现方法如下:

    在OnBeforeRequest方法中添加如下代码(由于没搞懂Fiddler2中添加程序集引用的逻辑,一些CLR中的类型调用都是使用反射来完成的):

 

   1: if(oSession.fullUrl.Contains("top100.cn") && oSession.uriContains(".mp3"))
   2:     {
   3:         var temp: String = oSession.url;
   4:                     
   5:         temp = temp.Substring(temp.LastIndexOf("/")+1);
   6:         temp = temp.Substring(0,temp.IndexOf("."));
   7:         
   8:         var type: Type = Type.GetType("System.Web.HttpUtility, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
   9:         
  10:         var types: Type[] = new Type[1];
  11:         types[0] = Type.GetType("System.String");
  12:         
  13:         var method: System.Reflection.MethodInfo = type.GetMethod("UrlDecode", types);
  14:         
  15:         var objects: Object[] = new Object[1];
  16:         objects[0] = temp;
  17:         
  18:         var result : Object = method.Invoke(temp, objects);
  19:         
  20:         
  21:         temp = "C:\\Users\\Pag\\Downloads\\"+result.ToString()+".mp3";
  22:         
  23:         if(!System.IO.File.Exists(temp))
  24:         {
  25:             type = Type.GetType("System.Net.WebClient, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
  26:         
  27:             var client: Object = System.Activator.CreateInstance(type);
  28:         
  29:             types = new Type[2];
  30:             types[0] = Type.GetType("System.String");
  31:             types[1] = Type.GetType("System.String");
  32:         
  33:             method = type.GetMethod("DownloadFile",types);
  34:         
  35:             objects = new Object[2];
  36:             objects[0] = oSession.fullUrl;
  37:             objects[1] = temp;
  38:         
  39:             method.Invoke(client,objects);
  40:         
  41:             method = type.GetMethod("Dispose");
  42:         
  43:             objects = new Object[0]
  44:             method.Invoke(client,objects);
  45:         }
  46:     }

 

编辑Customize Rule需要安装Fiddler2的插件:FiddlerScript Editor,这个插件可以通过安装Syntax-Highlighting Addons获得。

参考链接:FiddlerScript CookBook

posted @ 2009-07-26 11:58  Pengzhi Sun  阅读(540)  评论(0编辑  收藏  举报