Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element not found when parsing dates #35

Closed
tomasz-tomczyk opened this issue Nov 2, 2013 · 5 comments
Closed

Element not found when parsing dates #35

tomasz-tomczyk opened this issue Nov 2, 2013 · 5 comments

Comments

@tomasz-tomczyk
Copy link

Hi,
It's my first time using Python so my debugging skills are very rough, but upon iterating over some search results, I'm getting the following error: TypeError: 'NoneType' object is not iterable

Stack trace ends with the following:

  File "/Users/tomasz/Server/couch/venv/lib/python2.7/site-packages/tpb/tpb.py", line 144, in items
    for item in super(Paginated, self).items():
  File "/Users/tomasz/Server/couch/venv/lib/python2.7/site-packages/tpb/tpb.py", line 60, in items
    yield self._build_torrent(row)
  File "/Users/tomasz/Server/couch/venv/lib/python2.7/site-packages/tpb/tpb.py", line 102, in _build_torrent
    created = dateutil.parser.parse(match.groups()[0].replace('\xa0', ' '))
  File "/Users/tomasz/Server/couch/venv/lib/python2.7/site-packages/dateutil/parser.py", line 748, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Users/tomasz/Server/couch/venv/lib/python2.7/site-packages/dateutil/parser.py", line 310, in parse
    res, skipped_tokens = self._parse(timestr, **kwargs)

After digging into the code, line 102 in tpb.py seems to be the one causing issues
created = dateutil.parser.parse(match.groups()[0].replace('\xa0', ' '))

When replaced with datetime.new() it works. Having said that, it might be TPB itself causing problems as I can't reliably reproduce the issue 100% of the time... sometimes it just works. Any thoughts?

@mmsobral
Copy link

mmsobral commented Nov 4, 2013

I found that it happens due to the way TPB reports recent torrents, like "Today 04:59", or "10 mins ago". To fix that I added the following method to class "parser":

def _fix_date(self, timestr):
    if timestr[-3:] == 'ago':
      num,kind,ago = timestr.split()
      num = int(num)
      t = time.time()
      if kind in ['min','mins']:
        t = t - num*60
      elif kind in ['sec','secs']:
        t -= num
      elif kind in ['hour','hours']:
        t = t - num*3600
      timestr = time.strftime('%d-%m %H:%S', time.localtime(t))
      return timestr.decode('utf8')
    today = time.strftime('%d-%m',time.localtime())
    timestr = timestr.replace('Today',today)
    # if date is still invalid ... (sometimes it happens !)
    # replace it with current date.
    m = re.match('[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}', timestr)
    if not m:
      timestr = time.strftime('%d-%m %H:%S', time.localtime())
    return timestr.decode('utf8')

... and in method "parse" of the same class, I call _fix_date just before _parse:

    timestr = self._fix_date(timestr)
    res, skipped_tokens = self._parse(timestr, **kwargs)

Despite not an elegant solution (it was clearly coded in a hurry), it works fine ;-)

Obs: This fix needs "re" module, so you must include it in the top of parser.py.

@umazalakain
Copy link
Collaborator

I currently haven't time to look into this but I'll try to write some tests for it when I do. Thanks for the contribution!!

@karan
Copy link
Owner

karan commented Nov 5, 2013

I'll look over this in a few days. Very busy with exams this week. :/

@umazalakain
Copy link
Collaborator

I started working on this (finished my exams too :-p) though I didn't wrote the tests yet, give me till tomorrow ;-)

karan pushed a commit that referenced this issue Nov 12, 2013
Fixed #35 -- correct torrent creation date parsing
@umazalakain
Copy link
Collaborator

Finally corrected in 203b533

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants