int CSVPToolBox::CopyFile(std::wstring sourcePath, std::wstring destPath) { char* strsource=WideChar2MultiByte(sourcePath.c_str()); char* strdest=WideChar2MultiByte(destPath.c_str()); char s[512]; sprintf(s,"cp %s \"%s\"",strsource,strdest); system(s); return 0; }
bool CSVPToolBox::ifFileExist_STL(std::wstring szPathname, bool evenSlowDriver) { std::wstring szPathExt = szPathname.substr(0, 6); std::transform(szPathExt.begin(), szPathExt.end(), szPathExt.begin(), ::tolower); if (szPathExt == L"rar://") { /* //RARTODO: 检测rar内的文件是否存在 //Done CSVPRarLib svpRar; if(svpRar.SplitPath_STL(szPathname)) szPathname = (LPCTSTR)svpRar.m_fnRAR; else return false;*/ printf("rar file!\n"); } struct stat sbuf; return (!stat(WideChar2MultiByte(szPathname.c_str()), &sbuf) && S_IFREG & sbuf.st_mode);//Get status information on a file. }
std::wstring CSVPToolBox::DetectSubFileLanguage_STL(std::wstring fn) { std::wstring szRet = L".chn"; FILE *stream ; if ( (stream=fopen(WideChar2MultiByte(fn.c_str()), "rb")) ==NULL) { //detect bom? int totalWideChar = 0; int totalChar = 0; int ch; for(int i=0; (feof( stream ) == 0 ); i++) { ch = 0xff & fgetc(stream); if (ch >= 0x80 ) totalWideChar++; totalChar++; } fclose( stream ); if(totalWideChar < (totalChar / 10) && totalWideChar < 1700) szRet = L".eng"; } return szRet; }
void CSVPToolBox::filePutContent_STL(std::wstring szFilePath, std::wstring szData, bool bAppend) { FILE* f; if ( (f=fopen(WideChar2MultiByte(szFilePath.c_str()), "w"))=NULL) { fwrite(szData.c_str(), sizeof(wchar_t), szData.length(), f); fclose(f); } }
int main(int argc, char* argv[]) { if (argc!=2) { printf("Usage:GetSubtitle videopath\n"); return 0; } printf("Please wait...\n"); setlocale(LC_ALL,""); SubTransFormat SubTF; char* strfilepath=argv[1]; std::wstring filepath=MutliByte2WideChar(strfilepath); std::wstring filehash=SubTF.ComputerFileHash_STL(strfilepath); std::wstring shortname= SubTF.GetShortFileNameForSearch(filepath); char* strfilehash=WideChar2MultiByte(filehash.c_str()); char* strshortname=WideChar2MultiByte(shortname.c_str()); std::wstring uniqueIDHash2=SubTF.genVHash(strfilepath,strfilehash); char* struniqueIDHash2=WideChar2MultiByte(uniqueIDHash2.c_str()); printf("hash:%s\nshortname:%s\nuniqueIDHash:%s\n",strfilehash,strshortname,struniqueIDHash2); struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; char* form; form=WideChar2MultiByte(filepath.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "pathinfo", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); form=WideChar2MultiByte(filehash.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "filehash", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); form=WideChar2MultiByte(uniqueIDHash2.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "vhash", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); form=WideChar2MultiByte(shortname.c_str()); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "shortname", CURLFORM_COPYCONTENTS, form,CURLFORM_END); free(form); CURL* curl = curl_easy_init(); SetCURLopt(curl); char url[100]; char filename[L_tmpnam]; tmpnam(filename); FILE* fp=fopen(filename,"wb+"); //FILE* fp=tmpfile(); if(fp==NULL) return 0; printf("Searching for subtitles\n\n"); for (int i=1;i<=7;i++) { GetUrlByType('sapi',i,url); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode res = curl_easy_perform(curl); if (res==0) { int respcode=0; curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &respcode); if (respcode==200) { ExtractDataFromAiSubRecvBuffer_STL(fp); } break; } else { long l_oserr = 0; curl_easy_getinfo(curl,CURLINFO_OS_ERRNO,&l_oserr); printf(curl_easy_strerror(res)); } } curl_easy_cleanup(curl); std::wstring szSubFilePath; for (int i = 0; i < toolbox.szaSubTmpFileList.size(); i++) { szSubFilePath = toolbox.getSubFileByTempid_STL(i,strfilepath); if (szSubFilePath == L"EXIST") printf("\nThe same subtitle already existed!\n"); else if (!szSubFilePath.empty()) printf("\nSubtitle downloaded to %s\n\n",WideChar2MultiByte(szSubFilePath.c_str())); else printf("\nFail to get sub file\n"); } return 0; }
std::wstring CSVPToolBox::getSubFileByTempid_STL(int iTmpID,char* strPath) { //get base path name std::wstring szVidPath=MutliByte2WideChar(strPath); std::vector<std::wstring> szVidPathInfo; std::wstring szTargetBaseName = L""; std::wstring szDefaultSubPath = L""; std::wstring StoreDir = L""; getVideoFileBasename(szVidPath, &szVidPathInfo); StoreDir = szVidPathInfo.at(SVPATH_DIRNAME).c_str(); std::wstring tmBasenamePath=StoreDir; getVideoFileBasename(szVidPath, &szVidPathInfo); tmBasenamePath+=(szVidPathInfo.at(SVPATH_FILENAME).c_str()); std::wstring szBasename = tmBasenamePath; //set new file name std::vector<std::wstring> szSubfiles; //std::wstring szXTmpdata = this->szaSubTmpFileList.GetAt(iTmpID); std::wstring szXTmpdata = this->szaSubTmpFileList.at(iTmpID); Explode(szXTmpdata, L";", &szSubfiles); bool bIsIdxSub = false; int ialreadyExist = 0; if (szSubfiles.size() < 1) printf("Not enough files in tmp array"); for(int i = 0; i < szSubfiles.size(); i++) { std::vector<std::wstring> szSubTmpDetail; Explode(szSubfiles[i], L"|", &szSubTmpDetail); if (szSubTmpDetail.size() < 2) { printf("Not enough detail in sub tmp string"); continue; } std::wstring szSource = szSubTmpDetail[1]; std::wstring szLangExt = L".chn"; //TODO: use correct language perm if(bIsIdxSub) szLangExt = L""; else szLangExt = DetectSubFileLanguage_STL(szSource);//check if english sub if (szSubTmpDetail[0].at(0) != L'.') szSubTmpDetail[0] = L"." + szSubTmpDetail[0]; std::wstring szTarget = szBasename + szLangExt + szSubTmpDetail[0]; szTargetBaseName = szBasename + szLangExt ; CMD5Checksum cm5source; std::wstring szSourceMD5 = cm5source.GetMD5(szSource); std::wstring szTargetMD5; //check if target exist wchar_t szTmp[128]; wcscpy(szTmp, L""); int ilan = 1; while(ifFileExist_STL(szTarget)) { //TODO: compare if its the same file cm5source.Clean(); szTargetMD5 = cm5source.GetMD5(szTarget); if(szTargetMD5 == szSourceMD5) { // TODO: if there is a diffrence in delay //printf("同样的字幕文件已经存在了\n"); ialreadyExist++; //TODO: 如果idx+sub里面只有一个文件相同怎么办 ??~~ break; } swprintf(szTmp,128, L"%d", ilan); szTarget = szBasename + szLangExt + (std::wstring)szTmp + szSubTmpDetail[0]; szTargetBaseName = szBasename + szLangExt + (std::wstring)szTmp; ilan++; } printf("Copying file\n"); CopyFile(szSource.c_str(), szTarget.c_str()); if (((bIsIdxSub && szSubTmpDetail[0].compare(L"idx") == 0) || !bIsIdxSub) && szDefaultSubPath.empty()) szDefaultSubPath = szTarget; std::vector<std::wstring> szaDesclines; if (szaSubDescs.size() > iTmpID) { Explode(szaSubDescs.at(iTmpID), L"\x0b\x0b", &szaDesclines); if(szaDesclines.size() > 0) { int iDelay = 0; swscanf(szaDesclines.at(0).c_str(), L"delay=%d", &iDelay); if (iDelay) { wchar_t szBuf[128]; swprintf(szBuf, 128, L"%d", iDelay); filePutContent_STL(szTarget + L".delay", (std::wstring)szBuf); } else remove(WideChar2MultiByte((szTarget + L".delay").c_str())); } } else printf("Count of szaSubDescs not match with count of subs "); unlink(WideChar2MultiByte(szSource.c_str())); } if(ialreadyExist) return L"EXIST"; else return szDefaultSubPath; }