aparat.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. HEADRequest,
  8. )
  9. class AparatIE(InfoExtractor):
  10. _VALID_URL = r'^https?://(?:www\.)?aparat\.com/(?:v/|video/video/embed/videohash/)(?P<id>[a-zA-Z0-9]+)'
  11. _TEST = {
  12. 'url': 'http://www.aparat.com/v/wP8On',
  13. 'md5': '6714e0af7e0d875c5a39c4dc4ab46ad1',
  14. 'info_dict': {
  15. 'id': 'wP8On',
  16. 'ext': 'mp4',
  17. 'title': 'تیم گلکسی 11 - زومیت',
  18. },
  19. # 'skip': 'Extremely unreliable',
  20. }
  21. def _real_extract(self, url):
  22. video_id = self._match_id(url)
  23. # Note: There is an easier-to-parse configuration at
  24. # http://www.aparat.com/video/video/config/videohash/%video_id
  25. # but the URL in there does not work
  26. embed_url = ('http://www.aparat.com/video/video/embed/videohash/' +
  27. video_id + '/vt/frame')
  28. webpage = self._download_webpage(embed_url, video_id)
  29. video_urls = [video_url.replace('\\/', '/') for video_url in re.findall(
  30. r'(?:fileList\[[0-9]+\]\s*=|"file"\s*:)\s*"([^"]+)"', webpage)]
  31. for i, video_url in enumerate(video_urls):
  32. req = HEADRequest(video_url)
  33. res = self._request_webpage(
  34. req, video_id, note='Testing video URL %d' % i, errnote=False)
  35. if res:
  36. break
  37. else:
  38. raise ExtractorError('No working video URLs found')
  39. title = self._search_regex(r'\s+title:\s*"([^"]+)"', webpage, 'title')
  40. thumbnail = self._search_regex(
  41. r'image:\s*"([^"]+)"', webpage, 'thumbnail', fatal=False)
  42. return {
  43. 'id': video_id,
  44. 'title': title,
  45. 'url': video_url,
  46. 'ext': 'mp4',
  47. 'thumbnail': thumbnail,
  48. }