我想使用OpenSubtitles API为电影下载字幕,但无法在任何地方找到完整的工作示例。我发现的只有这个:
http://www.yanniel.info/2012/01/open-subtitles-api-in-delphi.html
我做了所说的所有事情,但它不完整。我登录并找到一个副标题,但不知道如何最终下载文件?
编辑:这是我到目前为止所做的;
unit OpensubtitlesAPI;
interface
uses
IdHTTP, Classes, SysUtils;
function LogIn(aUsername, aPassword,
aLanguage, aUserAgent: string): string;
function LogOut(aToken: string): string;
function SearchSubtitles(aToken, aSublanguageID,
aMovieHash: string;
aMovieByteSize: Cardinal): string; overload;
function SearchSubtitles(aToken, aSublanguageID: string;
aImdbID: Cardinal): string; overload;
function SearchSubtitles(aToken, aSublanguageID,
aQuery: string): string; overload;
implementation
function XML_RPC(aRPCRequest: string): string;
const
cURL= 'http://api.opensubtitles.org/xml-rpc';
var
lHTTP: TIdHTTP;
Source,
ResponseContent: TStringStream;
begin
lHTTP := TIdHTTP.Create(nil);
lHTTP.Request.ContentType := 'text/xml';
lHTTP.Request.Accept := '*/*';
lHTTP.Request.Connection := 'Keep-Alive';
lHTTP.Request.Method := 'POST';
lHTTP.Request.UserAgent := 'OS Test User Agent';
Source := TStringStream.Create(aRPCRequest);
ResponseContent:= TStringStream.Create;
try
try
lHTTP.Post(cURL, Source, ResponseContent);
Result:= ResponseContent.DataString;
except
Result:= '';
end;
finally
lHTTP.Free;
Source.Free;
ResponseContent.Free;
end;
end;
function LogIn(aUsername, aPassword, aLanguage, aUserAgent: string): string;
const
LOG_IN = '<?xml version="1.0"?>' +
'<methodCall>' +
' <methodName>LogIn</methodName>' +
' <params>' +
' <param>' +
' <value><string>%0:s</string></value>' +
' </param>' +
' <param>' +
' <value><string>%1:s</string></value>' +
' </param>' +
' <param>' +
' <value><string>%2:s</string></value>' +
' </param>' +
' <param>' +
' <value><string>%3:s</string></value>' +
' </param>' +
' </params>' +
'</methodCall>';
begin
//TODO: XML Encoding
Result:= XML_RPC(Format(LOG_IN, [aUsername, aPassword, aLanguage, aUserAgent]));
end;
function LogOut(aToken: string): string;
const
LOG_OUT = '<?xml version="1.0"?>' +
'<methodCall>' +
' <methodName>LogOut</methodName>' +
' <params>' +
' <param>' +
' <value><string>%0:s</string></value>' +
' </param>' +
' </params>' +
'</methodCall>';
begin
//TODO: XML Encoding
Result:= XML_RPC(Format(LOG_OUT, [aToken]));
end;
function SearchSubtitles(aToken, aSublanguageID, aMovieHash: string; aMovieByteSize: Cardinal): string;
const
SEARCH_SUBTITLES = '<?xml version="1.0"?>' +
'<methodCall>' +
' <methodName>SearchSubtitles</methodName>' +
' <params>' +
' <param>' +
' <value><string>%0:s</string></value>' +
' </param>' +
' <param>' +
' <value>' +
' <array>' +
' <data>' +
' <value>' +
' <struct>' +
' <member>' +
' <name>sublanguageid</name>' +
' <value><string>%1:s</string>' +
' </value>' +
' </member>' +
' <member>' +
' <name>moviehash</name>' +
' <value><string>%2:s</string></value>' +
' </member>' +
' <member>' +
' <name>moviebytesize</name>' +
' <value><double>%3:d</double></value>' +
' </member>' +
' </struct>' +
' </value>' +
' </data>' +
' </array>' +
' </value>' +
' </param>' +
' </params>' +
'</methodCall>';
begin
//TODO: XML Encoding
Result:= XML_RPC(Format(SEARCH_SUBTITLES, [aToken, aSublanguageID, aMovieHash, aMovieByteSize]));
end;
function SearchSubtitles(aToken, aSublanguageID: string;
aImdbID: Cardinal): string;
const
SEARCH_SUBTITLES = '<?xml version="1.0"?>' +
'<methodCall>' +
' <methodName>SearchSubtitles</methodName>' +
' <params>' +
' <param>' +
' <value><string>%0:s</string></value>' +
' </param>' +
' <param>' +
' <value>' +
' <array>' +
' <data>' +
' <value>' +
' <struct>' +
' <member>' +
' <name>sublanguageid</name>' +
' <value><string>%1:s</string>' +
' </value>' +
' </member>' +
' <member>' +
' <name>imdbid</name>' +
' <value><string>%2:d</string></value>' +
' </member>' +
' </struct>' +
' </value>' +
' </data>' +
' </array>' +
' </value>' +
' </param>' +
' </params>' +
'</methodCall>';
begin
//TODO: XML Encoding
Result:= XML_RPC(Format(SEARCH_SUBTITLES, [aToken, aSublanguageID, aImdbID]));
end;
function SearchSubtitles(aToken, aSublanguageID,
aQuery: string): string;
const
SEARCH_SUBTITLES = '<?xml version="1.0"?>' +
'<methodCall>' +
' <methodName>SearchSubtitles</methodName>' +
' <params>' +
' <param>' +
' <value><string>%0:s</string></value>' +
' </param>' +
' <param>' +
' <value>' +
' <array>' +
' <data>' +
' <value>' +
' <struct>' +
' <member>' +
' <name>sublanguageid</name>' +
' <value><string>%1:s</string>' +
' </value>' +
' </member>' +
' <member>' +
' <name>query</name>' +
' <value><string>%2:s</string></value>' +
' </member>' +
' </struct>' +
' </value>' +
' </data>' +
' </array>' +
' </value>' +
' </param>' +
' </params>' +
'</methodCall>';
begin
//TODO: XML Encoding
Result:= XML_RPC(Format(SEARCH_SUBTITLES, [aToken, aSublanguageID, aQuery]));
end;
end.
最后,我使用C ++ Builder做下一步:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString response = LogIn("", "", "en", "OS Test User Agent");
XMLDocument1->XML->Text = response;
XMLDocument1->Active = true;
UnicodeString token = XMLDocument1->ChildNodes->Nodes[1]->GetChildNodes()->Nodes[0]->
GetChildNodes()->Nodes[0]->GetChildNodes()->Nodes[0]->
GetChildNodes()->Nodes[0]->GetChildNodes()->Nodes[0]->
GetChildNodes()->Nodes[1]->GetChildNodes()->Nodes[0]->
GetText();
Memo1->Text = SearchSubtitles(token, "en", "7d9cd5def91c9432", 735934464);
LogOut(token);
}
当我执行此操作时,我得到这样的回复:
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>status</name>
<value>
<string>200 OK</string>
</value>
</member>
<member>
<name>data</name>
<value>
<boolean>0</boolean>
</value>
</member>
<member>
<name>seconds</name>
<value>
<double>0.008</double>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
那么,下一步是什么?如何获取文件(字幕)位置? 感谢。
答案 0 :(得分:0)
错误是:OpenSubtitles需要包含3个字符的国家/地区代码(“eng”代替“en”)。