abhinavk99 / jikanpy

Python wrapper for the Jikan API
MIT License
216 stars 29 forks source link

[Asking] How can i loop with dict when using jikanpy? #93

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hello . I was trying to pass though a loop of dict to get all title from result query... This is my latest code All i want to get is all title, url.

from jikanpy import Jikan
import urllib
import json
jikan = Jikan()
print("Input anime you want: ")
search = input()
result = urllib.parse.quote(search)
search_result = jikan.search('anime', result,parameters={'limit': 4})
# print(search_result)
# search foreach
for dict_item in search_result:  
    print(search_result[dict_item])

print(search_result[dict_item])

Ace
request:search:5ac5be203ed230c8033ab02d7c6298b43027a078
False
432000
[{'mal_id': 18689, 'url': 'https://myanimelist.net/anime/18689/Diamond_no_Ace', 'image_url': 'https://cdn.myanimelist.net/images/anime/5/54235.jpg?s=6274728d6d1b274365c964ff74619d21', 'title': 'Diamond no Ace', 'airing': False, 'synopsis': 'With a stray pitch that completely missed the batter, Eijun Sawamura loses his final middle school baseball game. Frustrated by this defeat, Eijun and his teammates vow to reach the national tournamen...', 'type': 'TV', 'episodes': 75, 'score': 8.11, 'start_date': '2013-10-06T00:00:00+00:00', 'end_date': '2015-03-29T00:00:00+00:00', 'members': 188213, 'rated': 'PG-13'}, {'mal_id': 30230, 'url': 'https://myanimelist.net/anime/30230/Diamond_no_Ace__Second_Season', 'image_url': 'https://cdn.myanimelist.net/images/anime/9/74398.jpg?s=3a65df0ba1e32d5454a294bfbd29fb0e', 'title': 'Diamond no Ace: Second Season', 'airing': False, 'synopsis': 'After the National Tournament, the Seidou High baseball team moves forward with uncertainty as the Fall season quickly approaches. In an attempt to build a stronger team centered around their new capt...', 'type': 'TV', 'episodes': 51, 'score': 8.31, 'start_date': '2015-04-06T00:00:00+00:00', 'end_date': '2016-03-28T00:00:00+00:00', 'members': 104843, 'rated': 'PG-13'}, {'mal_id': 38731, 'url': 'https://myanimelist.net/anime/38731/Diamond_no_Ace__Act_II', 'image_url': 'https://cdn.myanimelist.net/images/anime/1153/100511.jpg?s=c363ec119a3569f7a9dadbd252f792c2', 'title': 'Diamond no Ace: Act II', 'airing': False, 'synopsis': 'The hallowed ground of Koshien Stadium is the "field of dreams," where the ambitions of high school baseball players come true. After emerging victorious in the autumn tournament last year, Seidou Hig...', 'type': 'TV', 'episodes': 52, 'score': 8.23, 'start_date': '2019-04-02T00:00:00+00:00', 'end_date': '2020-03-31T00:00:00+00:00', 'members': 58455, 'rated': 'PG-13'}, {'mal_id': 8542, 'url': 'https://myanimelist.net/anime/8542/Shin_Ace_wo_Nerae', 'image_url': 'https://cdn.myanimelist.net/images/anime/13/23161.jpg?s=79065b6280bff0af3d61a6f52d5ca9bc', 'title': 'Shin Ace wo Nerae!', 'airing': False, 'synopsis': 'Heroine, Hiromi Oka, has just entered a high school famous for its tennis club. She has started playing tennis yearning after Madam Butterfly. She was expecting to enjoy playing tennis with her best f...', 'type': 'TV', 'episodes': 25, 'score': 6.48, 'start_date': '1978-10-14T00:00:00+00:00', 'end_date': '1979-03-31T00:00:00+00:00', 'members': 1748, 'rated': 'PG-13'}]
8
https://api.jikan.moe/v3/search/anime?q=Ace&limit=4
{'Server': 'nginx/1.18.0 (Ubuntu)', 'Date': 'Sun, 18 Apr 2021 16:22:56 GMT', 'Content-Type': 'application/json', 'Content-Length': '1103', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': '*', 'Cache-Control': 'private, must-revalidate', 'ETag': '"90975ff85142fc7c7b0274bc1c446249"', 'X-Request-Hash': 'request:search:5ac5be203ed230c8033ab02d7c6298b43027a078', 'X-Request-Cached': '', 'X-Request-Cache-Ttl': '432000', 'Expires': 'Fri, 23 Apr 2021 16:22:56 GMT', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'X-Cache-Status': 'MISS'}
seanbreckenridge commented 3 years ago

Loop through the 'results' key of the search result

from jikanpy import Jikan
import urllib
import json
jikan = Jikan()
print("Input anime you want: ", end="")
search = input()
result = urllib.parse.quote(search)
search_result = jikan.search('anime', result,parameters={'limit': 4})
for dict_item in search_result["results"]:
    print(dict_item["title"], dict_item["url"])
python3 test.py
Input anime you want: takagi
Karakai Jouzu no Takagi-san https://myanimelist.net/anime/35860/Karakai_Jouzu_no_Takagi-san
Karakai Jouzu no Takagi-san 2 https://myanimelist.net/anime/38993/Karakai_Jouzu_no_Takagi-san_2
Karakai Jouzu no Takagi-san: Water Slide https://myanimelist.net/anime/37621/Karakai_Jouzu_no_Takagi-san__Water_Slide
Touhai Densetsu Akagi: Yami ni Maiorita Tensai https://myanimelist.net/anime/658/Touhai_Densetsu_Akagi__Yami_ni_Maiorita_Tensai

Closing this, feel free to re-open if this doesnt solve your problem

ghost commented 3 years ago

Hey there. First of all. Thanks for your help. It working great. Now i have another problem. Let said i want to sort order of series. Example: This is log when i search Detective Conan series

Input anime you want: Conan
Mirai Shounen Conan https://myanimelist.net/anime/302/Mirai_Shounen_Conan
Detective Conan OVA 01: Conan vs. Kid vs. Yaiba https://myanimelist.net/anime/1369/Detective_Conan_OVA_01__Conan_vs_Kid_vs_Yaiba
Detective Conan https://myanimelist.net/anime/235/Detective_Conan
Detective Conan Movie 19: The Hellfire Sunflowers https://myanimelist.net/anime/28479/Detective_Conan_Movie_19__The_Hellfire_Sunflowers
Mirai Shounen Conan (Movie) https://myanimelist.net/anime/7308/Mirai_Shounen_Conan_Movie
Detective Conan Movie 13: The Raven Chaser https://myanimelist.net/anime/5460/Detective_Conan_Movie_13__The_Raven_Chaser
Detective Conan Movie 22: Zero the Enforcer https://myanimelist.net/anime/35798/Detective_Conan_Movie_22__Zero_the_Enforcer
Mirai Shounen Conan 2: Taiga Daibouken https://myanimelist.net/anime/2613/Mirai_Shounen_Conan_2__Taiga_Daibouken
Detective Conan OVA 04: Conan and Kid and Crystal Mother https://myanimelist.net/anime/2514/Detective_Conan_OVA_04__Conan_and_Kid_and_Crystal_Mother
Lupin III vs. Detective Conan https://myanimelist.net/anime/6115/Lupin_III_vs_Detective_Conan
Detective Conan OVA 03: Conan and Heiji and the Vanished Boy https://myanimelist.net/anime/2513/Detective_Conan_OVA_03__Conan_and_Heiji_and_the_Vanished_Boy
Detective Conan: Conan vs. Kid - Shark & Jewel https://myanimelist.net/anime/9785/Detective_Conan__Conan_vs_Kid_-_Shark___Jewel
Detective Conan vs. Wooo https://myanimelist.net/anime/10766/Detective_Conan_vs_Wooo
Detective Conan Movie 14: The Lost Ship in the Sky https://myanimelist.net/anime/6467/Detective_Conan_Movie_14__The_Lost_Ship_in_the_Sky
Detective Conan: Conan vs. Kid - Jet Black Sniper https://myanimelist.net/anime/9786/Detective_Conan__Conan_vs_Kid_-_Jet_Black_Sniper
Detective Conan OVA 10: Kid in Trap Island https://myanimelist.net/anime/8609/Detective_Conan_OVA_10__Kid_in_Trap_Island
Lupin III vs. Detective Conan: The Movie https://myanimelist.net/anime/18429/Lupin_III_vs_Detective_Conan__The_Movie
Detective Conan OVA 02: 16 Suspects https://myanimelist.net/anime/2512/Detective_Conan_OVA_02__16_Suspects
Detective Conan Movie 01: The Timed Skyscraper https://myanimelist.net/anime/779/Detective_Conan_Movie_01__The_Timed_Skyscraper
Detective Conan Movie 05: Countdown to Heaven https://myanimelist.net/anime/1364/Detective_Conan_Movie_05__Countdown_to_Heaven

You can see, there are no order. It possible to sort it from oldest to latest? Like Movie - Detective Conan Movie 01 -> Detective Conan Movie 02 - > Detective Conan Movie 03 and so on?

seanbreckenridge commented 3 years ago

As far as I know the order of relations on myanimelist is arbitrary -- sometimes they're ordered in the 'correct' release order, sometimes in chronological

If you wanted to order it in release order, the only way to be sure would be to request each entry in the relation and grab its original release/air date, and then order it manually -- would be lots of requests but I dont think theres a way to be sure otherwise

ghost commented 3 years ago

Oh ok thanks for help me understand it ! kudo author. Have a good day ❤️ !