def update_history(): # result = tvshow_manager.to_watch() # print result trakt_manager = Trakt() tvshow_manager = TVShowTime() print 'Fetching Trakt.tv watched history...' history = trakt_manager.get_history() checkin_list = [] list_shows = {} for ep in history: show = { "show_id": ep['show']['ids']['tvdb'], "season": ep['episode']['season'], "episode": ep['episode']['number'] } # filter by greater ep and season filter_list = filter( lambda x: x['season'] >= show['season'] and x['show_id'] == show[ 'show_id'] and x['episode'] > show['episode'], checkin_list) if len(filter_list) == 0: checkin_list.append(show) print 'Saving progress on TVShowTime' print tvshow_manager.save_progress(checkin_list)
def test_refresh_token_off(): credentials = TraktCredentials("access", "refresh", "scope", 100) client = Trakt("", "", http_component=TOKEN_REFRESH_HTTP, user=credentials) client.countries.get_countries(type="shows") assert client.user.refresh_token == "refresh" assert client.user.access_token == "access"
def test_bad_request_exception(): client = Trakt("", "") http = DefaultHttpComponent( client, requests_dependency=MockRequests({".*": [{}, 400]}) ) with pytest.raises(BadRequest): http.request("...")
def test_get_quargs(): client = Trakt("", "") p = Path("a", {}, filters={"query", "genres"}, extended=["metadata"]) assert p.is_valid(client, extended=True, query="xyz", genres=["a", "b"]) _, quargs = p.get_path_and_qargs() expected = {"genres": "a,b", "query": "xyz", "extended": "metadata"} assert quargs == expected
def handler(event, context): parser = ConfigParser.ConfigParser() parser.read('config.ini') trakt_manager = Trakt() if not trakt_manager.is_token_valid(): print 'Your access has been revoked or invalidated, please authenticate again.' else: update_history() collect_new_followed()
def mk_mock_client(endpoints, client_id="", client_secret="", user=False, paginated=None, **config): return Trakt( client_id, client_secret, http_component=get_mock_http_component(endpoints, paginated=paginated), user=USER if user is False else None, **config, )
def test_filters(): client = Trakt("", "") p = Path("a", {}) with pytest.raises(ArgumentError): p.is_valid(client, genres="genre") p = Path("a", {}, filters={"query", "genres"}) assert p.is_valid(client, query="xyz") with pytest.raises(ArgumentError): p.is_valid(client, query=["xyz", "abc"]) assert p.is_valid(client, genres="genre") assert p.is_valid(client, genres=["abc", "xyz"]) with pytest.raises(ArgumentError): p.is_valid(client, query=[100, "abc"])
def test_optional_args(): client = Trakt("", "") p = Path("calendars/all/shows/new/?start_date/?days", [{"a": str}]) assert p.methods == ["GET"] assert p.args == ["?start_date", "?days"] default_alias = "calendars.all.shows.new" assert p.aliases == [default_alias] assert p.does_match(default_alias) assert not p.does_match(default_alias[1:]) assert p.is_valid(client) assert p.get_path_and_qargs() == ("calendars/all/shows/new", {}) assert p.is_valid(client, start_date="2018-10-10") assert p.get_path_and_qargs() == ("calendars/all/shows/new/2018-10-10", {})
def collect_new_followed(): print 'Collection new followed series to trakt.tv' trakt_manager = Trakt() tvshow_manager = TVShowTime() result = tvshow_manager.to_watch() watchlist = [] for s in result['episodes']: print 'Processing ' + s['show']['name'] + '...' show = {"title": s['show']['name'], "ids": {"tvdb": s['show']['id']}} if s['season_number'] == 1 and s['number'] == 1: watchlist.append(show) obj = {"shows": watchlist} trakt_manager.add_to_watchlist(obj) print 'Collected'
def test_extended(): client = Trakt("", "") p = Path("a", {}, extended=["full"]) assert p.is_valid(client) assert p.is_valid(client, extended="full") assert p.is_valid(client, extended=True) with pytest.raises(ArgumentError): p.is_valid(client, extended="meta") p.is_valid(client, extended=True) _, quargs = p.get_path_and_qargs() assert "extended" in quargs and quargs["extended"] == "full" p = Path("a", {}) p.is_valid(client) _, quargs = p.get_path_and_qargs() assert "extended" not in quargs
def test_extra_info_return(): client = Trakt("", "") resp_headers = { "X-Pagination-Item-Count": 4, "X-Pagination-Limit": 1, "X-Pagination-Page": 2, "X-Pagination-Page-Count": 3, } http = DefaultHttpComponent( client, requests_dependency=MockRequests({".*": [{"a": "v"}, 200, resp_headers]}), ) res = http.request("abc") assert res.json == {"a": "v"} assert res.original.status_code == 200 assert res.pagination["limit"] == 1 assert res.pagination["page_count"] == 3
def test_refresh_token_on(): client = Trakt("", "", http_component=TOKEN_REFRESH_HTTP, auto_refresh_token=True) # token is not going to expire soon (should not refresh) expire_at = int(time.time()) + 2 * 30 * 24 * 60 * 60 # 60 days client.set_user(TraktCredentials("access", "refresh", "scope", expire_at)) client.countries.get_countries(type="shows") assert client.user.refresh_token == "refresh" assert client.user.access_token == "access" # token is going to expire soon expire_at = int(time.time()) + 15 * 24 * 60 * 60 # 15 days client.set_user(TraktCredentials("access", "refresh", "scope", expire_at)) client.countries.get_countries(type="shows") assert client.user.refresh_token == OAUTH_GET_TOKEN["refresh_token"] assert client.user.access_token == OAUTH_GET_TOKEN["access_token"]
def test_required_args(): client = Trakt("", "") p = Path("aaa/!b/ccc/?d", [{"a": str}]) assert p.methods == ["GET"] assert p.args == ["!b", "?d"] default_alias = "aaa.ccc" assert p.aliases == [default_alias] assert p.does_match(default_alias) with pytest.raises(ArgumentError): p.is_valid(client) with pytest.raises( ArgumentError): # intentional, assert didn't bind any values p.is_valid(client) assert p.is_valid(client, b=10) assert p.get_path_and_qargs() == ("aaa/10/ccc", {})
trakt_manager.add_to_watchlist(obj) print 'Collected' def handler(event, context): parser = ConfigParser.ConfigParser() parser.read('config.ini') trakt_manager = Trakt() if not trakt_manager.is_token_valid(): print 'Your access has been revoked or invalidated, please authenticate again.' else: update_history() collect_new_followed() if __name__ == '__main__': # handler() parser = ConfigParser.ConfigParser() parser.read('config.ini') trakt_manager = Trakt() if not trakt_manager.is_token_valid(): print 'Your access has been revoked or invalidated, please authenticate again.' else: update_history() collect_new_followed()
config = ConfigParser.ConfigParser() config.read('myepisodes_export.ini') tvdb = tvdb_api.Tvdb() my_episodes = MyEpisodes(config.get('MyEpisodes', 'Username'), config.get('MyEpisodes', 'Password')) login = my_episodes.login() if (login == False): print "ERROR - Could not login to MyEpisodes" sys.exit(1) my_episodes.get_show_list() trakt = Trakt(config.get('Trakt', 'ClientId'), config.get('Trakt', 'ClientSecret')) print "Requesting Trakt.tv authorization..." print "To authorize access to you trakt.tv account access the following URL in a web browser and copy the authorization code:" print trakt.get_authorize_url() code = raw_input('Paste the authorization code here: ') trakt.authorize(code) for show in my_episodes.show_list: show['name'] = unicodedata.normalize('NFKD', show['name']).encode( 'ascii', 'ignore') print "\nProcessing: {}".format(show['name']) try: tvdb_data = tvdb[show['name']] except:
def test_get_url(): client = Trakt("", "") url = client.http.get_url("a/b/c", {"d": "e"}) assert url == "https://api.trakt.tv/a/b/c?d=e"