Skip to content

Commit 34598b0

Browse files
committed
update
1 parent 1469855 commit 34598b0

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

packages/crawler/observables.ts

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const nodeFetch = require('node-fetch');
33
const { githubUrl } = require('app-constant');
44
import {
55
of,
6+
defer,
67
from,
78
forkJoin,
89
tap,
@@ -31,64 +32,70 @@ getCategory$().subscribe(category$);
3132
export const getSiteData$ = () => forkJoin([category$, getSite$()]);
3233

3334
const getGithubPage$ = (subUrl: string) =>
34-
new Observable((subscriber) => {
35-
const httpOptions = {
36-
timeout: 5 * 1000,
37-
headers: {
38-
Cookie,
39-
'User-Agent':
40-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
41-
Host: 'github.com',
42-
},
43-
};
44-
nodeFetch(`${githubUrl}/${subUrl}`, httpOptions)
45-
.then((res) => res.text())
46-
.then((res) => {
47-
subscriber.next(res);
48-
subscriber.complete();
35+
defer(
36+
() =>
37+
new Observable((subscriber) => {
38+
const httpOptions = {
39+
timeout: 5 * 1000,
40+
headers: {
41+
Cookie,
42+
'User-Agent':
43+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
44+
Host: 'github.com',
45+
},
46+
};
47+
nodeFetch(`${githubUrl}/${subUrl}`, httpOptions)
48+
.then((res) => res.text())
49+
.then((res) => {
50+
subscriber.next(res);
51+
subscriber.complete();
52+
})
53+
.catch((err) => {
54+
subscriber.error(err);
55+
});
4956
})
50-
.catch((err) => {
51-
subscriber.error(err);
52-
});
53-
});
57+
);
5458

5559
const getGithubList$ = () =>
56-
getGithub$().pipe(
57-
switchMap((x: any) => {
58-
const bar = new ProgressBar('complete :gitIndex of :total: :gtnm', {
59-
total: x.length,
60-
});
61-
return from<Observable<GitSchema>>(x).pipe(
62-
map((v: GitSchema, k: number) => [v, k]),
63-
concatMap(([v, gitIndex]: any[]) =>
64-
getGithubPage$(v.github).pipe(
65-
tap(() => {
66-
bar.tick({ gitIndex: gitIndex + 1, gtnm: v.github });
67-
}),
68-
retry(retryAttempt),
69-
map((v) => parseExtractGithub(v as string)),
70-
catchError(
71-
(): Observable<GitParseResult> =>
72-
of({
73-
star: -1,
74-
lastUpdate: '',
75-
})
76-
),
77-
map(
78-
(parseRes: GitParseResult): GitSchema => mergeResult(v, parseRes)
79-
),
80-
tap(({ star, github }) => {
81-
if (star < 0) {
82-
// eslint-disable-next-line no-console
83-
console.log(`\n - failed on ${github}`);
84-
}
85-
}),
86-
delay(crawlerStepDelay)
87-
)
88-
),
89-
toArray()
90-
);
91-
})
60+
defer(() =>
61+
getGithub$().pipe(
62+
switchMap((x: any) => {
63+
const bar = new ProgressBar('complete :gitIndex of :total: :gtnm', {
64+
total: x.length,
65+
});
66+
return from<Observable<GitSchema>>(x).pipe(
67+
map((v: GitSchema, k: number) => [v, k]),
68+
concatMap(([v, gitIndex]: any[]) =>
69+
getGithubPage$(v.github).pipe(
70+
tap(() => {
71+
bar.tick({ gitIndex: gitIndex + 1, gtnm: v.github });
72+
}),
73+
retry(retryAttempt),
74+
map((v) => parseExtractGithub(v as string)),
75+
catchError(
76+
(): Observable<GitParseResult> =>
77+
of({
78+
star: -1,
79+
lastUpdate: '',
80+
})
81+
),
82+
map(
83+
(parseRes: GitParseResult): GitSchema =>
84+
mergeResult(v, parseRes)
85+
),
86+
tap(({ star, github }) => {
87+
if (star < 0) {
88+
// eslint-disable-next-line no-console
89+
console.log(`\n - failed on ${github}`);
90+
}
91+
}),
92+
delay(crawlerStepDelay)
93+
)
94+
),
95+
toArray()
96+
);
97+
})
98+
)
9299
);
93100

94101
export const getGithubData$ = () => forkJoin([category$, getGithubList$()]);

0 commit comments

Comments
 (0)