Repository: Flexget/Flexget

Issue:
Unexpected crash in plugin discover: NoneType object has no attribute 'text'
### Expected behaviour:
List of new episodes are sent to remote deluge daemon
### Actual behaviour:
Unexpected crash has occurred. 
BUG: Unhandled error in plugin discover: 'NoneType' object has no attribute 'text'

### Steps to reproduce:
I can reproduce it using my config, but I'm unable to figure out how to gather more detailed information (aside from setting flexget's log to debug).

This issue didn't start occurring until I updated flexget to the latest version. Since then, I've tried the latest version as well as the development version. I've even tried different versions of Python. At the time of this writing, I'm using:

flexget 2.9.7.dev
python 2.7.9

I'm pretty new at flexget, but I think the issue with related with this search string:
https://iptorrents.com/t?4=&5=&22=&q=Teen+Wolf+S06E08&qf=


#### Config:
http://pastebin.com/6sLVmUSe

#### Log:
http://pastebin.com/AY1pqZuP

### Additional information:

- Flexget Version: 2.9.7.dev
- Python Version: 2.7.9
- Installation method:  https://flexget.com/GitHubInstall
- OS and version: running docker container phusion/baseimage
- Link to crash log: http://pastebin.com/cweVDm9S

<!---
Please verify that the following data is present before submitting your issue:

- Paste or link to a paste service (http://pastebin.com/ for example) of relevant config (preferably full config including templates if present. Remember to redact any personal information! Please make sure the paste does not expire, if possible.
- Paste or link to a paste service of debug level logs of relevant task/s. Use `flexget -L debug execute --tasks <Task_name>`
- Flexget version (Use `flexget -V` to get it).
- Full Python version (`2.7.11` for example). Run `python -V` to get it.
- Installation method (pip, git install, etc.)
- OS and version
- Attach crash log if available
--->



Discussion:
Iptorrents change probably:
```
File "/root/flexget-dev/flexget/plugins/sites/iptorrents.py", line 150, in search
 *, entry['title'] = torrent.findPrevious("a", attrs={'class': 't_title'}).text
AttributeError: 'NoneType' object has no attribute 'text'
```
How can I help figure out what changed?
I'm having this same issue with my iptorrents search.  Is there a fix yet?
I am also having a similar problem with the iptorrents plugin, which I've had to disable temporarily. Not sure if they are related?

```
2017-01-17 23:15 CRITICAL task          download-movies BUG: Unhandled error in plugin discover: a bytes-like object is required, not 'str'
Traceback (most recent call last):
  File "/home/frank/flexget/lib/python3.5/site-packages/flexget/task.py", line 490, in __run_plugin
    return method(*args, **kwargs)
  File "/home/frank/flexget/lib/python3.5/site-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/home/frank/flexget/lib/python3.5/site-packages/flexget/plugins/input/discover.py", line 290, in on_task_input
    return self.execute_searches(config, entries, task)
  File "/home/frank/flexget/lib/python3.5/site-packages/flexget/plugins/input/discover.py", line 155, in execute_searches
    search_results = search.search(task=task, entry=entry, config=plugin_config)
  File "/home/frank/flexget/lib/python3.5/site-packages/flexget/plugin.py", line 118, in wrapped_func
    return func(*args, **kwargs)
  File "/home/frank/flexget/lib/python3.5/site-packages/flexget/plugins/sites/iptorrents.py", line 140, in search
    if '/u/' + str(config.get('uid')) not in req.content:
TypeError: a bytes-like object is required, not 'str'
```
The only chance of a fix is if someone creates a PR. None on the 'team' have access to IPT.

@frankyw that issue is unrelated.
I think I understand how the parser works. From what I can see, the parser is looking for `<td class="some name">` I have a snip of the search results of the IP torrents link. Let me know if I'm on the right track

http://pastebin.com/C2zXd286
Yes, they have removed the class t_title from the href, so it is a little more difficult to reference. Trying to fix it now.
I found the fix

Change iptorrents.py where its referring to class="t_title" to "b"
![image](https://cloud.githubusercontent.com/assets/4251071/22087599/05d75912-dd9c-11e6-869d-c67a249f5e1b.png)

I updated my local file and it completed successfully. I have no clue how to do a PR or if there are other ways to test to see if this change will cause new problems. Let me know how I can help :)
And if you are using Python 3.5, you also need to change line 140 to fix the other error I posted:

`if '/u/' + str(config.get('uid')) not in req.content.decode('utf-8'):`
where would i find my local iptorrents.py file?

That would depend on your OS as well as how you installed flexget. Try searching for iptorrents.py on the computer. 
> if '/u/' + str(config.get('uid')) not in req.content.decode('utf-8'):

This could be simplified to: `if '/u/' + config['uid'] not in req.text:`

'uid' is a required config element, (so no need for the 'get',) and is already text, (so no need for the 'str()'.) Requests also provides the '.text' attribute, which is the content decoded to text already.

If someone who is capable of testing wants to make a PR with the fixes you guys found it would be appreciated.
@gazpachoking 

I've never submitted a PR before, but I'll try to submit one sometime this weekend. 
(if anybody else wants to do that beforehand, I won't be angry, LOL)
> 'uid' is a required config element, (so no need for the 'get',) and is already text

It's optionally text. I think this should work:
`if u'/u/' + unicode(config['uid']) not in req.content:`
> It's optionally text. I think this should work:
> if u'/u/' + unicode(config['uid']) not in req.content:

Ahh, it can be an int as well. In that case it should be `str` not `unicode` (python 3 style)

`if '/u/' + str(config['uid']) not in req.text:`
Weird, where's that TypeError @frankyw mentioned coming from, then? I'm not using Python 3 so I can't test.
> Weird, where's that TypeError @frankyw mentioned coming from, then? I'm not using Python 3 so I can't test.

That was because the begining bit is text, (`'/u/' + str(whatever)`), and the end bit was bytes (`req.content`). Changing to `req.text` means both sides of the 'in' are text, which resolves that error.
Got it. I'll have a PR ready in a few minutes.

Resolving patch:
diff --git a/flexget/plugins/sites/iptorrents.py b/flexget/plugins/sites/iptorrents.py
--- a/flexget/plugins/sites/iptorrents.py
+++ b/flexget/plugins/sites/iptorrents.py
@@ -137,7 +137,7 @@ def search(self, task, entry, config=None):
             log.debug('searching with url: %s' % url)
             req = requests.get(url, cookies={'uid': str(config['uid']), 'pass': config['password']})
 
-            if '/u/' + str(config.get('uid')) not in req.content:
+            if '/u/' + str(config['uid']) not in req.text:
                 raise plugin.PluginError("Invalid cookies (user not logged in)...")
 
             soup = get_soup(req.content, parser="html.parser")
@@ -147,7 +147,7 @@ def search(self, task, entry, config=None):
                 entry = Entry()
                 entry['url'] = "{base}{link}?torrent_pass={key}".format(
                     base=BASE_URL, link=torrent['href'], key=config.get('rss_key'))
-                entry['title'] = torrent.findPrevious("a", attrs={'class': 't_title'}).text
+                entry['title'] = torrent.findPrevious('a', attrs={'class': 'b'}).text
 
                 seeders = torrent.findNext('td', {'class': 'ac t_seeders'}).text
                 leechers = torrent.findNext('td', {'class': 'ac t_leechers'}).text


Test patch: