Repository: Flexget/Flexget

Issue:
descargas2020 plugin broken
### Expected behaviour:
it downloads torrent properly.

### Actual behaviour:

### Steps to reproduce:
add this rss http://feeds.feedburner.com/descargas2020new
flexget execute
you will get:
2018-12-20 07:45 WARNING  urlrewriter   series_sigo     URL rewriting descargas2020 failed: Unable to locate torrent ID from url http://descargas2020.com/descargar/serie-en-hd/9-1-1/temporada-2/capitulo-10/
2018-12-20 07:45 ERROR    entry         series_sigo     Failed 9-1-1 - Temporada 2  [HDTV 720p][Cap.210][AC3 5.1 Castellano][www.pctnew.com]9-1-1 - Temporada 2 [HDTV 720p][Cap.210][AC3 5.1 Castellano][www.pctnew.com] (None)
2018-12-20 07:45 INFO     task          series_sigo     Rerunning the task in case better resolution can be achieved.

#### Config:

```
tasks:
  series_sigo:
    rss:
      url: http://feeds.feedburner.com/descargas2020new
      link: link
    all_series: yes
    seen: local
    regexp:
      reject:
        - PCDVD
    accept_all: yes
    thetvdb_lookup: yes
    no_entries_ok: yes
    set:
      filename: "{{title|pathscrub}}.torrent"
    download: /home/flexget/torrent/
```
  
#### Log:

2018-12-20 07:45 WARNING  urlrewriter   series_sigo     URL rewriting descargas2020 failed: Unable to locate torrent ID from url http://descargas2020.com/descargar/serie-en-hd/9-1-1/temporada-2/capitulo-10/
2018-12-20 07:45 ERROR    entry         series_sigo     Failed 9-1-1 - Temporada 2  [HDTV 720p][Cap.210][AC3 5.1 Castellano][www.pctnew.com]9-1-1 - Temporada 2 [HDTV 720p][Cap.210][AC3 5.1 Castellano][www.pctnew.com] (None)
2018-12-20 07:45 INFO     task          series_sigo     Rerunning the task in case better resolution can be achieved.

```
paste log output here
```
</details>

### Additional information:

- FlexGet version: 2.17.20
- Python version: 2.7.13
- Installation method: i don't remember
- Using daemon (yes/no): no
- OS and version: debian 9 x64
- Link to crash log:

<!---
In config and debug/crash logs, remember to redact any personal or sensitive information such as passwords, API keys, private URLs and so on.

Please verify that the following data is present before submitting your issue:

- Link to a paste service or paste above the relevant config (preferably full config, including templates if present). Please make sure the paste does not expire, if possible.
- Link to a paste service or paste above debug-level logs of the relevant task/s (use `flexget -L debug execute --tasks <Task_name>`).
- FlexGet version (use `flexget -V` to get it).
- Full Python version, for example `2.7.11` (use `python -V` to get it). Note that FlexGet is not supported for use with Python v3.0, 3.1, 3.2 or 3.6.
- Installation method (pip, git install, etc).
- Whether or not you're running FlexGet as a daemon.
- OS and version.
- Attach crash log if one was generated, in addition to the debug-level log. It can be found in the directory with your config file.
--->



Discussion:
Hi,

Yes! it is also happening to me! I am not crazy! :)

**Log:**
```
VERBOSE  descargas2020 Descargas2020   Descargas2020 URL: http://descargas2020.com/descargar/serie-en-hd/superstore/temporada-4/capitulo-02/
DEBUG    utils.requests Descargas2020   GETing URL http://descargas2020.com/descargar/serie-en-hd/superstore/temporada-4/capitulo-02/ with args () and kwargs {'allow_redirects': True, u'timeout': 30}
DEBUG    descargas2020 Descargas2020   torrent ID not found, searching openTorrent script
WARNING  urlrewriter   Descargas2020   URL rewriting descargas2020 failed: Unable to locate torrent ID from url http://descargas2020.com/descargar/serie-en-hd/superstore/temporada-4/capitulo-02/
DEBUG    entry         Descargas2020   Marking entry 'Superstore_-_Temporada_4__[HDTV_720p][Cap.402][AC3_5.1_Castellano][www.pctnew.com]' as failed
ERROR    entry         Descargas2020   Failed Superstore_-_Temporada_4__[HDTV_720p][Cap.402][AC3_5.1_Castellano][www.pctnew.com] (None)
DEBUG    failed        Descargas2020   Marking Superstore_-_Temporada_4__[HDTV_720p][Cap.402][AC3_5.1_Castellano][www.pctnew.com] in failed list. Has failed 1 times.
INFO     task          Descargas2020   Plugin retry_failed has requested task to be ran again after execution has completed.
VERBOSE  task          Descargas2020   FAILED: `Superstore_-_Temporada_4__[HDTV_720p][Cap.402][AC3_5.1_Castellano][www.pctnew.com]` by urlrewriting plugin
```

It seems something has changed in the page configuration. I have also tried updating python, flexget and setuptools to the latest version with:
pip install --upgrade setuptools
pip install --upgrade flexget
but still failing.  Current code is:
```
        if not torrent_id:
            log.debug('torrent ID not found, searching openTorrent script')
            **torrent_id_prog = re.compile('function openTorrent.*\n.*\{.*(\n.*)+window\.location\.href =\s*\"(.*\/\d+_-.*[^\/])\/?\";')
            torrent_ids = soup.findAll(text=torrent_id_prog)**
            if torrent_ids:
                match = torrent_id_prog.search(torrent_ids[0])
                if match:
                    ...

        if not torrent_id:
            raise UrlRewritingError('Unable to locate torrent ID from url %s' % url)
```


I assume it is necessary to change the regex condition but my knowledge is limited.

Thank you in advance


Hi,

Found temporary solution. I assume it is not the most elegant/robust one, but ti works:
Between the 'if not torrent_id' add a new check, based on simple search. Final code will be:

        if not torrent_id:
            log.debug('torrent ID not found, searching openTorrent script')
            ...

        if not torrent_id:
            log.debug('torrent ID not found, searching text directly')
            torrent_ids = re.search('window\.location\.href = "(.*)\/"', page.text)
            if torrent_ids:
                torrent_id = torrent_ids.group(1)
                return torrent_id.replace('descargar-torrent', 'download') + '.torrent'
                    
        if not torrent_id:
            raise UrlRewritingError('Unable to locate torrent ID from url %s' % url)


Hi mate, I dont know which file to edit and add the code between these two block of orders that you are explaining. I have less knowlegment than you... I am sorry. Could you guide me to the file to edit?

Thank you.

PD: ok, I asume the file to edit is the rss descargas2020 file, regards.

PD2: very good trick, it works perfectly. thank you!!
> Hi mate, I dont know which file to edit and add the code between these two block of orders that you are explaining. I have less knowlegment than you... I am sorry. Could you guide me to the file to edit?
> 
> Thank you.
> 
> PD: ok, I asume the file to edit is the rrs descargas2020 file, regards.
> 
> PD2: very good trick, it works perfectly. thank you!!

Hi,
Yes the file to be updated is ...\flexget\plugins\sites\descargas2020.py

@Aokromes, have you tried it?

ok, I've found the file here: /flexget/lib/python2.7/site-packages/flexget/plugins/sites

Regards
Hi, I have checked with amd64 machines and it runs ok with the edited file, but not runs with arm64 machines as rock64... that's curious... don't know why it fails in other machines. amd64 machines sometimes the torrent added before, they are duplicated on next flexget execute....
Hello,

I´ve just changed the file in my pi /usr/local/lib/python2.7/dist-packages/flexget/plugins/sites/descargas2020.py

It didn´t work. It looks that, with the new check, it runs the process couple more times but it doesn´t find the Torrent file.

Could you upload the file somewhere where we could check it?

Thanks anyway.

Resolving patch:
diff --git a/flexget/plugins/sites/descargas2020.py b/flexget/plugins/sites/descargas2020.py
--- a/flexget/plugins/sites/descargas2020.py
+++ b/flexget/plugins/sites/descargas2020.py
@@ -22,7 +22,7 @@
 requests.headers.update({'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'})
 requests.add_domain_limiter(TimedLimiter('descargas2020.com', '2 seconds'))
 
-DESCARGAS2020_TORRENT_FORMAT = 'http://descargas2020.com/torrents/{:0>6}.torrent'
+DESCARGAS2020_TORRENT_FORMAT = 'http://descargas2020.com/download/{:0>6}.torrent'
 
 class UrlRewriteDescargas2020(object):
     """Descargas2020 urlrewriter and search."""
@@ -67,13 +67,10 @@ def parse_download_page(self, url, task):
                 torrent_id = match.group(1)
         if not torrent_id:
             log.debug('torrent ID not found, searching openTorrent script')
-            torrent_id_prog = re.compile('function openTorrent.*\n.*\{.*(\n.*)+window\.location\.href =\s*\"(.*\/\d+_-.*[^\/])\/?\";')
-            torrent_ids = soup.findAll(text=torrent_id_prog)
-            if torrent_ids:
-                match = torrent_id_prog.search(torrent_ids[0])
-                if match:
-                    torrent_id = match.group(2)
-                    return torrent_id.replace('descargar-torrent', 'download') + '.torrent'
+            match = re.search('function openTorrent.*\n.*\{.*(\n.*)+window\.location\.href =\s*\".*\/(\d+.*)\";',
+                              page.text, re.MULTILINE)
+            if match:
+                torrent_id = match.group(2).rstrip('/')
 
         if not torrent_id:
             raise UrlRewritingError('Unable to locate torrent ID from url %s' % url)


Test patch: