Created
April 18, 2017 03:28
-
-
Save blaulan/50d462696bce5da9225b8d596a8c6fb4 to your computer and use it in GitHub Desktop.
remove trakt.tv duplicate history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# @Author: Yue Wu <me@blaulan.com> | |
# @Date: 2017-04-01 19:32:32 | |
# @Last Modified by: Yue Wu | |
# @Last Modified time: 2017-04-17 23:24:13 | |
import json | |
import requests | |
client_id = 'YOUR CLIENT ID' | |
oauth_token = 'YOUR OAUTH TOKEN' | |
headers = { | |
'Content-Type': 'application/json', | |
'trakt-api-version': '2', | |
'trakt-api-key': client_id, | |
'Authorization': 'Bearer ' + oauth_token | |
} | |
params = { | |
'user': 'blaulan', | |
'list': 'history', | |
'type': 'movies', | |
'page': 1, | |
'limit': 1000 | |
} | |
def get_history(params): | |
url = 'https://api.trakt.tv/users/{user}/{list}/{type}?page={page}&limit={limit}' | |
results = [] | |
while True: | |
resp = requests.get(url.format(**params), headers=headers) | |
if resp.status_code != 200: | |
print(resp.text) | |
continue | |
results += resp.json() | |
if int(resp.headers['X-Pagination-Page-Count']) != params['page']: | |
params['page'] += 1 | |
else: | |
break | |
return results | |
def remove_duplicate(params): | |
history = get_history(params) | |
episodes = [] | |
duplicates = [] | |
for i in history[::-1]: | |
if i['episode']['ids']['trakt'] in episodes: | |
duplicates.append(i['id']) | |
else: | |
episodes.append(i['episode']['ids']['trakt']) | |
result = requests.post( | |
'https://api.trakt.tv/sync/history/remove'.format(**params), | |
headers=headers, json={'ids': duplicates} | |
) | |
return result.text | |
if __name__ == '__main__': | |
params['type'] = 'episodes' | |
with open('{type}.json'.format(**params), 'w') as output: | |
json.dump(get_history(params), output, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this
and https://github.com/anthofo/trakt-tv-duplicates-removal