Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
New Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mimorisuzuko committed Sep 4, 2020
1 parent 66a37b0 commit 59cd718
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 644 deletions.
2 changes: 1 addition & 1 deletion app/dst/main.js

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions app/dst/vendor.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@babel/polyfill": "^7.2.5",
"@babel/preset-env": "^7.3.4",
"@babel/preset-react": "^7.0.0",
"@sindresorhus/is": "^2.1.0",
"@types/electron": "^1.6.10",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
Expand Down Expand Up @@ -78,4 +77,4 @@
"webpack-dev-server": "^3.2.1"
},
"dependencies": {}
}
}
236 changes: 78 additions & 158 deletions src/fetchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,124 +5,49 @@ import { convertHtmlToHtmlString } from './util';
import liburl from 'url';
import dayjs from 'dayjs';
import urljoin from 'url-join';
import { renderToStaticMarkup } from 'react-dom/server';
import { renderTweetCard, renderInstgramCard, ogpCard } from './util';
import React from 'react';
import uuid from 'uuid/v4';

const dparser = new DOMParser();

export class LineBlog {
/**
* @param {string}
*/
static getURL(id) {
static getURL = (id) => {
return `https://lineblog.me/${id}`;
}
};

static async idToImageUrlAndName(id) {
static idToImageUrlAndName = async (id) => {
const body = await rp(LineBlog.getURL(id));
const $parsed = dparser.parseFromString(body, 'text/html');

return {
name: $parsed.querySelector('h2').innerText,
url: $parsed.querySelector('.profile-photo-thumbnail img').src
};
}
};

static async parse(body) {
static parse = async (body) => {
const $parsed = dparser.parseFromString(body, 'text/html');
const ret = [];

for (const $article of $parsed.querySelectorAll('.article')) {
const $title = $article.querySelector('.article-title a');
const mediaDic = {};
const $content = $article.querySelector('.article-body');

for (const $tweet of $content.querySelectorAll('.twitter-tweet')) {
const key = `_tweet_${uuid()}`;

mediaDic[key] = renderToStaticMarkup(
await renderTweetCard($tweet.children[1].href)
);
$tweet.outerHTML = key;
}

for (const $instagram of $content.querySelectorAll(
'.instagram-media'
)) {
const key = `_instagram_${uuid()}`;

mediaDic[key] = renderToStaticMarkup(
await renderInstgramCard(
$instagram.dataset.instgrmPermalink
)
);
$instagram.outerHTML = key;
}

for (const $video of $content.querySelectorAll('.uploaded-video')) {
const { src } = $video.querySelector('source');
const key = `_video_${uuid()}`;

mediaDic[key] = renderToStaticMarkup(
<div>
<video controls src={src} />
</div>
);
$video.outerHTML = key;
}

for (const $ogp of $content.querySelectorAll('.ogpLink')) {
const key = `_ogp_${uuid()}`;

mediaDic[key] = renderToStaticMarkup(
await ogpCard.render($ogp.href)
);
$ogp.outerHTML = key;
}

for (const $iframe of $content.querySelectorAll('iframe')) {
$iframe.outerText = renderToStaticMarkup(
<div>
<iframe
width='480'
height='270'
src={$iframe.src}
frameBorder='0'
allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture'
allowFullScreen
/>
</div>
);
}

ret.push({
date: dayjs($article.querySelector('.article-date').innerText),
title: $title.innerText,
author: $parsed.querySelector('.profile-photo h2').innerText,
content: $content.innerText,
html: convertHtmlToHtmlString($content)
.replace(
/<img\s+src="(https:\/\/parts\.lineblog\.me\/img\/emoji\/line\/\d+\/\d+\.png)">/g,
(match, p1) => {
return `<img src="${p1}" style="width:1.3em;height:1.3em;position:relative;top:0.2em;" alt="emoji">`;
}
)
.replace(
/(_(video|instagram|tweet|ogp)_[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12})/gi,
(match, key) => {
return mediaDic[key];
}
),
html: await convertHtmlToHtmlString($content),
url: $title.href
});
}

return ret;
}
};

static async fetch(entry) {
static fetch = async (entry) => {
const ret = [];
const _ids = entry.get('_ids');
const { size } = _ids;
Expand Down Expand Up @@ -162,11 +87,11 @@ export class LineBlog {
}

return ret;
}
};
}

export class Nogi {
static parse(body) {
static parse = async (body) => {
const $parsed = dparser.parseFromString(body, 'text/html');
const names = $parsed.querySelectorAll('.author');
const titles = $parsed.querySelectorAll('h1 .entrytitle');
Expand All @@ -185,61 +110,60 @@ export class Nogi {
date: dayjs(dates[i].childNodes[0].nodeValue.slice(0, -1)),
title: titles[i].innerText,
author: names[i].innerText.trim(),
html: convertHtmlToHtmlString($content),
html: await convertHtmlToHtmlString($content),
content: $content.innerText,
url: (urls[i] ? urls[i] : titles[i].querySelector('a')).href
});
}

return ret;
}
};

static async fetch(entry) {
static fetch = async (entry) => {
return _.map(
Nogi.parse(
await Nogi.parse(
await rp(`http://blog.nogizaka46.com/?p=${entry.get('page')}`)
),
(a) => new Article(a)
);
}
};
}

export class Keyaki {
static get BASE_URL() {
return 'http://www.keyakizaka46.com';
}
static BASE_URL = 'http://www.keyakizaka46.com';

static parse(body) {
return _.map(
dparser
.parseFromString(body, 'text/html')
.querySelectorAll('article'),
($article) => {
const { innerText: datestr } = $article.querySelector(
'.box-bottom li'
);
const $title = $article.querySelector('h3');
const { innerText: name } = $article.querySelector('.name');
const $content = $article.querySelector('.box-article');
static parse = async (body) => {
const ret = [];

return {
date: dayjs(datestr),
title: $title.innerText.trim(),
author: name.trim(),
html: convertHtmlToHtmlString($content),
content: $content.innerText,
url: liburl.resolve(
Keyaki.BASE_URL,
_.get($title.querySelector('a'), 'pathname') || ''
)
};
}
);
}
for (const $article of dparser
.parseFromString(body, 'text/html')
.querySelectorAll('article')) {
const { innerText: datestr } = $article.querySelector(
'.box-bottom li'
);
const $title = $article.querySelector('h3');
const { innerText: name } = $article.querySelector('.name');
const $content = $article.querySelector('.box-article');

static async fetch(entry) {
ret.push({
date: dayjs(datestr),
title: $title.innerText.trim(),
author: name.trim(),
html: await convertHtmlToHtmlString($content),
content: $content.innerText,
url: liburl.resolve(
Keyaki.BASE_URL,
_.get($title.querySelector('a'), 'pathname') || ''
)
});
}

return ret;
};

static fetch = async (entry) => {
return _.map(
Keyaki.parse(
await Keyaki.parse(
await rp(
liburl.resolve(
Keyaki.BASE_URL,
Expand All @@ -249,49 +173,45 @@ export class Keyaki {
),
(a) => new Article(a)
);
}
};
}

export class Hinata {
static get BASE_URL() {
return 'https://www.hinatazaka46.com';
}
static BASE_URL = 'https://www.hinatazaka46.com';

static parse(body) {
return _.map(
dparser
.parseFromString(body, 'text/html')
.querySelectorAll('.p-blog-article'),
($article) => {
const $content = $article.querySelector(
'.c-blog-article__text'
);
static parse = async (body) => {
const ret = [];

return {
date: dayjs(
$article.querySelector('.c-blog-article__date')
.innerText
),
title: $article
.querySelector('.c-blog-article__title')
.innerText.trim(),
author: $article
.querySelector('.c-blog-article__name')
.innerText.trim(),
html: convertHtmlToHtmlString($content),
content: $content.innerText,
url: liburl.resolve(
Hinata.BASE_URL,
$article.querySelector('.c-button-blog-detail').pathname
)
};
}
);
}
for (const $article of dparser
.parseFromString(body, 'text/html')
.querySelectorAll('.p-blog-article')) {
const $content = $article.querySelector('.c-blog-article__text');

ret.push({
date: dayjs(
$article.querySelector('.c-blog-article__date').innerText
),
title: $article
.querySelector('.c-blog-article__title')
.innerText.trim(),
author: $article
.querySelector('.c-blog-article__name')
.innerText.trim(),
html: await convertHtmlToHtmlString($content),
content: $content.innerText,
url: liburl.resolve(
Hinata.BASE_URL,
$article.querySelector('.c-button-blog-detail').pathname
)
});
}

return ret;
};

static async fetch(entry) {
static fetch = async (entry) => {
return _.map(
Hinata.parse(
await Hinata.parse(
await rp(
liburl.resolve(
Hinata.BASE_URL,
Expand All @@ -303,5 +223,5 @@ export class Hinata {
),
(a) => new Article(a)
);
}
};
}
Loading

0 comments on commit 59cd718

Please sign in to comment.