⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templateex.cpp

📁 概述:数据的纵向收集
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	LOG("LetMeSee:(%s)",strValue1);

	return 0;
}

static int SetConstantVar(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-3);//varname
	const char* strValue2 = lua_tostring(L,-2);//url
	const char* strValue3 = lua_tostring(L,-1);//pattern
	CString strResult="";
	if (debug_template_file == 0)
	{
		CCrawler::DownloadAndRegexPage((char*)strValue2,NULL,(char*)strValue3,strResult);
		if(strResult != "")
		{
			//加入map
			 CTemplateEx::s_arrMapConstant[strValue1]=strResult;
		}
	}
	else
	{
		OutputDebugInfo("SetConstantVar", strValue1,strValue2,strValue3, "", "");
	}

	return 0;
}

static int GetConstantVar(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-1);//url
	std::map<std::string,std::string>::iterator   pos;   
	pos = CTemplateEx::s_arrMapConstant.find(strValue1);
	if(pos != CTemplateEx::s_arrMapConstant.end())
	{
		lua_pushstring(L,pos->second.c_str());
	}
	else
	{
		lua_pushstring(L,"");
	}
	return 1;
}

static int DownloadAndRegexPage(lua_State *L)
{
	const char* strValue2 = lua_tostring(L,-2);//url
	const char* strValue3 = lua_tostring(L,-1);//pattern
	CString strResult="";
	CCrawler::DownloadAndRegexPage((char*)strValue2,NULL,(char*)strValue3,strResult);
	if(strResult != "")
	{
		lua_pushstring(L,strResult);
	}
	else
	{
		lua_pushstring(L,"");
	}
	return 1;
}

static int SetDiscuzCookie(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-2);//rooturl
	const char* strValue2 = lua_tostring(L,-1);//fixcookie
	CString strResult="";
	CCrawler::DownloadAndRegexHeader((char*)strValue1,(char*)strValue2,"cdb_sid=[a-zA-Z0-9]+",strResult);
	if(strResult != "")
	{
		CTemplate::Instance()->SetCookie(strResult);		
	}
	else
	{
		DebugString("SetDiscuzCookie","cann't found regex result");
	}
	return 0;
}
static int SetDvbbsCookie(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-1);//rooturl
	char szOutBuf[10240]={0};
	DWORD dwSize = 10240;
	//从本地文件得到cookie
	if(InternetGetCookie((char*)strValue1,NULL,szOutBuf,&dwSize))
	{
		CString strResult="";
		CCrawler::DownloadAndRegexHeader((char*)strValue1,szOutBuf,"ASPSESSIONID.*?=[A-Z]+",strResult);
		if(strResult != "")
		{
			CTemplate::Instance()->SetCookie(strResult);		
		}
		else
		{
			DebugString("SetDvbbsCookie","cann't found regex result");
		}
	}
	else
	{
		DWORD dwCode = GetLastError();
		DebugString("SetDvbbsCookie","error code: %d",dwCode);
	}
	
	return 0;

}

static int SetCookie(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-1);//cookie
	CTemplate::Instance()->SetCookie(strValue1);
	return 0;
}
static int SetFilterReplace(lua_State *L)
{
	const char* strValue = lua_tostring(L,-1);

	CTemplate::Instance()->SetFilter(strValue,FT_REPLACE);
	return 0;
}

static int RegisterReplaceCallback(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-2);
	const char* strValue2 = lua_tostring(L,-1);

	CTemplateEx::SetReplaceCallback(strValue1,strValue2);
	return 0;
}

static int AddFingerFilter(lua_State *L)
{
	const char* strValue1 = lua_tostring(L,-2);
	const char* strValue2 = lua_tostring(L,-1);

	CTemplate::Instance()->AddFingerFilter(strValue1,strValue2);
	return 0;
}

static int SetNULLInBody(lua_State *L)
{
	int nValue = (int)lua_tointeger(L,-1);
	CTemplate::Instance()->SetNULLInBody(nValue);
	return 0;
}


//////////////////////////////////////////////////////////////////////////


#undef xx
#define xx(a) {a,#a}
static struct {
lua_CFunction pFoo;
char* lpszFoo;
}arrFoo[]={
xx(SetLog),
xx(SetSid),
xx(SetThread),
xx(SetCTimeOut),
xx(SetConnect),
xx(SetUserAgent),
xx(SetSeedPage),
xx(SetSeedPattern),
xx(SetConnentType),
xx(SetFilterSkip),
xx(SetFilterParse),
xx(SetFilterStore),
xx(SetFilterReplace),
xx(SetUtf8),
xx(AddTask),
xx(AddTaskEx),
xx(RegisterStoreCallback),
xx(RegisterStoreCallbackEx),
xx(RegisterStoreCallbackEx2),
xx(RegisterReplaceCallback),
xx(AddTaskStoreAlbum),
xx(AddTaskStoreSong),
xx(AddTaskStoreLyric),
xx(AddTaskStoreLink),
xx(AddTaskStoreInfo),
xx(SetDebugLua),
xx(JsCall),
xx(LetMeSee),
xx(SetConstantVar),
xx(GetConstantVar),
xx(DownloadAndRegexPage),
xx(SetDiscuzCookie),
xx(SetDvbbsCookie),
xx(SetCookie),
xx(AddFingerFilter),
xx(SetNULLInBody),
{NULL,NULL}
};
#undef xx

static void RegisterFoo(lua_State *L)
{
	for(int i=0;i < sizeof(arrFoo)/sizeof(arrFoo[0]) && arrFoo[i].lpszFoo!=NULL;i++)
	{
		lua_pushcfunction(L, arrFoo[i].pFoo);
		lua_setglobal(L,arrFoo[i].lpszFoo);		
	}
}

//////////////////////////////////////////////////////////////////////////
CString CTemplateEx::s_strScript;
std::vector<CCallBackTag> CTemplateEx::s_arrStoreCallback;
std::vector<CCallBackTag> CTemplateEx::s_arrReplaceCallback;
std::vector<CCallBackTag> CTemplateEx::s_arrStoreCallbackEx;
std::vector<CCallBackTag> CTemplateEx::s_arrStoreCallbackEx2;
std::map<std::string,std::string> CTemplateEx::s_arrMapConstant;

//////////////////////////////////////////////////////////////////////////
void CTemplateEx::Load(const CString& strPath)
{
	LoadScript(strPath);
	CallInit();
}

void CTemplateEx::LoadScript(const CString& strPath)
{
	CHAR pBuffer[MAX_PATH];
	std::ifstream in;

	s_strScript="";
	in.open(strPath);
	while(!in.eof())
	{
		memset(pBuffer,0,MAX_PATH);
		in.getline(pBuffer,MAX_PATH);
		s_strScript.Append(pBuffer);
		s_strScript.Append("\n");
	}
	in.close();
}

//////////////////////////////////////////////////////////////////////////
void CTemplateEx::CallInit()
{
	lua_State *L = luaL_newstate();
	if(L==NULL)
	{
		lua_message("cannot create state: not enough memory");
		return;
	}

	luaL_openlibs(L);
	//lua_cpcall(L,luaopen_base,0);
	//lua_cpcall(L,luaopen_io,0);
	//lua_cpcall(L,luaopen_string,0);

	//register
	RegisterFoo(L);

	//invoke,技巧:把脚本全部写成函数,那么在这里就可以使用dostring了,然后需要初始化的东西
	//通过调用特定的函数就可以了,loadstring在这里不行,tmd
	//2007.2.8,sunwang
	int rc = luaL_dostring(L,s_strScript);
	if(rc !=0)
	{
		DebugString("[CTemplateEx::CallInit]","luaL_loadstring error(rc=%d)",rc);
		lua_report(L,rc);
	}
	else
	{
		LPCSTR lpszFoo=SCRIPT_INIT_FOO;
		lua_getglobal(L,lpszFoo);
		rc = lua_pcall(L,0,0,0);
		if(rc !=0)
		{
			DebugString("[CTemplateEx::CallInit]","lua_pcall error(rc=%d)",rc);
			lua_report(L,rc);
		}
	}

	lua_close(L);
}

void CTemplateEx::CallStoreCallback(LPCSTR lpszTask)
{
	if(s_arrStoreCallback.size()==0)
		return;

	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	//lua_cpcall(L,luaopen_base,0);
	//lua_cpcall(L,luaopen_io,0);
	//lua_cpcall(L,luaopen_string,0);

	//register
	RegisterFoo(L);

	//invoke
	int rc = luaL_dostring(L,s_strScript);
	if(rc !=0)
	{
		DebugString("[CTemplateEx::CallStoreCallback]","luaL_loadstring error(rc=%d)",rc);
		lua_report(L,rc);
	}
	else
	{
		for (int i=0;i<(int)s_arrStoreCallback.size();i++)
		{
			LPCSTR lpszFoo=s_arrStoreCallback[i].strFunction;
			LPCSTR lpszPatternTask=s_arrStoreCallback[i].strPatternTask;
			if(CheckTask(lpszPatternTask,lpszTask))
			{
				lua_getglobal(L,lpszFoo);
				lua_pushstring(L,lpszTask);
				rc = lua_pcall(L,1,0,0);
				if(rc !=0)
				{
					DebugString("[CTemplateEx::CallStoreCallback]","lua_pcall error(rc=%d)",rc);
					lua_report(L,rc);
				}
			}
		}
	}

	lua_close(L);
}

void CTemplateEx::CallReplaceCallback(LPCSTR lpszTask)
{
	if(s_arrReplaceCallback.size()==0)
		return;

	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	//lua_cpcall(L,luaopen_base,0);
	//lua_cpcall(L,luaopen_io,0);
	//lua_cpcall(L,luaopen_string,0);

	//register
	RegisterFoo(L);

	//invoke
	int rc = luaL_dostring(L,s_strScript);
	if(rc !=0)
	{
		DebugString("[CTemplateEx::CallReplaceCallback]","luaL_loadstring error(rc=%d)",rc);
		lua_report(L,rc);
	}
	else
	{
		for (int i=0;i<(int)s_arrReplaceCallback.size();i++)
		{
			LPCSTR lpszFoo=s_arrReplaceCallback[i].strFunction;
			LPCSTR lpszPatternTask=s_arrReplaceCallback[i].strPatternTask;
			if(CheckTask(lpszPatternTask,lpszTask))
			{
				lua_getglobal(L,lpszFoo);
				lua_pushstring(L,lpszTask);
				rc = lua_pcall(L,1,0,0);
				if(rc !=0)
				{
					DebugString("[CTemplateEx::CallReplaceCallback]","lua_pcall error(rc=%d)",rc);
					lua_report(L,rc);
				}
			}
		}
	}

	lua_close(L);
}

void CTemplateEx::CallStoreCallbackEx(LPCSTR lpszTask,LPCSTR lpszContent)
{
	if(s_arrStoreCallbackEx.size()==0)
		return;

	if(lpszContent == NULL || strlen(lpszContent) == 0)
	{
		LOGE("error: blank content url=%s",lpszTask);
	}
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	//lua_cpcall(L,luaopen_base,0);
	//lua_cpcall(L,luaopen_io,0);
	//lua_cpcall(L,luaopen_string,0);

	//register
	RegisterFoo(L);

	//invoke
	int rc = luaL_dostring(L,s_strScript);
	if(rc !=0)
	{
		DebugString("[CTemplateEx::CallStoreCallbackEx]","luaL_loadstring error(rc=%d)",rc);
		lua_report(L,rc);
	}
	else
	{
		for (int i=0;i<(int)s_arrStoreCallbackEx.size();i++)
		{
			LPCSTR lpszFoo=s_arrStoreCallbackEx[i].strFunction;
			LPCSTR lpszPatternTask=s_arrStoreCallbackEx[i].strPatternTask;
			LPCSTR lpszPatternContent=s_arrStoreCallbackEx[i].strPatternContent;
			
			CString strResult;
			if( CheckTask(lpszPatternTask,lpszTask))
			{
				if(f_strlen(lpszPatternContent))
				{
					//content pattern!!! just push result
					if(CheckContent(lpszPatternContent,lpszContent,strResult))
					{
						lua_getglobal(L,lpszFoo);	  //-3,foo name
						lua_pushstring(L,lpszTask);   //-2,task url
						lua_pushstring(L,strResult);  //-1,content
						rc = lua_pcall(L,2,0,0);      //两个参数 0个返回值
						if(rc !=0)
						{
							DebugString("[CTemplateEx::CallStoreCallbackEx]","lua_pcall error(rc=%d)",rc);
							lua_report(L,rc);
						}
					}
					else
					{
						//not match content
					}
				}
				else
				{
					//no content pattern,so just push the content all
					lua_getglobal(L,lpszFoo);	  //-3,foo name
					lua_pushstring(L,lpszTask);   //-2,task url
					lua_pushstring(L,lpszContent);//-1,content
					rc = lua_pcall(L,2,0,0);      //两个参数 0个返回值
					if(rc !=0)
					{
						DebugString("[CTemplateEx::CallStoreCallbackEx]","lua_pcall error(rc=%d)",rc);
						lua_report(L,rc);
					}
				}
			}
			else
			{
				//not match task
			}
		}
	}

	lua_close(L);
}

void CTemplateEx::CallStoreCallbackEx2(LPCSTR lpszTask,LPCSTR lpszHttpHeader,LPCSTR lpszContent)
{
	if(s_arrStoreCallbackEx2.size()==0)
		return;

	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
	//lua_cpcall(L,luaopen_base,0);
	//lua_cpcall(L,luaopen_io,0);
	//lua_cpcall(L,luaopen_string,0);

	//register
	RegisterFoo(L);

	//invoke
	int rc = luaL_dostring(L,s_strScript);
	if(rc !=0)
	{
		DebugString("[CTemplateEx::CallStoreCallbackEx]","luaL_loadstring error(rc=%d)",rc);
		lua_report(L,rc);
	}
	else
	{
		for (int i=0;i<(int)s_arrStoreCallbackEx2.size();i++)
		{
			LPCSTR lpszFoo=s_arrStoreCallbackEx2[i].strFunction;
			LPCSTR lpszPatternTask=s_arrStoreCallbackEx2[i].strPatternTask;
			LPCSTR lpszPatternContent=s_arrStoreCallbackEx2[i].strPatternContent;
			
			CString strResult;
			if( CheckTask(lpszPatternTask,lpszTask))
			{
				if(f_strlen(lpszPatternContent))
				{
					//content pattern!!! just push result
					if(CheckContent(lpszPatternContent,lpszContent,strResult))
					{
						lua_getglobal(L,lpszFoo);	  //-4,foo name
						lua_pushstring(L,lpszTask);   //-3,task url
						lua_pushstring(L,lpszHttpHeader);  //-2,httpheader
						lua_pushstring(L,strResult);  //-1,content
						rc = lua_pcall(L,3,0,0);      //3个参数 0个返回值
						if(rc !=0)
						{
							DebugString("[CTemplateEx::CallStoreCallbackEx]","lua_pcall error(rc=%d)",rc);
							lua_report(L,rc);
						}
					}
					else
					{
						//not match content
					}
				}
				else
				{
                    //no content pattern,so just push the content all
                    lua_getglobal(L,lpszFoo);	  //-4,foo name
                    lua_pushstring(L,lpszTask);   //-3,task url
                    lua_pushstring(L,lpszHttpHeader);  //-2,httpheader
                    lua_pushstring(L,lpszContent);  //-1,content
                    rc = lua_pcall(L,3,0,0);      //3个参数 0个返回值
					if(rc !=0)
					{
						DebugString("[CTemplateEx::CallStoreCallbackEx]","lua_pcall error(rc=%d)",rc);
						lua_report(L,rc);
					}
				}
			}
			else
			{
				//not match task
			}
		}
	}

	lua_close(L);
}


//////////////////////////////////////////////////////////////////////////
BOOL CTemplateEx::CheckTask(LPCSTR lpszPattern,LPCSTR lpszString)
{
	BOOL bResult=FALSE;
	const char* lpszError=NULL;
	int nErrorOffset=0;
	int rc=0;
	int ovector[30]={0};/* should be a multiple of 3 */

	pcre* re = pcre_compile(lpszPattern,0,&lpszError,&nErrorOffset,NULL);
	if(re)
	{
		rc = pcre_exec(re,NULL,lpszString,(int)f_strlen(lpszString),0,0,ovector,30);
		if(rc<0)
		{
			switch(rc)
			{
			case PCRE_ERROR_NOMATCH:
				break;
			default:
				break;
			}
		}
		else
		{
			bResult=TRUE;
		}

		if(lpszError)
		{
			pcre_free_substring(lpszError);
		}
		pcre_free(re);
	}

	return bResult;
}

BOOL CTemplateEx::CheckContent(LPCSTR lpszPattern,LPCSTR lpszString,CString& strResult)
{
	BOOL bResult=FALSE;
	const char* lpszError=NULL;
	int nErrorOffset=0;
	int rc=0;
	int ovector[30]={0};/* should be a multiple of 3 */

	pcre* re = pcre_compile(lpszPattern,PCRE_DOTALL,&lpszError,&nErrorOffset,NULL);
	if(re)
	{
		rc = pcre_exec(re,NULL,lpszString,(int)f_strlen(lpszString),0,0,ovector,30);
		if(rc<0)
		{
			switch(rc)
			{
			case PCRE_ERROR_NOMATCH:
				break;
			default:
				break;
			}
		}
		else
		{
			bResult=TRUE;
			strResult.Empty();
			strResult.Append(lpszString+ovector[2],ovector[3]-ovector[2]);
		}

		if(lpszError)
		{
			pcre_free_substring(lpszError);
		}
		pcre_free(re);
	}

	return bResult;
}
//////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -