|
| 1 | +# HowLongToBeat Python API |
| 2 | + |
| 3 | +[](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/actions/workflows/python-test.yml) |
| 4 | +[](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/actions/workflows/codeql-analysis.yml) |
| 5 | + |
| 6 | +[](https://codecov.io/gh/ScrappyCocco/HowLongToBeat-PythonAPI) |
| 7 | +[](https://sonarcloud.io/dashboard?id=ScrappyCocco_HowLongToBeat-PythonAPI) |
| 8 | + |
| 9 | +A simple Python API to read data from howlongtobeat.com. |
| 10 | + |
| 11 | +It is inspired by [ckatzorke - howlongtobeat](https://github.com/ckatzorke/howlongtobeat) JS API. |
| 12 | + |
| 13 | +## Content |
| 14 | + |
| 15 | +- [Usage](#usage) |
| 16 | +- [Installation](#installation) |
| 17 | + - [Installing the package downloading the last release](#installing-the-package-downloading-the-last-release) |
| 18 | + - [Installing the package from the source code](#installing-the-package-from-the-source-code) |
| 19 | +- [Usage in code](#usage-in-code) |
| 20 | + - [Start including it in your file](#start-including-it-in-your-file) |
| 21 | + - [Now call search()](#now-call-search) |
| 22 | + - [Alternative search (by ID)](#alternative-search-by-id) |
| 23 | + - [DLC search](#dlc-search) |
| 24 | + - [Results auto-filter](#results-auto-filter) |
| 25 | + - [Reading an entry](#reading-an-entry) |
| 26 | +- [Issues, Questions & Discussions](#issues-questions--discussions) |
| 27 | +- [Authors](#authors) |
| 28 | +- [License](#license) |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +## Installation |
| 33 | + |
| 34 | +### Installing the package downloading the last release |
| 35 | + |
| 36 | +```python |
| 37 | +pip install howlongtobeatpy |
| 38 | +``` |
| 39 | + |
| 40 | +### Installing the package from the source code |
| 41 | + |
| 42 | +Download the repo, enter the folder with 'setup.py' and run the command |
| 43 | + |
| 44 | +```python |
| 45 | +pip install . |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage in code |
| 49 | + |
| 50 | +### Start including it in your file |
| 51 | + |
| 52 | +```python |
| 53 | +from howlongtobeatpy import HowLongToBeat |
| 54 | +``` |
| 55 | + |
| 56 | +### Now call search() |
| 57 | + |
| 58 | +The API main functions are: |
| 59 | + |
| 60 | +```python |
| 61 | +results = HowLongToBeat().search("Awesome Game") |
| 62 | +``` |
| 63 | + |
| 64 | +or, if you prefer using async: |
| 65 | + |
| 66 | +```python |
| 67 | +results = await HowLongToBeat().async_search("Awesome Game") |
| 68 | +``` |
| 69 | + |
| 70 | +The return of that function is a **list** of possible games, or **None** in case you passed an invalid "game name" as parameter or if there was an error in the request. |
| 71 | + |
| 72 | +If the list **is not None** you should choose the best entry checking the Similarity value with the original name, example: |
| 73 | + |
| 74 | +```python |
| 75 | +results_list = await HowLongToBeat().async_search("Awesome Game") |
| 76 | +if results_list is not None and len(results_list) > 0: |
| 77 | + best_element = max(results_list, key=lambda element: element.similarity) |
| 78 | +``` |
| 79 | + |
| 80 | +Once done, "best_element" will contain the best game found in the research. |
| 81 | +Every entry in the list (if not None in case of errors) is an object of type: [HowLongToBeatEntry](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/blob/master/howlongtobeatpy/howlongtobeatpy/HowLongToBeatEntry.py). |
| 82 | + |
| 83 | +### Alternative search (by ID) |
| 84 | + |
| 85 | +If you prefer, you can get a game by ID, this can be useful if you already have the game's howlongtobeat-id (the ID is the number in the URL, for example in [https://howlongtobeat.com/game/7231]([hello](https://howlongtobeat.com/game/7231)) the ID is 7231). |
| 86 | + |
| 87 | +To avoid a new parser, the search by ID use a first request to get the game title, and then use the standard search with that title, filtering the results and returning the unique game with that ID. |
| 88 | + |
| 89 | +Remember that it could be a bit slower, but you avoid searching the game in the array by similarity. |
| 90 | + |
| 91 | +Here's the example: |
| 92 | + |
| 93 | +```python |
| 94 | +result = HowLongToBeat().search_from_id(123456) |
| 95 | +``` |
| 96 | + |
| 97 | +or, if you prefer using async: |
| 98 | + |
| 99 | +```python |
| 100 | +result = await HowLongToBeat().async_search_from_id(123456) |
| 101 | +``` |
| 102 | + |
| 103 | +This call will return an unique [HowLongToBeatEntry](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/blob/master/howlongtobeatpy/howlongtobeatpy/HowLongToBeatEntry.py) or None in case of errors. |
| 104 | + |
| 105 | +### DLC search |
| 106 | + |
| 107 | +An `enum` has been added to have a filter in the search: |
| 108 | + |
| 109 | +```python |
| 110 | +SearchModifiers.NONE # default |
| 111 | +SearchModifiers.ISOLATE_DLC |
| 112 | +SearchModifiers.HIDE_DLC |
| 113 | +``` |
| 114 | + |
| 115 | +This optional parameter allow you to specify in the search if you want the default search (with DLCs), to HIDE DLCs and only show games, or to ISOLATE DLCs (show only DLCs). |
| 116 | + |
| 117 | +### Results auto-filter |
| 118 | + |
| 119 | +To ignore games with a very different name, the standard search automatically filter results with a game name that has a similarity with the given name > than `0.4`, not adding the others to the result list. |
| 120 | +If you want all the results, or you want to change this value, you can put a parameter in the constructor: |
| 121 | + |
| 122 | +```python |
| 123 | +results = HowLongToBeat(0.0).search("Awesome Game") |
| 124 | +``` |
| 125 | + |
| 126 | +putting `0.0` (or just `0`) will return all the found games, otherwise you can write another (`float`) number between 0...1 to set a new filter, such as `0.7`. |
| 127 | + |
| 128 | +Also remember that by default the similarity check **is case-sensitive** between the name given and the name found, if you want to ignore the case you can use: |
| 129 | + |
| 130 | +```python |
| 131 | +results = HowLongToBeat(0.0).search("Awesome Game", similarity_case_sensitive=False) |
| 132 | +``` |
| 133 | + |
| 134 | +**Remember** that, when searching by ID, the similarity value and the case-sensitive bool are **ignored**. |
| 135 | + |
| 136 | +### Reading an entry |
| 137 | + |
| 138 | +An entry is made of few values, you can check them [in the Entry class file](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/blob/master/howlongtobeatpy/howlongtobeatpy/HowLongToBeatEntry.py). It also include the full JSON of values (already converted to Python dict) received from HLTB. |
| 139 | + |
| 140 | +## Issues, Questions & Discussions |
| 141 | + |
| 142 | +If you found a bug report it as soon as you can creating an [issue](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/issues/new), the code may not be perfect. |
| 143 | + |
| 144 | +If you need any new feature, or want to discuss the current implementation/features, consider opening a [discussion](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/discussions) or even propose a change with a [Pull Request](https://github.com/ScrappyCocco/HowLongToBeat-PythonAPI/pulls). |
| 145 | + |
| 146 | +## Authors |
| 147 | + |
| 148 | +* **ScrappyCocco** - Thank you for using my API |
| 149 | + |
| 150 | +## License |
| 151 | + |
| 152 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
0 commit comments