| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  | # coding: utf-8 | 
					
						
							|  |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-04 00:26:58 +07:00
										 |  |  | import itertools | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  | import re | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  | from .common import InfoExtractor | 
					
						
							|  |  |  | from .nexx import NexxIE | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  | from ..compat import compat_str | 
					
						
							| 
									
										
										
										
											2018-04-27 03:45:52 +07:00
										 |  |  | from ..utils import ( | 
					
						
							|  |  |  |     int_or_none, | 
					
						
							|  |  |  |     try_get, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunkBaseIE(InfoExtractor): | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |     _HEADERS = { | 
					
						
							|  |  |  |         'Accept': '*/*', | 
					
						
							|  |  |  |         'Accept-Language': 'en-US,en;q=0.9,ru;q=0.8', | 
					
						
							|  |  |  |         'authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnROYW1lIjoid2ViYXBwLXYzMSIsInNjb3BlIjoic3RhdGljLWNvbnRlbnQtYXBpLGN1cmF0aW9uLWFwaSxuZXh4LWNvbnRlbnQtYXBpLXYzMSx3ZWJhcHAtYXBpIn0.mbuG9wS9Yf5q6PqgR4fiaRFIagiHk9JhwoKES7ksVX4', | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     _AUTH = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGllbnROYW1lIjoid2ViYXBwLXYzMSIsInNjb3BlIjoic3RhdGljLWNvbnRlbnQtYXBpLGN1cmF0aW9uLWFwaSxuZXh4LWNvbnRlbnQtYXBpLXYzMSx3ZWJhcHAtYXBpIn0.mbuG9wS9Yf5q6PqgR4fiaRFIagiHk9JhwoKES7ksVX4' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @staticmethod | 
					
						
							|  |  |  |     def _make_headers(referer): | 
					
						
							|  |  |  |         headers = FunkBaseIE._HEADERS.copy() | 
					
						
							|  |  |  |         headers['Referer'] = referer | 
					
						
							|  |  |  |         return headers | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |     def _make_url_result(self, video): | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             '_type': 'url_transparent', | 
					
						
							|  |  |  |             'url': 'nexx:741:%s' % video['sourceId'], | 
					
						
							|  |  |  |             'ie_key': NexxIE.ie_key(), | 
					
						
							|  |  |  |             'id': video['sourceId'], | 
					
						
							|  |  |  |             'title': video.get('title'), | 
					
						
							|  |  |  |             'description': video.get('description'), | 
					
						
							|  |  |  |             'duration': int_or_none(video.get('duration')), | 
					
						
							|  |  |  |             'season_number': int_or_none(video.get('seasonNr')), | 
					
						
							|  |  |  |             'episode_number': int_or_none(video.get('episodeNr')), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunkMixIE(FunkBaseIE): | 
					
						
							|  |  |  |     _VALID_URL = r'https?://(?:www\.)?funk\.net/mix/(?P<id>[^/]+)/(?P<alias>[^/?#&]+)' | 
					
						
							|  |  |  |     _TESTS = [{ | 
					
						
							|  |  |  |         'url': 'https://www.funk.net/mix/59d65d935f8b160001828b5b/die-realste-kifferdoku-aller-zeiten', | 
					
						
							|  |  |  |         'md5': '8edf617c2f2b7c9847dfda313f199009', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '123748', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': '"Die realste Kifferdoku aller Zeiten"', | 
					
						
							|  |  |  |             'description': 'md5:c97160f5bafa8d47ec8e2e461012aa9d', | 
					
						
							|  |  |  |             'timestamp': 1490274721, | 
					
						
							|  |  |  |             'upload_date': '20170323', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     }] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							|  |  |  |         mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |         mix_id = mobj.group('id') | 
					
						
							|  |  |  |         alias = mobj.group('alias') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lists = self._download_json( | 
					
						
							|  |  |  |             'https://www.funk.net/api/v3.1/curation/curatedLists/', | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |             mix_id, headers=self._make_headers(url), query={ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |                 'size': 100, | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |             })['_embedded']['curatedListList'] | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         metas = next( | 
					
						
							|  |  |  |             l for l in lists | 
					
						
							|  |  |  |             if mix_id in (l.get('entityId'), l.get('alias')))['videoMetas'] | 
					
						
							|  |  |  |         video = next( | 
					
						
							|  |  |  |             meta['videoDataDelegate'] | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |             for meta in metas | 
					
						
							|  |  |  |             if try_get( | 
					
						
							|  |  |  |                 meta, lambda x: x['videoDataDelegate']['alias'], | 
					
						
							|  |  |  |                 compat_str) == alias) | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return self._make_url_result(video) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  | class FunkChannelIE(FunkBaseIE): | 
					
						
							|  |  |  |     _VALID_URL = r'https?://(?:www\.)?funk\.net/channel/(?P<id>[^/]+)/(?P<alias>[^/?#&]+)' | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  |     _TESTS = [{ | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |         'url': 'https://www.funk.net/channel/ba/die-lustigsten-instrumente-aus-dem-internet-teil-2', | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  |         'info_dict': { | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |             'id': '1155821', | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  |             'ext': 'mp4', | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |             'title': 'Die LUSTIGSTEN INSTRUMENTE aus dem Internet - Teil 2', | 
					
						
							|  |  |  |             'description': 'md5:a691d0413ef4835588c5b03ded670c1f', | 
					
						
							|  |  |  |             'timestamp': 1514507395, | 
					
						
							|  |  |  |             'upload_date': '20171229', | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  |         }, | 
					
						
							|  |  |  |         'params': { | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2018-04-27 03:45:52 +07:00
										 |  |  |     }, { | 
					
						
							|  |  |  |         # only available via byIdList API | 
					
						
							|  |  |  |         'url': 'https://www.funk.net/channel/informr/martin-sonneborn-erklaert-die-eu', | 
					
						
							|  |  |  |         'info_dict': { | 
					
						
							|  |  |  |             'id': '205067', | 
					
						
							|  |  |  |             'ext': 'mp4', | 
					
						
							|  |  |  |             'title': 'Martin Sonneborn erklärt die EU', | 
					
						
							|  |  |  |             'description': 'md5:050f74626e4ed87edf4626d2024210c0', | 
					
						
							|  |  |  |             'timestamp': 1494424042, | 
					
						
							|  |  |  |             'upload_date': '20170510', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'params': { | 
					
						
							|  |  |  |             'skip_download': True, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  |     }, { | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |         'url': 'https://www.funk.net/channel/59d5149841dca100012511e3/mein-erster-job-lovemilla-folge-1/lovemilla/', | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  |         'only_matching': True, | 
					
						
							|  |  |  |     }] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _real_extract(self, url): | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |         mobj = re.match(self._VALID_URL, url) | 
					
						
							|  |  |  |         channel_id = mobj.group('id') | 
					
						
							|  |  |  |         alias = mobj.group('alias') | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |         headers = self._make_headers(url) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 03:45:52 +07:00
										 |  |  |         video = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |         # Id-based channels are currently broken on their side: webplayer | 
					
						
							|  |  |  |         # tries to process them via byChannelAlias endpoint and fails | 
					
						
							|  |  |  |         # predictably. | 
					
						
							| 
									
										
										
										
											2018-08-04 00:26:58 +07:00
										 |  |  |         for page_num in itertools.count(): | 
					
						
							|  |  |  |             by_channel_alias = self._download_json( | 
					
						
							|  |  |  |                 'https://www.funk.net/api/v3.1/webapp/videos/byChannelAlias/%s' | 
					
						
							|  |  |  |                 % channel_id, | 
					
						
							|  |  |  |                 'Downloading byChannelAlias JSON page %d' % (page_num + 1), | 
					
						
							|  |  |  |                 headers=headers, query={ | 
					
						
							|  |  |  |                     'filterFsk': 'false', | 
					
						
							|  |  |  |                     'sort': 'creationDate,desc', | 
					
						
							|  |  |  |                     'size': 100, | 
					
						
							|  |  |  |                     'page': page_num, | 
					
						
							|  |  |  |                 }, fatal=False) | 
					
						
							|  |  |  |             if not by_channel_alias: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |             video_list = try_get( | 
					
						
							|  |  |  |                 by_channel_alias, lambda x: x['_embedded']['videoList'], list) | 
					
						
							| 
									
										
										
										
											2018-08-04 00:26:58 +07:00
										 |  |  |             if not video_list: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |                 video = next(r for r in video_list if r.get('alias') == alias) | 
					
						
							| 
									
										
										
										
											2018-08-04 00:26:58 +07:00
										 |  |  |                 break | 
					
						
							|  |  |  |             except StopIteration: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |             if not try_get( | 
					
						
							|  |  |  |                     by_channel_alias, lambda x: x['_links']['next']): | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not video: | 
					
						
							|  |  |  |             by_id_list = self._download_json( | 
					
						
							|  |  |  |                 'https://www.funk.net/api/v3.0/content/videos/byIdList', | 
					
						
							|  |  |  |                 channel_id, 'Downloading byIdList JSON', headers=headers, | 
					
						
							|  |  |  |                 query={ | 
					
						
							|  |  |  |                     'ids': alias, | 
					
						
							|  |  |  |                 }, fatal=False) | 
					
						
							|  |  |  |             if by_id_list: | 
					
						
							|  |  |  |                 video = try_get(by_id_list, lambda x: x['result'][0], dict) | 
					
						
							| 
									
										
										
										
											2018-04-27 03:45:52 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if not video: | 
					
						
							|  |  |  |             results = self._download_json( | 
					
						
							| 
									
										
										
										
											2018-07-06 23:49:36 +07:00
										 |  |  |                 'https://www.funk.net/api/v3.0/content/videos/filter', | 
					
						
							|  |  |  |                 channel_id, 'Downloading filter JSON', headers=headers, query={ | 
					
						
							| 
									
										
										
										
											2018-04-27 03:45:52 +07:00
										 |  |  |                     'channelId': channel_id, | 
					
						
							|  |  |  |                     'size': 100, | 
					
						
							|  |  |  |                 })['result'] | 
					
						
							|  |  |  |             video = next(r for r in results if r.get('alias') == alias) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:44:13 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 03:17:46 +07:00
										 |  |  |         return self._make_url_result(video) |