Skip to content

Commit

Permalink
feat(proxy): Increase agent functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
fhyoga committed Oct 11, 2019
1 parent 6b198ed commit b3c69d6
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ cache:

before_install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export CXX="g++-8"; fi
- rm -rf yarn.lock
- rm -rf package-lock.json

install:
- yarn --ignore-engines
Expand Down
6 changes: 5 additions & 1 deletion app/config-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import Store from 'electron-store';
const store = new Store({
defaults: {
scene: ['movie', 'normal'],
tags: ['无']
tags: ['无'],
proxy: {
enable: false,
url: 'http://127.0.0.1:1087'
}
}
});

Expand Down
1 change: 0 additions & 1 deletion app/containers/Home/ScrapeInfoModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const ScrapeModal = ({ visible, taskQueue, onCancel, handleTaskEnd }) => {
str: task.file.key === key ? str : task.str ? task.str : ''
}));
setTaskQ(_taskQ);
console.log(_taskQ, 1);
lastTaskQ.current = _taskQ;
});
emitter.on(EventType.SCRAPE_SUCCESS, ({ key }, json) => {
Expand Down
42 changes: 31 additions & 11 deletions app/containers/Home/SettingModal/form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useImperativeHandle, forwardRef, useState } from 'react';
import { Select, Form, Cascader, Row, Col } from 'antd';
import React, { useImperativeHandle, forwardRef } from 'react';
import { Select, Form, Cascader, Row, Col, Switch, Input } from 'antd';
import config from '@config';
import styles from './index.less';
import { mediaType } from '@scraper';
Expand All @@ -8,22 +8,29 @@ const { Option } = Select;
const { Item } = Form;

const SettingForm = ({ form }, ref) => {
const tags = config.get('tags');
const scene = config.get('scene', ['movie', 'normal']);
const tagsConfig = config.get('tags');
const sceneConfig = config.get('scene', ['movie', 'normal']);
const proxyConfig = config.get('proxy');
// const sceneSourceMapping = config.get('sceneSourceMapping', {});
useImperativeHandle(ref, () => ({
submitForm: () =>
new Promise((resolve, reject) => {
form.validateFields((err, values) => {
if (err) return reject(err);
config.set(values);
console.log(config.path);
const { tags, scene, proxyUrl, proxyEnable } = values;
config.set({
tags,
scene,
proxy: {
enable: proxyEnable,
url: proxyUrl
}
});
resolve();
});
})
}));
// const [mediaSourceVisible, setMediaSourceVisible] = useState(false);
console.log(useState);

const { getFieldDecorator } = form;
return (
<Form
Expand All @@ -33,9 +40,9 @@ const SettingForm = ({ form }, ref) => {
className={styles.setting_form}
>
<Item label="预设标签">
{getFieldDecorator('tags', { initialValue: tags })(
{getFieldDecorator('tags', { initialValue: tagsConfig })(
<Select mode="tags" style={{ width: '100%' }} placeholder="Tags">
{tags.map(tag => (
{tagsConfig.map(tag => (
<Option key={tag}>{tag}</Option>
))}
</Select>
Expand All @@ -44,12 +51,25 @@ const SettingForm = ({ form }, ref) => {
<Item label="场景">
<Row>
<Col span={10}>
{getFieldDecorator('scene', { initialValue: scene })(
{getFieldDecorator('scene', { initialValue: sceneConfig })(
<Cascader options={mediaType} allowClear={false} />
)}
</Col>
</Row>
</Item>
<Item label="代理">
<Row>
<Col span={10}>
{getFieldDecorator('proxyEnable', {
initialValue: proxyConfig.enable,
valuePropName: 'checked'
})(<Switch />)}
{getFieldDecorator('proxyUrl', {
initialValue: proxyConfig.url
})(<Input />)}
</Col>
</Row>
</Item>
</Form>
);
};
Expand Down
21 changes: 16 additions & 5 deletions app/scraper/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { writeFile, mkdirp } from 'fs-extra';
// import javbus from '../javBus';
import { downloadImg, emitter } from '../../utils';
import { EventType, QueryOpt, ToolHead, FileNode } from '@types';
import config from '@config';

const saveAsserts = async (model, file) => {
const proxy = config.get('proxy');
const url = proxy.enable ? proxy.url : '';
const json = model.getModel();
await mkdirp(`${file.wpath}/.actors`);
return Promise.all([
writeFile(
`${file.wpath + file.title}.nfo`,
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>${model.getXML()}`
),
downloadImg(json.art.poster._text, `${file.wpath + file.title}-poster.jpg`),
downloadImg(json.art.fanart._text, `${file.wpath + file.title}-fanart.jpg`),
downloadImg(
json.art.poster._text,
`${file.wpath + file.title}-poster.jpg`,
{ proxy: url }
),
downloadImg(
json.art.fanart._text,
`${file.wpath + file.title}-fanart.jpg`,
{ proxy: url }
),
json.actor.map(v =>
downloadImg(v.thumb._text, `${file.wpath}.actors/${v.name._text}.jpg`)
downloadImg(v.thumb._text, `${file.wpath}.actors/${v.name._text}.jpg`, {
proxy: url
})
)
])
.then(() => model)
Expand Down Expand Up @@ -43,7 +55,6 @@ class Scraper {

async start(queryOpts: QueryOpt[], name: string): Promise<TaskResult> {
this.stopFlag = false;
console.log(name, queryOpts);
const failureTasks = [];
const successTasks = [];
const { head } = this.heads.find(t => t.name === name);
Expand Down
13 changes: 6 additions & 7 deletions app/scraper/heads/avsox.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import request from 'request-promise';
import cheerio from 'cheerio';
import MovieModel from '../core/model';
import { MediaKeys } from '@types';

export default {
export default request => ({
head: async (queryString: string): Promise<any> => {
const movieModel = new MovieModel();
const encodedQueryString = encodeURIComponent(queryString);
const searchPage = await request(
`https://avsox.asia/cn/search/${encodedQueryString}`
);
const searchPage = await request({
url: `https://avsox.asia/cn/search/${encodedQueryString}`
});
const infoPageUrl = cheerio
.load(searchPage)('.movie-box')
.attr('href')
.replace(/https:\/\//, 'http://');
const $ = cheerio.load(await request(infoPageUrl));
const $ = cheerio.load(await request({ url: infoPageUrl }));
movieModel.setModel({
title: {
_text: $('h3')
Expand Down Expand Up @@ -65,4 +64,4 @@ export default {
},
name: 'avsox',
type: [MediaKeys.Movie, MediaKeys.Gentleman]
};
});
3 changes: 2 additions & 1 deletion app/scraper/heads/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import r from '../request';
import { ToolHead } from '@types';
import tmdb from './tmdb';
import javbus from './javbus';
import avsox from './avsox';

const heads: ToolHead[] = [tmdb, javbus, avsox];
const heads: ToolHead[] = [tmdb(r), javbus(r), avsox(r)];
export default heads;
13 changes: 6 additions & 7 deletions app/scraper/heads/javbus.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import request from 'request-promise';
import cheerio from 'cheerio';
import MovieModel from '../core/model';
import { MediaKeys } from '@types';

export default {
export default request => ({
head: async (queryString: string): Promise<any> => {
const movieModel = new MovieModel();
const encodedQueryString = encodeURIComponent(queryString);
const searchPage = await request(
`http://www.javbus.com/uncensored/search/${encodedQueryString}`
);
const searchPage = await request({
url: `http://www.javbus.com/uncensored/search/${encodedQueryString}`
});
const infoPageUrl = cheerio
.load(searchPage)('.movie-box')
.attr('href')
.replace(/https:\/\//, 'http://');
const $ = cheerio.load(await request(infoPageUrl));
const $ = cheerio.load(await request({ url: infoPageUrl }));
movieModel.setModel({
title: {
_text: $('h3')
Expand Down Expand Up @@ -65,4 +64,4 @@ export default {
},
name: 'javbus',
type: [MediaKeys.Movie, MediaKeys.Gentleman]
};
});
5 changes: 2 additions & 3 deletions app/scraper/heads/tmdb.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import request from 'request-promise';
import MovieModel from '../core/model';
import { MovieModelType, MediaKeys } from '@types';

export default {
export default request => ({
head: async (queryString: string): Promise<MovieModelType> => {
const movieModel = new MovieModel();
const baseUrl = 'http://api.themoviedb.org/3';
Expand Down Expand Up @@ -77,4 +76,4 @@ export default {
},
name: 'TMDB',
type: [MediaKeys.Movie, MediaKeys.Normal]
};
});
8 changes: 8 additions & 0 deletions app/scraper/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import request from 'request-promise';
import config from '@config';

export default opts => {
const proxy = config.get('proxy');
const url = proxy.enable ? proxy.url : '';
return request({ ...opts, proxy: url });
};
4 changes: 2 additions & 2 deletions app/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export const sleep = time =>
}
});

export const downloadImg = (url, ipath) =>
export const downloadImg = (url, ipath, opt?) =>
new Promise((resolve, reject) => {
request(url)
request({ url, ...opt })
.pipe(fs.createWriteStream(ipath))
.on('finish', () => {
resolve(ipath);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "u-scraper",
"productName": "uScraper",
"version": "0.1.0-beta",
"version": "0.2.0-beta",
"description": "A scraper that switches between normal mode and gentleman mode, built on Eletron, React",
"scripts": {
"build": "concurrently \"yarn build-main\" \"yarn build-renderer\"",
Expand Down

0 comments on commit b3c69d6

Please sign in to comment.