-
Notifications
You must be signed in to change notification settings - Fork 49
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
Created getUser() method #85
Conversation
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.
Thank you for this! I tested it and it works fine!
I added some changes, some of them can be committed as is if you like them, otherwise we can discuss them.
Also, please add some tests for your feature in the test
folder.
src/users.js
Outdated
const pfp = $('#content .user-image img') // getting the profile picture page section | ||
const status1 = $('#content .user-profile .user-status-title') // getting the status titles page section | ||
const status2 = $('#content .user-profile .user-status-data') // getting the status data page section | ||
const result = [] // we will put here all the properties of the final object | ||
// pushing some basic properties and values | ||
result.push({ Username: name }) | ||
result.push({ ProfilePictureLink: $(pfp).attr('data-src').trim() }) | ||
result.push({ LastOnline: $(status2[0]).text() }) | ||
// loop for the status | ||
let i = 1 | ||
const arrayLength = status1.length | ||
while (i < arrayLength - 1) { | ||
const val = $(status1[i]).text() | ||
switch (val) { | ||
case 'Gender': | ||
result.push({ | ||
Gender: $(status2[i]).text() | ||
}) | ||
break | ||
case 'Birthday': | ||
result.push({ | ||
Birthday: $(status2[i]).text() | ||
}) | ||
break | ||
case 'Location': | ||
result.push({ | ||
Location: $(status2[i]).text() | ||
}) | ||
break | ||
case 'Joined': | ||
result.push({ | ||
Joined: $(status2[i]).text() | ||
}) | ||
} | ||
i++ | ||
} |
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 pfp = $('#content .user-image img') // getting the profile picture page section | |
const status1 = $('#content .user-profile .user-status-title') // getting the status titles page section | |
const status2 = $('#content .user-profile .user-status-data') // getting the status data page section | |
const result = [] // we will put here all the properties of the final object | |
// pushing some basic properties and values | |
result.push({ Username: name }) | |
result.push({ ProfilePictureLink: $(pfp).attr('data-src').trim() }) | |
result.push({ LastOnline: $(status2[0]).text() }) | |
// loop for the status | |
let i = 1 | |
const arrayLength = status1.length | |
while (i < arrayLength - 1) { | |
const val = $(status1[i]).text() | |
switch (val) { | |
case 'Gender': | |
result.push({ | |
Gender: $(status2[i]).text() | |
}) | |
break | |
case 'Birthday': | |
result.push({ | |
Birthday: $(status2[i]).text() | |
}) | |
break | |
case 'Location': | |
result.push({ | |
Location: $(status2[i]).text() | |
}) | |
break | |
case 'Joined': | |
result.push({ | |
Joined: $(status2[i]).text() | |
}) | |
} | |
i++ | |
} | |
const pfp = $('#content .user-image img') // getting the profile picture page section | |
const statusTitles = $('#content .user-profile .user-status-title') // getting the status titles page section | |
const statusData = $('#content .user-profile .user-status-data') // getting the status data page section | |
const result = [] // we will put here all the properties of the final object | |
// pushing some basic properties and values | |
result.push({ Username: name }) | |
result.push({ ProfilePictureLink: $(pfp).attr('data-src').trim() }) | |
result.push({ LastOnline: $(statusData[0]).text() }) | |
statusTitles.each(function (index, status) { | |
result.push({ | |
[$(status).text()]: $(statusData[index]).text() | |
}) | |
}) |
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 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.
Well then, just add a filter on $(status).text()
to have only the ones you want
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.
Is there a different way in JS to do that or do I need to add just an if statement?
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.
Nevermind, just added it on d357f88
src/users.js
Outdated
// putting all the properties and values to one object | ||
const finalObj = {} | ||
for (let i = 0; i < result.length; i++) { | ||
Object.assign(finalObj, result[i]) | ||
} | ||
return finalObj |
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.
Just make result
an object from the start et add properties instead of pushing them.
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.
That was a good point. It made the method even faster as well.
src/users.js
Outdated
const fav = $('#anime_favorites .fs10') | ||
if ($(fav).text() !== '') { // check if there are no favorites | ||
const favAnime = [] | ||
fav.each(function () { | ||
favAnime.push($(this).text()) | ||
}) | ||
result.push({ FavoriteAnime: favAnime }) | ||
} | ||
const fav2 = $('#manga_favorites .fs10') | ||
if ($(fav2).text() !== '') { // check if there are no favorites | ||
const favManga = [] | ||
fav2.each(function () { | ||
favManga.push($(this).text()) | ||
}) | ||
result.push({ | ||
FavoriteManga: favManga | ||
}) | ||
} | ||
const fav3 = $('#character_favorites .fs10') | ||
if ($(fav3).text() !== '') { // check if there are no favorites | ||
const favChar = [] | ||
fav3.each(function () { | ||
favChar.push($(this).text()) | ||
}) | ||
result.push({ | ||
FavoriteCharacters: favChar | ||
}) | ||
} | ||
const fav4 = $('.favmore .fs10') | ||
if ($(fav4).text() !== '') { // check if there are no favorites | ||
const favPeople = [] | ||
fav4.each(function () { | ||
favPeople.push($(this).text()) | ||
}) | ||
result.push({ | ||
FavoritePeople: favPeople | ||
}) | ||
} |
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.
This is kinda annoying to read.
Please rename fav
, fav1
etc.. to relevant names like fav
-> favAnimes
, fav2 ->
favManga` or something like that.
Also, it would be great to have each fav in its own method.
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.
added an addFavorites method on 01d4c6d
I hope it makes it way easier to read the code.
I forgot to mention this but can you change the |
I added a camelize method in d357f88, for some reason camelCase() didn't work. |
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.
Thank you very much for this contributions! I'll ship it with the next release whenever I have some time.
Would be great if you could add tests in another PR later 😄
I made a getUser method to scrap information, stats etc about a user.
It was also requested in #63
I didn't make a data user model because I'm not familiar with TypeScript, I hope someone else can do it.
My method still works.