当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript cheerio-httpcli.fetch函数代码示例

本文整理汇总了TypeScript中cheerio-httpcli.fetch函数的典型用法代码示例。如果您正苦于以下问题:TypeScript fetch函数的具体用法?TypeScript fetch怎么用?TypeScript fetch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了fetch函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: analyzeWikipedia

function analyzeWikipedia(req: express.Request, res: express.Response, url: URL.Url): void {

	const title: string = decodeURI(url.pathname.split('/')[2]);

	client.fetch(url.href).then((result: any) => {
		if (result.error !== undefined && result.error !== null) {
			return res.sendStatus(500);
		}

		const $: any = result.$;

		const text: string = $('#mw-content-text > p:first-of-type').text();

		// Favicon
		const icon: string = URL.resolve(url.href, $('link[rel="shortcut icon"]').attr('href'));

		const compiler: (locals: any) => string = jade.compileFile(
			`${__dirname}/summary.jade`);

		const viewer = compiler({
			url: url,
			title,
			icon,
			description: text,
			image: 'https://ja.wikipedia.org/static/images/project-logos/enwiki.png',
			siteName: 'Wikipedia'
		});

		res.send(viewer);
	}, (err: any) => {
		res.sendStatus(204);
	});
}
开发者ID:armchair-philosophy,项目名称:Misskey-Web,代码行数:33,代码来源:analyze.ts

示例2: analyzeImgur

function analyzeImgur(req: express.Request, res: express.Response, url: URL.Url): void {

	if (url.hostname === 'i.imgur.com') {
		showImage(res, wrapMisskeyProxy(url.href), url.href);
	} else {
		// リクエスト送信
		client.fetch(url.href).then((result: any) => {
			if (result.error !== undefined && result.error !== null) {
				return res.sendStatus(204);
			}

			const contentType: string = result.response.headers['content-type'];
			// HTMLじゃなかった場合は中止
			if (contentType.indexOf('text/html') === -1) {
				return res.sendStatus(204);
			}

			const $: any = result.$;
			let src = or(
				$('meta[property="misskey:image"]').attr('content'),
				$('meta[property="og:image"]').attr('content'),
				$('meta[property="twitter:image"]').attr('content'),
				$('link[rel="image_src"]').attr('href'),
				$('link[rel="apple-touch-icon"]').attr('href'),
				$('link[rel="apple-touch-icon image_src"]').attr('href'));
			if (src === null) {
				return res.sendStatus(204);
			}

			showImage(res, wrapMisskeyProxy(src.replace(URL.parse(src).search, "")), url.href);
		}, (err: any) => {
			res.sendStatus(204);
		});
	}
}
开发者ID:armchair-philosophy,项目名称:Misskey-Web,代码行数:35,代码来源:analyze.ts

示例3: if

	app.get(`/subdomain/${config.publicConfig.shieldDomain}/*`, (req, res) => {
		const url = req.url.replace(`/subdomain/${config.publicConfig.shieldDomain}/`, '');
		client.fetch(url).then((result: any) => {
			if (result.error !== undefined && result.error !== null) {
				return res.sendStatus(204);
			} else if (result.response.statusCode !== 200) {
				return res.sendStatus(204);
			}

			const contentType: string = result.response.headers['content-type'];

			if (contentType.indexOf('text/html') !== -1) {
				return res.sendStatus(204);
			}

			res.send(result.response.body);
		}, (err: any) => {
			res.sendStatus(204);
		});
	});
开发者ID:armchair-philosophy,项目名称:Misskey-Web,代码行数:20,代码来源:shield-server.ts

示例4: analyzeGithubGist

function analyzeGithubGist(req: express.Request, res: express.Response, url: URL.Url): void {

	client.fetch(url.href).then((result: any) => {
		if (result.error !== undefined && result.error !== null) {
			return res.sendStatus(204);
		}

		const $: any = result.$;

		const avatarUrl: string = $('meta[property="og:image"]').attr('content');
		const userName: string = $('meta[name="octolytics-dimension-owner_login"]').attr('content');
		const fileName: string = $('.gist-header-title > a').text();
		const description: string = $('meta[property="og:description"]').attr('content');
		const $rawButton = $('#gist-pjax-container .js-gist-file-update-container > .file > .file-header > .file-actions > .btn');
		const resolvedRawUrl = URL.resolve('https://gist.githubusercontent.com', $rawButton.attr('href'));

		request(resolvedRawUrl, (getRawErr: any, getRawResponse: any, raw: any) => {
			if (getRawErr !== null) {
				return res.sendStatus(204);
			} else if (getRawResponse.statusCode !== 200) {
				return res.sendStatus(204);
			} else {
				const compiler: (locals: any) => string = jade.compileFile(
					`${__dirname}/gist.jade`);

				const viewer: string = compiler({
					url: url.href,
					avatarUrl,
					userName,
					fileName,
					description,
					raw
				});

				res.send(viewer);
			}
		});
	});
}
开发者ID:armchair-philosophy,项目名称:Misskey-Web,代码行数:39,代码来源:analyze.ts

示例5: analyzeGeneral

/**
 * @param req MisskeyExpressRequest
 * @param res MisskeyExpressResponse
 * @param url url
 */
function analyzeGeneral(req: express.Request, res: express.Response, url: URL.Url): void {

	// リクエスト送信
	client.fetch(url.href).then((result: any) => {
		if (result.error !== undefined && result.error !== null) {
			return res.sendStatus(204);
		}

		const contentType: string = result.response.headers['content-type'];

		// HTMLじゃなかった場合は中止
		if (contentType.indexOf('text/html') === -1) {
			return res.sendStatus(204);
		}

		const $: any = result.$;

		let title = or(
			$('meta[property="misskey:title"]').attr('content'),
			$('meta[property="og:title"]').attr('content'),
			$('meta[property="twitter:title"]').attr('content'),
			$('title').text());
		if (title === null) {
			return res.sendStatus(204);
		}
		title = clip(entities.decode(title), 100);

		const lang: string = $('html').attr('lang');

		const type = or(
			$('meta[property="misskey:type"]').attr('content'),
			$('meta[property="og:type"]').attr('content'));

		let image = or(
			$('meta[property="misskey:image"]').attr('content'),
			$('meta[property="og:image"]').attr('content'),
			$('meta[property="twitter:image"]').attr('content'),
			$('link[rel="image_src"]').attr('href'),
			$('link[rel="apple-touch-icon"]').attr('href'),
			$('link[rel="apple-touch-icon image_src"]').attr('href'));
		image = image !== null ? wrapMisskeyProxy(URL.resolve(url.href, image)) : null;

		let description = or(
			$('meta[property="misskey:summary"]').attr('content'),
			$('meta[property="og:description"]').attr('content'),
			$('meta[property="twitter:description"]').attr('content'),
			$('meta[name="description"]').attr('content'));
		description = description !== null
			? clip(entities.decode(description), 300)
			: null;

		if (title === description) {
			description = null;
		}

		let siteName = or(
			$('meta[property="misskey:site-name"]').attr('content'),
			$('meta[property="og:site_name"]').attr('content'),
			$('meta[name="application-name"]').attr('content'));
		siteName = siteName !== null ? entities.decode(siteName) : null;

		let icon = or(
			$('meta[property="misskey:site-icon"]').attr('content'),
			$('link[rel="shortcut icon"]').attr('href'),
			$('link[rel="icon"]').attr('href'),
			'/favicon.ico');
		icon = icon !== null ? wrapMisskeyProxy(URL.resolve(url.href, icon)) : null;

		const compiler: (locals: any) => string = jade.compileFile(
			`${__dirname}/summary.jade`);

		// コンパイル
		const viewer: string = compiler({
			url: url,
			title: title,
			icon: icon,
			lang: lang,
			description: description,
			type: type,
			image: image,
			siteName: siteName
		});

		res.send(viewer);
	}, (err: any) => {
		res.sendStatus(204);
	});
}
开发者ID:armchair-philosophy,项目名称:Misskey-Web,代码行数:93,代码来源:analyze.ts


注:本文中的cheerio-httpcli.fetch函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。