|
1 | 1 | package org.schabi.newpipe.extractor; |
2 | 2 |
|
3 | | -import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
4 | | - |
5 | 3 | /* |
6 | 4 | * Created by Christian Schabesberger on 23.08.15. |
7 | 5 | * |
|
22 | 20 | * along with NewPipe. If not, see <http://www.gnu.org/licenses/>. |
23 | 21 | */ |
24 | 22 |
|
| 23 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 24 | + |
25 | 25 | /** |
26 | | - * Provides access to the video streaming services supported by NewPipe. |
27 | | - * Currently only Youtube until the API becomes more stable. |
| 26 | + * Provides access to streaming services supported by NewPipe. |
28 | 27 | */ |
29 | | - |
30 | | -@SuppressWarnings("ALL") |
31 | 28 | public class NewPipe { |
32 | 29 | private static final String TAG = NewPipe.class.toString(); |
| 30 | + private static Downloader downloader = null; |
33 | 31 |
|
34 | 32 | private NewPipe() { |
35 | 33 | } |
36 | 34 |
|
37 | | - private static Downloader downloader = null; |
| 35 | + public static void init(Downloader d) { |
| 36 | + downloader = d; |
| 37 | + } |
| 38 | + |
| 39 | + public static Downloader getDownloader() { |
| 40 | + return downloader; |
| 41 | + } |
| 42 | + |
| 43 | + /*////////////////////////////////////////////////////////////////////////// |
| 44 | + // Utils |
| 45 | + //////////////////////////////////////////////////////////////////////////*/ |
38 | 46 |
|
39 | 47 | public static StreamingService[] getServices() { |
40 | | - return ServiceList.serviceList; |
| 48 | + final ServiceList[] values = ServiceList.values(); |
| 49 | + final StreamingService[] streamingServices = new StreamingService[values.length]; |
| 50 | + |
| 51 | + for (int i = 0; i < values.length; i++) streamingServices[i] = values[i].getService(); |
| 52 | + |
| 53 | + return streamingServices; |
41 | 54 | } |
42 | 55 |
|
43 | 56 | public static StreamingService getService(int serviceId) throws ExtractionException { |
44 | | - for (StreamingService s : ServiceList.serviceList) { |
45 | | - if (s.getServiceId() == serviceId) { |
46 | | - return s; |
| 57 | + for (ServiceList item : ServiceList.values()) { |
| 58 | + if (item.getService().getServiceId() == serviceId) { |
| 59 | + return item.getService(); |
47 | 60 | } |
48 | 61 | } |
49 | | - return null; |
| 62 | + throw new ExtractionException("There's no service with the id = \"" + serviceId + "\""); |
50 | 63 | } |
51 | 64 |
|
52 | 65 | public static StreamingService getService(String serviceName) throws ExtractionException { |
53 | | - return ServiceList.serviceList[getIdOfService(serviceName)]; |
54 | | - } |
55 | | - |
56 | | - public static String getNameOfService(int id) { |
57 | | - try { |
58 | | - return getService(id).getServiceInfo().name; |
59 | | - } catch (Exception e) { |
60 | | - System.err.println("Service id not known"); |
61 | | - e.printStackTrace(); |
62 | | - return ""; |
| 66 | + for (ServiceList item : ServiceList.values()) { |
| 67 | + if (item.getService().getServiceInfo().name.equals(serviceName)) { |
| 68 | + return item.getService(); |
| 69 | + } |
63 | 70 | } |
| 71 | + throw new ExtractionException("There's no service with the name = \"" + serviceName + "\""); |
64 | 72 | } |
65 | 73 |
|
66 | | - public static int getIdOfService(String serviceName) { |
67 | | - for (int i = 0; i < ServiceList.serviceList.length; i++) { |
68 | | - if (ServiceList.serviceList[i].getServiceInfo().name.equals(serviceName)) { |
69 | | - return i; |
| 74 | + public static StreamingService getServiceByUrl(String url) throws ExtractionException { |
| 75 | + for (ServiceList item : ServiceList.values()) { |
| 76 | + if (item.getService().getLinkTypeByUrl(url) != StreamingService.LinkType.NONE) { |
| 77 | + return item.getService(); |
70 | 78 | } |
71 | 79 | } |
72 | | - return -1; |
| 80 | + throw new ExtractionException("No service can handle the url = \"" + url + "\""); |
73 | 81 | } |
74 | 82 |
|
75 | | - public static void init(Downloader d) { |
76 | | - downloader = d; |
77 | | - } |
78 | | - |
79 | | - public static Downloader getDownloader() { |
80 | | - return downloader; |
| 83 | + public static int getIdOfService(String serviceName) { |
| 84 | + try { |
| 85 | + //noinspection ConstantConditions |
| 86 | + return getService(serviceName).getServiceId(); |
| 87 | + } catch (ExtractionException ignored) { |
| 88 | + return -1; |
| 89 | + } |
81 | 90 | } |
82 | 91 |
|
83 | | - public static StreamingService getServiceByUrl(String url) { |
84 | | - for (StreamingService s : ServiceList.serviceList) { |
85 | | - if (s.getLinkTypeByUrl(url) != StreamingService.LinkType.NONE) { |
86 | | - return s; |
87 | | - } |
| 92 | + public static String getNameOfService(int id) { |
| 93 | + try { |
| 94 | + //noinspection ConstantConditions |
| 95 | + return getService(id).getServiceInfo().name; |
| 96 | + } catch (Exception e) { |
| 97 | + System.err.println("Service id not known"); |
| 98 | + e.printStackTrace(); |
| 99 | + return "<unknown>"; |
88 | 100 | } |
89 | | - return null; |
90 | 101 | } |
91 | 102 | } |
0 commit comments