Skip to content
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

Merged
merged 12 commits into from
Nov 30, 2022
Merged

Created getUser() method #85

merged 12 commits into from
Nov 30, 2022

Conversation

StavrosNik4
Copy link
Contributor

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.

Copy link
Owner

@Kylart Kylart left a 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
Comment on lines 10 to 45
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++
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()
})
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's a good solution. Here are the results from my account:
Untitled

Copy link
Owner

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

Copy link
Contributor Author

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?

Copy link
Contributor Author

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
Comment on lines 103 to 108
// 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
Copy link
Owner

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.

Copy link
Contributor Author

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
Comment on lines 65 to 102
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
})
}
Copy link
Owner

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.

Copy link
Contributor Author

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.

@Kylart
Copy link
Owner

Kylart commented Nov 28, 2022

I forgot to mention this but can you change the result properties names to use camelCase instead of PascalCase? This way it's coherent with the prest of the package.

@StavrosNik4
Copy link
Contributor Author

StavrosNik4 commented Nov 29, 2022

I added a camelize method in d357f88, for some reason camelCase() didn't work.
@Kylart I think I fix everything, do you believe we are ready to proceed to the merging or should I provide more documentation as well?
I don't think I have that much time right now to add tests. I will add them once I have more time.

Copy link
Owner

@Kylart Kylart left a 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 😄

@Kylart Kylart merged commit f832492 into Kylart:dev Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants