gweis / isodate

ISO 8601 date/time parser
BSD 3-Clause "New" or "Revised" License
155 stars 59 forks source link

Parsing duration in http://schema.org/VideoObject #46

Closed vovanz closed 6 years ago

vovanz commented 6 years ago

So, I was working on a tool to parse schema.org markup for videos as described here

The documentation for schema.org format says that duration is "The duration of the video in ISO 8601 format" and gives the following example: T1M33S

So, I've tried to parse it via the isodate lib, but it didn't work:

[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import isodate
>>> t = 'T1M33S'
>>> isodate.parse_time(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vovanz/dyno_dev_venv/local/lib/python2.7/site-packages/isodate/isotime.py", line 148, in parse_time
    raise ISO8601Error('Unrecognised ISO 8601 time format: %r' % timestring)
isodate.isoerror.ISO8601Error: Unrecognised ISO 8601 time format: 'T1M33S'
>>> isodate.parse_date(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vovanz/dyno_dev_venv/local/lib/python2.7/site-packages/isodate/isodates.py", line 203, in parse_date
    raise ISO8601Error('Unrecognised ISO 8601 date format: %r' % datestring)
isodate.isoerror.ISO8601Error: Unrecognised ISO 8601 date format: 'T1M33S'
>>> isodate.parse_datetime(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vovanz/dyno_dev_venv/local/lib/python2.7/site-packages/isodate/isodatetime.py", line 55, in parse_datetime
    tmpdate = parse_date(datestring)
  File "/home/vovanz/dyno_dev_venv/local/lib/python2.7/site-packages/isodate/isodates.py", line 203, in parse_date
    raise ISO8601Error('Unrecognised ISO 8601 date format: %r' % datestring)
isodate.isoerror.ISO8601Error: Unrecognised ISO 8601 date format: ''
>>> isodate.parse_duration(t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vovanz/dyno_dev_venv/local/lib/python2.7/site-packages/isodate/isoduration.py", line 104, in parse_duration
    raise ISO8601Error("Unable to parse duration string %r" % datestring)
isodate.isoerror.ISO8601Error: Unable to parse duration string 'T1M33S'

I'm trying to understand what is the problem here: is there a bug in isodate or this string (T1M33S) is not a valid ISO 8601 time?

gweis commented 6 years ago

Try it with:

>>> import isodate
>>> t = 'PT1M33S'
>>> isodate.parse_duration(t)

Also, as far as I know, a duration string must start with 'P'.

I guess a video length could also be just a simple ISO 8601 time string like '01:30' or '0130', which the 'parse_time' function would be able to handle.

Hope that helps.

vovanz commented 6 years ago

Hope that helps.

Yeah, thanks.