将url分割,并判断每一部分是否符合一定的逻辑

#pragma once

extern "C" _declspec(dllexport) bool _SourceCanPlay_(char * str);


// FilterJudgement.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"

#include "FilterJudgement.h"

#include <regex>
#include <iostream>
#include <string>
#include <map>
#include <functional>
using namespace std;

#ifdef DEBUG
#ifdef _DEBUG
#define db DString
#else
#define db DString
#endif
#else
#define db
#endif

void DString(const char * str)
{
	cout<<str<<endl;
}

void MySplitFunc(const string & sourceStr , const char * delimeter , vector<string> & result_container)
{
	result_container.empty();
	if (sourceStr.length() == 0)return;

	int iLen = sourceStr.length();
	char * buffer = new char[iLen + 1];
	strcpy(buffer , sourceStr.c_str());
	buffer[iLen] = '\0';

	char * p = strtok(buffer , delimeter);
	while( p != NULL)
	{
		result_container.push_back(p);
		p = strtok(NULL , delimeter);
	}

	delete [] buffer;
}

void trimLeft(string & sourceStr)
{
	if ( strncmp(sourceStr.c_str() , "http://" , strlen("http://")) == 0 )
	{
		sourceStr.erase(0 , strlen("http://"));
	}
}

void trimRight(string & sourceStr)
{
	string html(".html");
	string shtml(".shtml");

	size_t pos = sourceStr.rfind(html);
	if (pos != string::npos)
	{
		sourceStr.erase(pos , html.length());
		return;
	}

	pos = sourceStr.rfind(shtml);
	if ( pos != string::npos )
	{
		sourceStr.erase(pos , shtml.length());
		return;
	}
}

void print(const vector<string> & str)
{
	for_each(str.begin() , str.end() , [&](string aStr)
	{
		cout<<aStr<<endl;
	});
}

typedef map<int,function<bool(const string &)> > Rule;

bool isUrlCanPlay(const vector<string> & str , int iMustPart , Rule rule)
{
	if (iMustPart != -1)
	{
		if (str.size() != iMustPart)return false;
	}

	int n = min( str.size() , rule.size() );

	for (int i=0;i<n;++i)
	{
		function<bool(const string &)> judge = rule[i];
		if ( !judge(str[i]) )
		{
			cout<<"the "<<i<<" condition is not correspond"<<endl;
			return false;
		}
	}

	return true;
}

bool isStrMatch(const string & sourceStr , const string & pat)
{
	const std::regex pattern(pat);
	return std::regex_match(sourceStr, pattern);
}

bool AlwaysTure(const string &)
{
	return true;
}

bool IsAllNum(const string & str)
{
	auto it = str.begin();
	for (;it != str.end() ; ++it)
	{
		if ( !isdigit(*it) )
		{
			return false;
		}
	}
	return true;
}

bool IsAllCh(const string & str)
{
	auto it = str.begin();
	for (;it != str.end() ; ++it)
	{
		if ( !isgraph(*it) )
		{
			return false;
		}
	}
	return true;
}

struct RuleBase
{
	Rule rule;

	virtual ~RuleBase(){}
};

struct _56_Judgement_A : public RuleBase
{
	_56_Judgement_A()
	{
		function<bool(const string &)> judge56a = std::bind(isStrMatch , placeholders::_1 , string("u(\\d{2,3})"));
		rule[0] = AlwaysTure;
		rule[1] = judge56a;
		rule[2] = AlwaysTure;
	}
};

struct _56_Judgement_B : public RuleBase
{
	_56_Judgement_B()
	{
		rule[0] = AlwaysTure;
		rule[1] = AlwaysTure;
	}
};

struct _cntv_Judgement : public RuleBase
{
	_cntv_Judgement()
	{
		rule[0] = AlwaysTure;
		rule[1] = AlwaysTure;
		rule[2] = IsAllNum;
		rule[3] = IsAllCh;
	}
};

struct _pps_Judgement : public RuleBase
{
	_pps_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "play_(\\w*)");
		rule[0] = AlwaysTure;
		rule[2] = filter;
	}
};

struct _pptv_Judgement : public RuleBase
{
	_pptv_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "show");
		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = AlwaysTure;
	}
};

struct _letv_Judgement : public RuleBase
{
	_letv_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "ptv");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "(p|v)play");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = filter2;
		rule[3] = IsAllNum;
	}
};

struct _youku_Judgement : public RuleBase
{
	_youku_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "v_show");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "id_.*");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = filter2;
	}
};

struct _tx_Judgement : public RuleBase
{
	_tx_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "cover");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "(\\d)|([A-Za-z])");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = filter2;
	}
};

struct _tudou_Judgement : public RuleBase
{
	_tudou_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "albumplay");

		rule[0] = AlwaysTure;
		rule[1] = filter;
	}
};

struct _m1905_Judgement : public RuleBase
{
	_m1905_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "vod");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "play");
		function<bool(const string &)> filter3 = bind(isStrMatch , placeholders::_1 , "(\\d+).*");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = filter2;
		rule[3] = filter3;
	}
};

struct _wasu_Judgement : public RuleBase
{
	_wasu_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "Play");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "show");
		function<bool(const string &)> filter3 = bind(isStrMatch , placeholders::_1 , "id");
		function<bool(const string &)> filter4 = bind(isStrMatch , placeholders::_1 , "(\\d+).*");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = filter2;
		rule[3] = filter3;
		rule[4] = filter4;
	}
};

struct _iqiyi_Judgement : public RuleBase
{
	_iqiyi_Judgement()
	{
		rule[0] = AlwaysTure;
		rule[1] = AlwaysTure;
		rule[2] = IsAllNum;
		rule[3] = IsAllCh;
	}
};

struct _sohu_Judgement : public RuleBase
{
	_sohu_Judgement()
	{
		rule[0] = AlwaysTure;
		rule[1] = IsAllNum;
		rule[2] = IsAllCh;
	}
};

struct _sina_Judgement : public RuleBase
{
	_sina_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "(\\d)|([A-Za-z])");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = IsAllCh;
	}
};

struct _xunlei_Judgement : public RuleBase
{
	_xunlei_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "v");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "(\\d+).*");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = IsAllNum;
		rule[3] = filter2;
	}
};

struct _funshion_Judgement : public RuleBase
{
	_funshion_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "subject");
		function<bool(const string &)> filter2 = bind(isStrMatch , placeholders::_1 , "play");
		function<bool(const string &)> filter3 = bind(isStrMatch , placeholders::_1 , "(\\d+).*");

		rule[0] = AlwaysTure;
		rule[1] = filter;
		rule[2] = AlwaysTure;
		rule[3] = filter3;
	}
};

struct _imgo_Judgement : public RuleBase
{
	_imgo_Judgement()
	{
		function<bool(const string &)> filter = bind(isStrMatch , placeholders::_1 , "clip(-(\\d+){5}");
		rule[0] = AlwaysTure;
		rule[1] = filter;
	}
};

enum Video_Source
{
	VIDEO_INVALID ,

	VIDEO_56_A , 
	VIDEO_56_B ,

	VIDEO_CNTV ,

	VIDEO_PPS ,

	VIDEO_PPTV ,

	VIDEO_LETV ,

	VIDEO_YOUKU ,

	VIDEO_TX ,

	VIDEO_TUDOU ,

	VIDEO_M1905 ,

	VIDEO_WASU ,

	VIDEO_IQIYI ,

	VIDEO_SOHU ,

	VIDEO_SINA ,

	VIDEO_XUNLEI ,

	VIDEO_FUNSHION ,

	VIDEO_IMGO ,

	VIDEO_COUNT ,
};


Video_Source UrlIsWhatSource(const string & str)
{
	function<bool(const string &)> is_56source_a = bind(isStrMatch , placeholders::_1 , "(56\\.com)|(www\\.56\\.com)");
	function<bool(const string &)> is_56source_b = bind(isStrMatch , placeholders::_1 , "56\\.pptv\\.com");
	function<bool(const string &)> is_cntv = bind(isStrMatch , placeholders::_1 , "hd\\.cntv\\.cn");
	function<bool(const string &)> is_pps = bind(isStrMatch , placeholders::_1 , "v\\.pps\\.tv");
	function<bool(const string &)> is_pptv = bind(isStrMatch , placeholders::_1 , "v\\.pptv\\.com");
	function<bool(const string &)> is_letv = bind(isStrMatch , placeholders::_1 , "www\\.letv\\.com");
	function<bool(const string &)> is_youku = bind(isStrMatch , placeholders::_1 , "v\\.youku\\.com");
	function<bool(const string &)> is_tx = bind(isStrMatch , placeholders::_1 , "(v|(film))\\.qq\\.com");
	function<bool(const string &)> is_tudou = bind(isStrMatch , placeholders::_1 , "(www\\.)?tudou\\.com");
	function<bool(const string &)> is_m1905 = bind(isStrMatch , placeholders::_1 , "(www\\.)?m1905\\.com");
	function<bool(const string &)> is_wasu = bind(isStrMatch , placeholders::_1 , "(www\\.)?wasu\\.cn");
	function<bool(const string &)> is_iqiyi = bind(isStrMatch , placeholders::_1 , "www\\.iqiyi\\.com");
	function<bool(const string &)> is_sohu = bind(isStrMatch , placeholders::_1 , "tv\\.sohu\\.com");
	function<bool(const string &)> is_sina = bind(isStrMatch , placeholders::_1 , "video\\.sina\\.com\\.cn");
	function<bool(const string &)> is_xunlei = bind(isStrMatch , placeholders::_1 , "vod\\.kankan\\.com");
	function<bool(const string &)> is_funshion = bind(isStrMatch , placeholders::_1 , "(www\\.)?funshion\\.com");
	function<bool(const string &)> is_imgo = bind(isStrMatch , placeholders::_1 , "(www\\.)?imgo\\.tv");

	if (is_56source_a(str))
	{
		db("56 a");
		return VIDEO_56_A;
	}

	if (is_56source_b(str))
	{
		db("56 b");

		return VIDEO_56_B;
	}

	if (is_cntv(str))
	{
		db("cntv");

		return VIDEO_CNTV;
	}

	if ( is_pps(str) )
	{
		db("pps");

		return VIDEO_PPS;
	}

	if ( is_pptv(str) )
	{
		db("pptv");

		return VIDEO_PPTV;
	}

	if ( is_letv(str) )
	{
		db("letv");

		return VIDEO_LETV;
	}

	if ( is_youku(str) )
	{
		db("youku");

		return VIDEO_YOUKU;
	}

	if ( is_tx(str) )
	{
		db("tx");

		return VIDEO_TX;
	}

	if ( is_tudou(str) )
	{
		db("tudou");

		return VIDEO_TUDOU;
	}

	if ( is_m1905(str) )
	{
		db("m1905");

		return VIDEO_M1905;
	}

	if ( is_wasu(str) )
	{
		db("wasu");

		return VIDEO_WASU;
	}

	if ( is_iqiyi(str) )
	{
		db("iqiyi");

		return VIDEO_IQIYI;
	}

	if ( is_sohu(str) )
	{
		db("sohu");

		return VIDEO_SOHU;
	}

	if ( is_sina(str) )
	{
		db("sina");

		return VIDEO_SINA;
	}

	if ( is_xunlei(str) )
	{
		db("xunlei");

		return VIDEO_XUNLEI;
	}

	if ( is_funshion(str) )
	{
		db("funshion");

		return VIDEO_FUNSHION;
	}

	if ( is_imgo(str) )
	{
		db("imgo");

		return VIDEO_IMGO;
	}

	db("没有来源");
	return VIDEO_INVALID;
}

bool SourceCanPlay(string & str)
{
	db("===========================");
	db(str.c_str());

	trimLeft(str);
	trimRight(str);

	db(str.c_str());

	vector<string> result;
	MySplitFunc(str , "/" , result);

	Video_Source source = UrlIsWhatSource(result[0]);

	RuleBase * pRule = NULL;

	switch(source)
	{
	case VIDEO_INVALID:
		return true;
	case VIDEO_56_A:
		pRule = new _56_Judgement_A;

		break;
	case VIDEO_56_B:
		pRule = new _56_Judgement_B;

		break;
	case VIDEO_CNTV:
		pRule = new _cntv_Judgement;

		break;
	case VIDEO_PPS:
		pRule = new _pps_Judgement;

		break;
	case VIDEO_PPTV:
		pRule = new _pptv_Judgement;

		break;
	case VIDEO_LETV:
		pRule = new _letv_Judgement;

		break;
	case VIDEO_YOUKU:
		pRule = new _youku_Judgement;

		break;

	case VIDEO_TX:
		pRule = new _tx_Judgement;

		break;

	case VIDEO_TUDOU:
		pRule = new _tudou_Judgement;

		break;

	case VIDEO_M1905:
		pRule = new _m1905_Judgement;

		break;

	case VIDEO_WASU:
		pRule = new _wasu_Judgement;

		break;

	case VIDEO_IQIYI:
		pRule = new _iqiyi_Judgement;

		break;

	case VIDEO_SOHU:
		pRule = new _sohu_Judgement;

		break;

	case VIDEO_SINA:
		pRule = new _sina_Judgement;

		break;

	case VIDEO_XUNLEI:
		pRule = new _xunlei_Judgement;

		break;

	case VIDEO_FUNSHION:
		pRule = new _funshion_Judgement;

		break;

	case VIDEO_IMGO:
		pRule = new _imgo_Judgement;

		break;
	default:
		return true;
	}

	bool ret = true;

	if (pRule)
	{
		ret = isUrlCanPlay(result , -1 , pRule->rule);

		delete pRule;
		pRule = NULL;
	}

	if (ret)
	{
		db("可播放来源");
	}
	else
	{
		db("不可播放来源");
	}

	db("===========================");

	return ret;
}

bool _SourceCanPlay_(char * str)
{
	string param(str);
	return SourceCanPlay(param);
}
判断每个url是否满足一定的规则

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值