---
title: "简单的迅雷链接转换工具"
author: "lua"
date: "2009-09-17T09:11:34Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-09-17T09:11:34Z"
id: 470178
url: "https://lua.iteye.com/blog/470178"
---

http://bbs.luaer.cn/read-Lua-tid-861.html

\--------------------------------------------------

 

require ("wx")

 -- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss &lt;[alexthkloss@web.de](mailto:alexthkloss@web.de) &gt;  
 -- licensed under the terms of the LGPL2

 -- character table string  
 local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

 -- enconding  
 function enc(data)  
     return ((data:gsub('.', function(x)  
         local r,b='',x:byte()  
         for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)&gt;0 and '1' or '0') end  
         return r;  
     end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)  
         if (#x &lt; 6) then return '' end  
         local c=0  
         for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end  
         return b:sub(c+1,c+1)  
     end)..({ '', '==', '=' })\[#data%3+1])  
 end

 -- decoding  
 function dec(data)  
     return (data:gsub('.', function(x)  
         if (x == '=') then return '00' end  
         local r,f='',(b:find(x)-1)  
         for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)&gt;0 and '1' or '0') end  
         return r;  
    end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)         
         if (#x ~= 8) then return '' end  
         local c=0  
         for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end  
         return string.char(c)  
     end))  
 end

 -- command line if not called as library  
 if (arg ~= nil) then  
     local func = 'enc'  
     for n,v in ipairs(arg) do  
         if (n &gt; 0) then  
             if (v == "-h") then print "base64.lua \[-e] \[-d] text/data" break  
             elseif (v == "-e") then func = 'enc'  
             elseif (v == "-d") then func = 'dec'  
             else print(\_G\[func](v)) end  
         end  
     end  
 else  
     module('base64',package.seeall)  
 end

function main()

ID\_OK\_BUTTON    = wx:wxNewId()  
ID\_CLOSE\_BUTTON = wx:wxNewId()  
ID\_ADDBUTTON    = wx:wxNewId()  
ID\_CLEAR\_BUTTON    = wx:wxNewId()

dialog = wx.wxDialog(wx.NULL, wx.wxID\_ANY, "迅雷链接转换工具",  
                     wx.wxDefaultPosition, wx.wxDefaultSize)       

local mainSizer = wx.wxBoxSizer(wx.wxVERTICAL)

local nextSizer = wx.wxBoxSizer(wx.wxVERTICAL)

local buttonSizer = wx.wxBoxSizer( wx.wxHORIZONTAL )

multiText = wx.wxTextCtrl(dialog, -1, "", wx.wxDefaultPosition, wx.wxSize(350,80), wx.wxTE\_MULTILINE)  
multiText2 = wx.wxTextCtrl(dialog, -1, "", wx.wxDefaultPosition, wx.wxSize(350,80), wx.wxTE\_MULTILINE)

nextSizer:Add(wx.wxStaticText( dialog, wx.wxID\_ANY, "请输入迅雷链接："))  
nextSizer:Add(multiText)

subButton = wx.wxButton( dialog, ID\_OK\_BUTTON, "转换并复制")  
closeButton = wx.wxButton( dialog, ID\_CLOSE\_BUTTON, "关闭")  
clearButton = wx.wxButton( dialog, ID\_CLEAR\_BUTTON, "清除")  
buttonSizer:Add( clearButton, 0, wx.wxALIGN\_CENTER + wx.wxRIGHT, 5)

buttonSizer:Add( subButton, 0, wx.wxALIGN\_CENTER + wx.wxRIGHT, 5)  
buttonSizer:Add( closeButton, 0, wx.wxALIGN\_CENTER + wx.wxEXPAND)  
nextSizer:Add(wx.wxStaticLine(dialog, wx.wxID\_ANY), 0, wx.wxEXPAND + wx.wxBOTTOM + wx.wxTOP, 14)  
nextSizer:Add(wx.wxStaticText( dialog, wx.wxID\_ANY, "输出"))  
nextSizer:Add(multiText2)  
nextSizer:Add(wx.wxStaticLine(dialog, wx.wxID\_ANY), 0, wx.wxEXPAND + wx.wxBOTTOM + wx.wxTOP, 14)  
nextSizer:Add(buttonSizer, 0, wx.wxALIGN\_RIGHT)

mainSizer:Add(nextSizer, 0, wx.wxALL, 14)

dialog:SetSizerAndFit(mainSizer)

dialog:Centre()  
dialog:Show(true)

subButton:Connect(wx.wxEVT\_COMMAND\_BUTTON\_CLICKED,  
    function(event)  
        \_, \_, str = string.find(multiText:GetValue(), "%s\*thunder://(%S+)%s\*")  
        if str ~= nil then  
            str = dec(str)  
            multiText2:SetValue(string.sub(str, 3, string.len(str) - 3))  
            text\_data = wx.wxTextDataObject(multiText2:GetValue())  
            local clipBoard = wx.wxClipboard.Get()  
            if text\_data and clipBoard and clipBoard:Open() then  
                clipBoard.Get():SetData(text\_data)  
                clipBoard.Get():Flush()  
                clipBoard.Get():Close()  
            end

        else  
            wx.wxMessageBox("请输入正确的迅雷链接！","错误", wx.wxOK + wx.wxICON\_ERROR)  
        end  
    end  
)

clearButton:Connect(wx.wxEVT\_COMMAND\_BUTTON\_CLICKED,  
    function(event)  
        multiText:SetValue("")  
        multiText2:SetValue("")  
    end  
)

dialog:Connect(ID\_CLOSE\_BUTTON, wx.wxEVT\_COMMAND\_BUTTON\_CLICKED,  
    function(event) dialog:Destroy() end)

dialog:Connect(wx.wxEVT\_CLOSE\_WINDOW,  
    function (event)  
        dialog:Destroy()  
        event:Skip()  
    end)

     
end

main()

wx.wxGetApp():MainLoop()

原理：[迅雷专用链接原理及转换](http://www.baidu.com/s?wd=%D1%B8%C0%D7%D7%A8%D3%C3%C1%B4%BD%D3%D4%AD%C0%ED%BC%B0%D7%AA%BB%BB)

说明：  
Base64编码转换是从[这里](http://lua-users.org/wiki/BaseSixtyFour) 找到的

剪贴板的操作参考了：[saving the clipboard on exit](http://osdir.com/ml/lib.wxwidgets.wxlua.user/2008-05/msg00008.html)

clipBoard.Get():Flush()是必须的！如果没有，那么关闭这个小工具后剪贴板会被清空 ！

写的不好！希望大家多提意见！