Repository: Flexget/Flexget

Issue:
Error in npo_watchlist
### Expected behaviour:
Flexget should find my NPO Watchlist entries
### Actual behaviour:
Flexget stops at a critial error
### Steps to reproduce:
- Step 1: Execute task with NPO Watchlist in it

#### Config:
```
tasks:
  NPO series:
    npo_watchlist:
      email: maxdaniel98@gmail.com
      password: ********
      remove_accepted: yes
    accept_all: yes
    exec:
      fail_entries: yes
      auto_escape: yes
      on_output:
        for_accepted:
          - download-npo -o "/var/hdd/media/series/{{series_name_plain}}/" -f "{serie_titel} - {datum} {aflevering_titel} ({episode_id})" -t {{url}}
```
  
#### Log:
```
2016-12-25 22:53 CRITICAL task          NPO series      BUG: Unhandled error in plugin npo_watchlist: u'min' is not in list
2016-12-25 22:53 CRITICAL manager       NPO series      An unexpected crash has occurred. Writing crash report to /home/pi/.flexget/crash_report.2016.12.25.225356276310.log. Please verify you are running the latest version of flexget by using "flexget -V" from CLI or by using version_checker plugin at http://flexget.com/wiki/Plugins/version_checker. You are currently using version 2.8.11
2016-12-25 22:53 WARNING  task          NPO series      Aborting task (plugin: npo_watchlist)
2016-12-25 22:55 INFO     search_npo    NPO series      Retrieving npo.nl episode watchlist for maxdaniel98@gmail.com
2016-12-25 22:55 CRITICAL task          NPO series      BUG: Unhandled error in plugin npo_watchlist: u'min' is not in list
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flexget/task.py", line 483, in __run_plugin
    return method(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/home/pi/.flexget/plugins/input/npo_watchlist.py", line 317, in on_task_input
    entries = self._get_watchlist_entries(task, config)
  File "/home/pi/.flexget/plugins/input/npo_watchlist.py", line 148, in _get_watchlist_entries
    entry_date = self._parse_date(listItem.find('span', class_='global__content-info').text)
  File "/home/pi/.flexget/plugins/input/npo_watchlist.py", line 81, in _parse_date
    month = months.index(date_match.group(2)) + 1
ValueError: u'min' is not in list
```

### Additional information:

- Flexget Version: 2.8.11 (latest release on 25-12-2016)
- Python Version: Python 2.7.9
- Installation method: Via PIP
- OS and version: Debian 8 Jessie on Raspberry Pi 2B
Error in npo_watchlist
### Expected behaviour:
Flexget should find my NPO Watchlist entries
### Actual behaviour:
Flexget stops at a critial error
### Steps to reproduce:
- Step 1: Execute task with NPO Watchlist in it

#### Config:
```
tasks:
  NPO series:
    npo_watchlist:
      email: maxdaniel98@gmail.com
      password: ********
      remove_accepted: yes
    accept_all: yes
    exec:
      fail_entries: yes
      auto_escape: yes
      on_output:
        for_accepted:
          - download-npo -o "/var/hdd/media/series/{{series_name_plain}}/" -f "{serie_titel} - {datum} {aflevering_titel} ({episode_id})" -t {{url}}
```
  
#### Log:
```
2016-12-25 22:53 CRITICAL task          NPO series      BUG: Unhandled error in plugin npo_watchlist: u'min' is not in list
2016-12-25 22:53 CRITICAL manager       NPO series      An unexpected crash has occurred. Writing crash report to /home/pi/.flexget/crash_report.2016.12.25.225356276310.log. Please verify you are running the latest version of flexget by using "flexget -V" from CLI or by using version_checker plugin at http://flexget.com/wiki/Plugins/version_checker. You are currently using version 2.8.11
2016-12-25 22:53 WARNING  task          NPO series      Aborting task (plugin: npo_watchlist)
2016-12-25 22:55 INFO     search_npo    NPO series      Retrieving npo.nl episode watchlist for maxdaniel98@gmail.com
2016-12-25 22:55 CRITICAL task          NPO series      BUG: Unhandled error in plugin npo_watchlist: u'min' is not in list
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flexget/task.py", line 483, in __run_plugin
    return method(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/home/pi/.flexget/plugins/input/npo_watchlist.py", line 317, in on_task_input
    entries = self._get_watchlist_entries(task, config)
  File "/home/pi/.flexget/plugins/input/npo_watchlist.py", line 148, in _get_watchlist_entries
    entry_date = self._parse_date(listItem.find('span', class_='global__content-info').text)
  File "/home/pi/.flexget/plugins/input/npo_watchlist.py", line 81, in _parse_date
    month = months.index(date_match.group(2)) + 1
ValueError: u'min' is not in list
```

### Additional information:

- Flexget Version: 2.8.11 (latest release on 25-12-2016)
- Python Version: Python 2.7.9
- Installation method: Via PIP
- OS and version: Debian 8 Jessie on Raspberry Pi 2B


Discussion:
I reduced the problem to the _parse_date function, for example `\nGisteren 19:30 · 119 min\n` doesn't get parsed well. I solved it by changing the `if` from line 80 or so to: 
````
 if first_word in ['vandaag', 'vanochtend', 'vanmiddag', 'vanavond']:
            return date.today()
        elif first_word == 'gisteren':
            return date.today() - timedelta(days=1)
        elif first_word == 'eergisteren':
            return date.today() - timedelta(days=2)
        elif date_match:
            day = int(date_match.group(1))
            month = months.index(date_match.group(2)) + 1

            year = date_match.group(3)
            if year is None:
                year = date.today().year
            else:
                year = int(year)

            return date(year, month, day)
        elif days_ago_match:
            days_ago = int(days_ago_match.group(1))
            return date.today() - timedelta(days=days_ago)
        elif first_word == 'kijk':
            return None
        else:
            log.error("Cannot understand date '%s'", date_text)
            return date.today()
````
Feel free to create a PR.
@cvium 
I'm not very familiar with Python and Git (I literally copied the plugin and gave it a new name), so I hope that  someone can help me with correcting it this in the original file. 
@maxdaniel98 could you detail why `\nGisteren 19:30 · 119 min\n` doesn't get parsed well? And why does switching the if-order solve it?
Normally, when the first_word is "gisteren" date_match should return false, but it is currently not. It can be fixed by not depending on date_match but instead looking first for ['vandaag', 'vanochtend', 'vanmiddag', 'vanavond'], gisteren or eergisteren. 
Alright, I understand, I will make a PR for this
I reduced the problem to the _parse_date function, for example `\nGisteren 19:30 · 119 min\n` doesn't get parsed well. I solved it by changing the `if` from line 80 or so to: 
````
 if first_word in ['vandaag', 'vanochtend', 'vanmiddag', 'vanavond']:
            return date.today()
        elif first_word == 'gisteren':
            return date.today() - timedelta(days=1)
        elif first_word == 'eergisteren':
            return date.today() - timedelta(days=2)
        elif date_match:
            day = int(date_match.group(1))
            month = months.index(date_match.group(2)) + 1

            year = date_match.group(3)
            if year is None:
                year = date.today().year
            else:
                year = int(year)

            return date(year, month, day)
        elif days_ago_match:
            days_ago = int(days_ago_match.group(1))
            return date.today() - timedelta(days=days_ago)
        elif first_word == 'kijk':
            return None
        else:
            log.error("Cannot understand date '%s'", date_text)
            return date.today()
````
Feel free to create a PR.
@cvium 
I'm not very familiar with Python and Git (I literally copied the plugin and gave it a new name), so I hope that  someone can help me with correcting it this in the original file. 
@maxdaniel98 could you detail why `\nGisteren 19:30 · 119 min\n` doesn't get parsed well? And why does switching the if-order solve it?
Normally, when the first_word is "gisteren" date_match should return false, but it is currently not. It can be fixed by not depending on date_match but instead looking first for ['vandaag', 'vanochtend', 'vanmiddag', 'vanavond'], gisteren or eergisteren. 
Alright, I understand, I will make a PR for this

Resolving patch:
diff --git a/flexget/plugins/input/npo_watchlist.py b/flexget/plugins/input/npo_watchlist.py
--- a/flexget/plugins/input/npo_watchlist.py
+++ b/flexget/plugins/input/npo_watchlist.py
@@ -76,28 +76,26 @@ def _parse_date(self, date_text):
         days_ago_match = days_ago_regex.search(date_text)
         first_word = date_text.split(' ')[0].lower().strip()
 
-        if date_match:
+        if first_word in ['vandaag', 'vanochtend', 'vanmiddag', 'vanavond']:
+            return date.today()
+        elif first_word == 'gisteren':
+            return date.today() - timedelta(days=1)
+        elif first_word == 'eergisteren':
+            return date.today() - timedelta(days=2)
+        elif first_word == 'kijk':
+            return None
+        elif date_match:
             day = int(date_match.group(1))
             month = months.index(date_match.group(2)) + 1
-
             year = date_match.group(3)
             if year is None:
                 year = date.today().year
             else:
                 year = int(year)
-
             return date(year, month, day)
         elif days_ago_match:
             days_ago = int(days_ago_match.group(1))
             return date.today() - timedelta(days=days_ago)
-        elif first_word in ['vandaag', 'vanochtend', 'vanmiddag', 'vanavond']:
-            return date.today()
-        elif first_word == 'gisteren':
-            return date.today() - timedelta(days=1)
-        elif first_word == 'eergisteren':
-            return date.today() - timedelta(days=2)
-        elif first_word == 'kijk':
-            return None
         else:
             log.error("Cannot understand date '%s'", date_text)
             return date.today()


Test patch: