Skip to content

Commit d9bff69

Browse files
committed
react-window, import cleanup, caching
1 parent 43adff8 commit d9bff69

File tree

7 files changed

+284
-221
lines changed

7 files changed

+284
-221
lines changed

database.sqlite

1.38 MB
Binary file not shown.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"react": "^16.6.3",
2929
"react-apollo": "^2.3.2",
3030
"react-dom": "^16.6.3",
31-
"react-virtualized": "9.9.0",
31+
"react-window": "^1.3.1",
32+
"react-window-infinite-loader": "^1.0.3",
3233
"sql-template-strings": "^2.2.2",
3334
"sqlite": "^3.0.0"
3435
},

pitchfork.js

+54-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Promise from 'bluebird';
88

99
/* eslint-disable no-console */
1010

11-
const maxPages = 2000;
11+
const MAX_PAGES = 2000;
12+
const MAX_SCORES_PER_PAGE = 25;
1213

1314
const parseReview = (db, $) => async (i, review) => {
1415
const $r = $(review);
@@ -28,51 +29,75 @@ const parseReview = (db, $) => async (i, review) => {
2829

2930
return db
3031
.run(
31-
SQL`INSERT OR REPLACE INTO albums (name, date, image, url) VALUES (${title}, ${date}, ${image}, ${url})`
32+
SQL`INSERT OR IGNORE INTO albums (name, date, image, url) VALUES (${title}, ${date}, ${image}, ${url})`
3233
)
3334
.then(async ({ stmt }) => {
34-
console.log('Inserted:', stmt.lastID);
35-
const albumId = stmt.lastID;
36-
37-
// console.log(title, artists);
35+
let albumId;
36+
if (stmt.changes === 0) {
37+
albumId = await db
38+
.get(SQL`SELECT album_id as id FROM albums WHERE name = ${title} AND url = ${url}`)
39+
.then(({ id }) => id);
40+
} else {
41+
albumId = stmt.lastID;
42+
}
3843

39-
await Promise.all(
44+
const artistIds = await Promise.all(
4045
artists.map(artist =>
4146
db
42-
.run(SQL`INSERT OR REPLACE INTO artists (name) VALUES (${artist})`)
43-
.then(({ stmt: st }) => st.lastID)
47+
.run(SQL`INSERT OR IGNORE INTO artists (name) VALUES (${artist})`)
48+
.then(({ stmt: st }) => {
49+
if (st.changes === 0) {
50+
return db
51+
.get(SQL`SELECT artist_id as id FROM artists WHERE name = ${artist}`)
52+
.then(({ id }) => id);
53+
}
54+
return st.lastID;
55+
})
4456
)
45-
).then(artistIds =>
46-
Promise.all(
47-
artistIds.map(artistId =>
48-
db.run(
49-
SQL`INSERT OR REPLACE INTO album_artists (album_id, artist_id) VALUES (${albumId}, ${artistId})`
57+
);
58+
59+
console.log(albumId, title, artists, artistIds);
60+
61+
await Promise.all(
62+
artistIds.map(artistId =>
63+
db
64+
.run(
65+
SQL`INSERT OR IGNORE INTO album_artists (album_id, artist_id) VALUES (${albumId}, ${artistId})`
5066
)
51-
)
67+
.then(({ stmt: st }) => st.lastID)
5268
)
5369
);
5470

55-
await Promise.all(
71+
const genreIds = await Promise.all(
5672
genres.map(genre =>
5773
db
58-
.run(SQL`INSERT OR REPLACE INTO genres (name) VALUES (${genre})`)
59-
.then(({ stmt: st }) => st.lastID)
74+
.run(SQL`INSERT OR IGNORE INTO genres (name) VALUES (${genre})`)
75+
.then(({ stmt: st }) => {
76+
if (st.changes === 0) {
77+
return db
78+
.get(SQL`SELECT genre_id as id FROM genres WHERE name = ${genre}`)
79+
.then(({ id }) => id);
80+
}
81+
return st.lastID;
82+
})
6083
)
61-
).then(genreIds =>
62-
Promise.all(
63-
genreIds.map(genreId =>
64-
db.run(
65-
SQL`INSERT OR REPLACE INTO album_genres (album_id, genre_id) VALUES (${albumId}, ${genreId})`
84+
);
85+
86+
await Promise.all(
87+
genreIds.map(genreId =>
88+
db
89+
.run(
90+
SQL`INSERT OR IGNORE INTO album_genres (album_id, genre_id) VALUES (${albumId}, ${genreId})`
6691
)
67-
)
92+
.then(({ stmt: st }) => st.lastID)
6893
)
6994
);
7095

7196
return Promise.resolve();
7297
});
7398
};
7499

75-
const request = (db, base, i = 1) => {
100+
const request = ({ db, base, maxPages, i = 1 }) => {
76101
const url = `${base}?page=${i}`;
77102

78103
console.log('Fetching:', url);
@@ -88,15 +113,15 @@ const request = (db, base, i = 1) => {
88113
reviews.each(iter);
89114

90115
if (i < maxPages) {
91-
return request(db, base, i + 1);
116+
return request({ db, base, maxPages, i: i + 1 });
92117
}
93118
return Promise.resolve();
94119
});
95120
};
96121

97122
const pageRows = async (db, rows) => {
98123
const queries = [];
99-
const promises = rows.splice(0, 25).map(row =>
124+
const promises = rows.splice(0, MAX_SCORES_PER_PAGE).map(row =>
100125
fetch(row.url)
101126
.then(r => r.text())
102127
.then(review => {
@@ -125,6 +150,7 @@ const pageRows = async (db, rows) => {
125150
let doRequest = true;
126151
let offset = 0;
127152
let limit = -1;
153+
const maxPages = argv.pages || MAX_PAGES;
128154

129155
if (argv.reviews) {
130156
base = 'https://pitchfork.com/reviews/albums/';
@@ -145,7 +171,7 @@ const pageRows = async (db, rows) => {
145171
}
146172

147173
if (doRequest) {
148-
await request(db, base);
174+
await request({ db, base, maxPages });
149175
}
150176

151177
const rows = await db.all(

src/components/App.js

-168
This file was deleted.

0 commit comments

Comments
 (0)