Repository: Flexget/Flexget

Issue:
BUG: t411_api invalid get requests with terms
When searching using terms the GET request sent to the server is sending invalid characters when chaining those terms. The result is those terms are not being filtered by t411 search server

For example, in this pcap file you can see the request and the response 
[t411_api_flexget.zip](https://github.com/Flexget/Flexget/files/264770/t411_api_flexget.zip)

The real request should be: 
/torrents/search/NCIS?cid=433&term%5B46%5D%5B%5D%27=949&limit=100&offset=0&term%5B45%5D%5B%5D%27=980&term%5B51%5D%5B%5D%27=1210&term%5B51%5D%5B%5D%27=1212&term%5B51%5D%5B%5D%27=1213&term%5B51%5D%5B%5D%27=1214

and it should give out 3 results only

{"query":"NCIS","offset":"0","limit":"100","total":"3","torrents":[{"id":"5505701","name":"NCIS.S13E12.FRENCH.HDTV.x264-FRiES","category":"433","rewritename":"ncis-s13e12-french-hdtv-x264-fries","seeders":"136","leechers":"1","comments":"1","isVerified":"1","added":"2016-05-05 23:02:43","size":"253477370","times_completed":"422","owner":"8039783","categoryname":"S\u00e9rie TV","categoryimage":"video-tv-series","username":"superbus49","privacy":"normal"},{"id":"5505743","name":"NCIS.S13E12.FRENCH.720p.HDTV.x264-FRiES","category":"433","rewritename":"ncis-s13e12-french-720p-hdtv-x264-fries","seeders":"169","leechers":"1","comments":"2","isVerified":"1","added":"2016-05-06 01:00:35","size":"923681846","times_completed":"459","owner":"8039783","categoryname":"S\u00e9rie TV","categoryimage":"video-tv-series","username":"superbus49","privacy":"normal"},{"id":"5505863","name":"NCIS.S13E12.FRENCH.HDTV.XviD-ZT","category":"433","rewritename":"ncis-s13e12-french-hdtv-xvid-zt","seeders":"219","leechers":"0","comments":"1","isVerified":"1","added":"2016-05-06 09:36:11","size":"326451286","times_completed":"668","owner":"99613071","categoryname":"S\u00e9rie TV","categoryimage":"video-tv-series","username":"Seuph","privacy":"normal"}]}

This is my config file
http://pastie.org/10809799



Discussion:
Thanks for your excellent analysis but I cannot reproduce your bug.

I think your bug appear during request serialization done by requests at https://github.com/Flexget/Flexget/blob/develop/flexget/plugins/api_t411.py#L254.
Key param strings seems serialized with adding a _b_ prefix. That means your are using Python 3 and probably explain why I cannot reproduce (indeed, I use python 2).

Key param string is built  at https://github.com/Flexget/Flexget/blob/develop/flexget/plugins/api_t411.py#L314.

My python skills are insufficient to further.

Edit: Only _termtype_ param key are concerned.

I use python 2.7. I'm on debian.
I don't have much python skills
Is there something else I can give to help pinpoint the issues?

I've added a log that displays the content of the param object before it's handed out to requests at
https://github.com/Flexget/Flexget/blob/develop/flexget/plugins/api_t411.py#L254.
with `log.debug('URL_PARAMS : %s' % (params))`

```
2016-05-18 13:07 VERBOSE  discover      tv-t411         Searching for `Vikings S02E02` with plugin `t411` (12 of 166)
2016-05-18 13:07 DEBUG    t411_api      tv-t411         Category named "Série TV" resolved by id 433
2016-05-18 13:07 DEBUG    t411_api      tv-t411         Resolving terms : [u'Fran\xe7ais (VFF/TrueFrench)', u'Multi (Fran\xe7ais inclus)', u'Multi (Qu\xe9b\xe9cois inclus)', u'Qu\xe9b\xe9cois (VFQ/French)', u'Saison 02', u'Episode 02']
2016-05-18 13:07 DEBUG    t411_api      tv-t411         None312312 API token. Authenticating with "kevmegforest" account...
2016-05-18 13:07 DEBUG    utils.requests tv-t411         Fetching http://api.t411.ch/auth
2016-05-18 13:07 DEBUG    t411_api      tv-t411         URL_PARAMS_BEFORE: {u'cid': 433, 'term[46][]': [938], u'limit': 100, u'offset': 0, 'term[45][]': [969], 'term[51][]': [1210, 1212, 1213, 1214]}
2016-05-18 13:07 DEBUG    t411_api      tv-t411         URL_PARAMS : {u'cid': 433, 'term[46][]': [938], u'limit': 100, u'offset': 0, 'term[45][]': [969], 'term[51][]': [1210, 1212, 1213, 1214]}
2016-05-18 13:07 DEBUG    utils.requests tv-t411         Fetching http://api.t411.ch/torrents/search/Vikings
2016-05-18 13:07 DEBUG    t411_api      tv-t411         Search produces 100 results including 0 'on pending' (the latter will not produces entries)
2016-05-18 13:07 DEBUG    discover      tv-t411         Discovered 100 entries from t411
```

The only thing that looks different is the terms  are not unicode. Could it have an impact with the requests plugin?

The solution is to make everything unicode. I think the requests function, when seeing the first parameter as unicode (u'cid') expects to see everything else as unicode or vice-versa if it's ascii string.
this is why the terms which are standard string are decoded with the b prefix by the request function

If this line is changed:
https://github.com/Flexget/Flexget/blob/develop/flexget/plugins/api_t411.py#L314
to `term_type_key_param = unicode('term[' + str(term_type_id) + '][]')`,
it fixes the problem. 

I don't think that's the best solution since this code also need to work with python3.

Since I'm a noob in python2 or 3, I really don't know what's the best way to make this line compatible with those 2 incompatible python versions.

I can reproduce your bug since I updated flexget.

@kevmegforest Good job :+1: 
I beg you to commit your patch :smile: 

@lildadou Do you know a way to make this command compatible with python3 and 2, since flexget seems to be making a big transition to it?


Resolving patch:
diff --git a/flexget/plugins/api_t411.py b/flexget/plugins/api_t411.py
--- a/flexget/plugins/api_t411.py
+++ b/flexget/plugins/api_t411.py
@@ -311,7 +311,7 @@ def search(self, query):
             url_params['offset'] = query['page_index']
         if query.get('terms') is not None:
             for (term_type_id, term_id) in query['terms']:
-                term_type_key_param = 'term[' + str(term_type_id) + '][]'
+                term_type_key_param = 'term[%s][]' % term_type_id
 
                 if url_params.get(term_type_key_param) is None:
                     url_params[term_type_key_param] = []


Test patch: