1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Anime List Client |
4
|
|
|
* |
5
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists |
6
|
|
|
* |
7
|
|
|
* PHP version 7 |
8
|
|
|
* |
9
|
|
|
* @package AnimeListClient |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
13
|
|
|
* @version 4.0 |
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\Command; |
18
|
|
|
|
19
|
|
|
use Amp\Artax; |
20
|
|
|
use Aviat\AnimeClient\API\Kitsu; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Clears the API Cache |
24
|
|
|
*/ |
25
|
|
|
class SyncKitsuWithMal extends BaseCommand { |
26
|
|
|
|
27
|
|
|
protected $kitsuModel; |
28
|
|
|
protected $malModel; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Run the image conversion script |
32
|
|
|
* |
33
|
|
|
* @param array $args |
34
|
|
|
* @param array $options |
35
|
|
|
* @return void |
36
|
|
|
* @throws \ConsoleKit\ConsoleException |
37
|
|
|
*/ |
38
|
|
|
public function execute(array $args, array $options = []) |
39
|
|
|
{ |
40
|
|
|
$this->setContainer($this->setupContainer()); |
41
|
|
|
$this->setCache($this->container->get('cache')); |
42
|
|
|
$this->kitsuModel = $this->container->get('kitsu-model'); |
43
|
|
|
$this->malModel = $this->container->get('mal-model'); |
44
|
|
|
|
45
|
|
|
//$kitsuCount = $this->getKitsuAnimeListPageCount(); |
|
|
|
|
46
|
|
|
//$this->echoBox("List item count: {$kitsuCount}"); |
|
|
|
|
47
|
|
|
$this->MALItemCreate(); |
48
|
|
|
|
49
|
|
|
//echo json_encode($this->getMALList(), \JSON_PRETTY_PRINT); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function getMALList() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
return $this->malModel->getFullList(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getKitsuAnimeListPageCount() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$cacheItem = $this->cache->getItem(Kitsu::AUTH_TOKEN_CACHE_KEY); |
61
|
|
|
|
62
|
|
|
$query = http_build_query([ |
63
|
|
|
'filter' => [ |
64
|
|
|
'user_id' => $this->kitsuModel->getUserIdByUsername(), |
65
|
|
|
'media_type' => 'Anime' |
66
|
|
|
], |
67
|
|
|
// 'include' => 'anime,anime.genres,anime.mappings,anime.streamingLinks', |
|
|
|
|
68
|
|
|
'page' => [ |
69
|
|
|
'limit' => 1 |
70
|
|
|
], |
71
|
|
|
'sort' => '-updated_at' |
72
|
|
|
]); |
73
|
|
|
$request = (new Artax\Request) |
74
|
|
|
->setUri("https://kitsu.io/api/edge/library-entries?{$query}") |
75
|
|
|
->setProtocol('1.1') |
76
|
|
|
->setAllHeaders([ |
77
|
|
|
'Accept' => 'application/vnd.api+json', |
78
|
|
|
'Content-Type' => 'application/vnd.api+json', |
79
|
|
|
'User-Agent' => "Tim's Anime Client/4.0" |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
if ($cacheItem->isHit()) |
83
|
|
|
{ |
84
|
|
|
$token = $cacheItem->get(); |
85
|
|
|
$request->setHeader('Authorization', "bearer {$token}"); |
86
|
|
|
} |
87
|
|
|
else |
88
|
|
|
{ |
89
|
|
|
$this->echoBox("WARNING: NOT LOGGED IN\nSome data might be missing"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$response = \Amp\wait((new Artax\Client)->request($request)); |
93
|
|
|
|
94
|
|
|
$body = json_decode($response->getBody(), TRUE); |
95
|
|
|
return $body['meta']['count']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function MALItemCreate() |
99
|
|
|
{ |
100
|
|
|
$input = json_decode('{ |
|
|
|
|
101
|
|
|
"watching_status": "current", |
102
|
|
|
"user_rating": "", |
103
|
|
|
"episodes_watched": "4", |
104
|
|
|
"rewatched": "0", |
105
|
|
|
"notes": "", |
106
|
|
|
"id": "15794526", |
107
|
|
|
"mal_id": "33731", |
108
|
|
|
"edit": "true" |
109
|
|
|
}', TRUE); |
110
|
|
|
|
111
|
|
|
$response = $this->malModel->createListItem([ |
|
|
|
|
112
|
|
|
'id' => 12255, |
113
|
|
|
'status' => 'planned', |
114
|
|
|
'type' => 'anime' |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
//$response = $this->malModel->updateListItem($input); |
|
|
|
|
118
|
|
|
//print_r($response); |
119
|
|
|
//echo $response->getBody(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function diffLists() |
124
|
|
|
{ |
125
|
|
|
// Get libraryEntries with media.mappings from Kitsu |
126
|
|
|
// Organize mappings, and ignore entries without mappings |
127
|
|
|
|
128
|
|
|
// Get MAL list data |
129
|
|
|
|
130
|
|
|
// Compare each list entry |
131
|
|
|
// If a list item exists only on MAL, create it on Kitsu with the existing data from MAL |
132
|
|
|
// If a list item exists only on Kitsu, create it on MAL with the existing data from Kitsu |
133
|
|
|
// If an item already exists on both APIS: |
134
|
|
|
// Compare last updated dates, and use the later one |
135
|
|
|
// Otherwise, use rewatch count, then episode progress as critera for selecting the more up |
136
|
|
|
// to date entry |
137
|
|
|
// Based on the 'newer' entry, update the other api list item |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.