-
Notifications
You must be signed in to change notification settings - Fork 1
Feat(script): Import book from amazon #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
kellnerd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job, I've been anticipating BB importer scripts ever since I first discovered the project, so thanks a lot for your work on the server's POST routes and the userscript 👍
I've just tested it on https://test.bookbrainz.org and noticed a few things which were not working or which could be improved:
- When you change the script's file extension to
.user.js, a userscript manager automatically offers to install the script in GitHub's raw file mode. - There should be a check in the code which aborts the import if the userscript is running on an unsupported Amazon page (not all products are books).
- Following that approach, the script should simply skip propertis for which it fails during extraction (missing DOM element) or parsing (unexpected format of the text).
- I am not sure whether the order of the dimensions (height, width and depth) is correct, probably there is not even a consistent order (see my example link below).
- The whole script fails as soon as the page's language is not English as the properties are named differently in other languages (e.g. I had to set the language to English on German Amazon to make it work).
- On MB it's best practice that importer scripts also seed the edit note with the URL of the data source and the name and version of the userscript (draws attention of other users and helps to understand potential systematic issues with a certain version of a script).
amazon-import.js
Outdated
| `, | ||
| "bookbrainz" | ||
| ); | ||
| GM_addStyle( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can combine all these stylesheet injections into a single call which injects all rules at once.
amazon-import.js
Outdated
| "bookbrainz" | ||
| ); | ||
| const convertToSI = { | ||
| cm: 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| cm: 1, | |
| cm: 10, // BB expects mm |
amazon-import.js
Outdated
| } | ||
| let [height, width, depth] = res.dimensions?.split("x"); | ||
| let lenghtToSIkey = depth.match(/[A-Za-z]+/gi)[0]; | ||
| let wtToSIkey = res.weight.match(/[A-Za-z]+/gi)[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let wtToSIkey = res.weight.match(/[A-Za-z]+/gi)[0]; | |
| let wtToSIkey = res.weight?.match(/[A-Za-z]+/gi)[0]; |
Prevents the script from failing if the weight is not available, but this should be fixed properly to skip all further processing of the missing field.
amazon-import.js
Outdated
| date = new Date(date); | ||
| date = date?.toISOString()?.split("T")[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| date = new Date(date); | |
| date = date?.toISOString()?.split("T")[0]; | |
| date = new Date(date.replace('.', '')); // temporary fix for unsupported dates like `20 Oct. 2021` | |
| date = [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-'); |
The second line is necessary to avoid dates which are off by one day for certain time zones.
But this is just a quick fix, the better solution would be to use a date parsing library as Date has a lot of caveats [1].
amazon-import.js
Outdated
| } | ||
| try { | ||
| // Setting up UI | ||
| const submitUrl = "https://bookbrainz.org/edition/create"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const submitUrl = "https://bookbrainz.org/edition/create"; | |
| const submitUrl = "https://test.bookbrainz.org/edition/create"; |
It would be helpful to have the link temporarily point to the test server until the changes have been released.
|
@kellnerd Thanks for reviewing the PR and giving the suggestions!
|
kellnerd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a check if author exist for a product only then run the import script.
Maybe I'm blind, but I don't see anything in the code which extracts or parses the author of the book at all... Are these local chnages which you haven't pushed to GitHub yet?
I also think submission note would be helpful when seeding, but currently BB PR doesn't support submission note seeding, i will update this PR as soon it does.
Great, I've already seen your commit for this feature in the BB PR, so let's wait for @MonkeyDo to review it.
amazon-import.user.js
Outdated
| <label class="bb-flabel" for="bb-date">Start date:</label> | ||
| <input class="bb-finput" name="editionSection.releaseDate" type="date" value=${ | ||
| itemDetails.date | ||
| } id="bb-date" name="" value="2018-07-22"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } id="bb-date" name="" value="2018-07-22"> | |
| } id="bb-date"> |
These duplicate attributes are probably left over from testing.
amazon-import.user.js
Outdated
| date = [ | ||
| date.getFullYear(), | ||
| ((date.getMonth() < 10) ? "0" : "") + `${date.getMonth() + 1}`, | ||
| date.getDate(), | ||
| ].join("-"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I forgot to pad the date (was not necessary for my example). The day also has to be padded, so I suggest to use the following (year is already long enough, so it will be left as is by the mapper function):
| date = [ | |
| date.getFullYear(), | |
| ((date.getMonth() < 10) ? "0" : "") + `${date.getMonth() + 1}`, | |
| date.getDate(), | |
| ].join("-"); | |
| date = [date.getFullYear(), date.getMonth() + 1, date.getDate()] | |
| .map((component) => String(component).padStart(2, '0')) | |
| .join('-'); |
amazon-import.user.js
Outdated
| g: 1, | ||
| pounds: 453.6, | ||
| ounces: 28.3, | ||
| inches: 2.5, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| inches: 2.5, | |
| inches: 25.4, // mm |
amazon-import.user.js
Outdated
| <textarea class="bb-finput" name="submissionSection" id="bb-format">${submissionNote}</textarea> | ||
| <button class="bb-btn" type="submit">Submit</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| <textarea class="bb-finput" name="submissionSection" id="bb-format">${submissionNote}</textarea> | |
| <button class="bb-btn" type="submit">Submit</button> |
Has to be removed or commented out until the server-side has been fixed.
Allow user to import book directly from amazon.