forked from modwedar/YoutubeDataApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.dart
125 lines (116 loc) · 4.39 KB
/
video.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import 'package:youtube_data_api/models/thumbnail.dart';
class Video {
///Youtube video id
String? videoId;
///Youtube video duration
String? duration;
///Youtube video title
String? title;
///Youtube video channel name
String? channelName;
///Youtube video views
String? views;
///Youtube video thumbnail
List<Thumbnail>? thumbnails;
Video(
{this.videoId,
this.duration,
this.title,
this.channelName,
this.views,
this.thumbnails});
factory Video.fromMap(Map<String, dynamic>? map) {
List<Thumbnail>? thumbnails;
if (map?.containsKey("videoRenderer") ?? false) {
//Trending and search videos
var lengthText = map?['videoRenderer']?['lengthText'];
var simpleText =
map?['videoRenderer']?['shortViewCountText']?['simpleText'];
thumbnails = [];
map?['videoRenderer']['thumbnail']['thumbnails'].forEach((thumbnail) {
thumbnails!.add(Thumbnail(
url: thumbnail['url'],
width: thumbnail['width'],
height: thumbnail['height']));
});
return Video(
videoId: map?['videoRenderer']?['videoId'],
duration: (lengthText == null) ? "Live" : lengthText?['simpleText'],
title: map?['videoRenderer']?['title']?['runs']?[0]?['text'],
channelName: map?['videoRenderer']['longBylineText']['runs'][0]
['text'],
thumbnails: thumbnails,
views: (lengthText == null)
? "Views " +
map!['videoRenderer']['viewCountText']['runs'][0]['text']
: simpleText);
} else if (map?.containsKey("compactVideoRenderer") ?? false) {
//Related videos
thumbnails = [];
map?['compactVideoRenderer']['thumbnail']['thumbnails']
.forEach((thumbnail) {
thumbnails!.add(Thumbnail(
url: thumbnail['url'],
width: thumbnail['width'],
height: thumbnail['height']));
});
return Video(
videoId: map?['compactVideoRenderer']['videoId'],
title: map?['compactVideoRenderer']?['title']?['simpleText'],
duration: map?['compactVideoRenderer']?['lengthText']?['simpleText'],
thumbnails: thumbnails,
channelName: map?['compactVideoRenderer']?['shortBylineText']?['runs']
?[0]?['text'],
views: map?['compactVideoRenderer']?['viewCountText']?['simpleText']);
} else if (map?.containsKey("gridVideoRenderer") ?? false) {
String? simpleText =
map?['gridVideoRenderer']['shortViewCountText']?['simpleText'];
thumbnails = [];
map?['gridVideoRenderer']['thumbnail']['thumbnails'].forEach((thumbnail) {
thumbnails!.add(Thumbnail(
url: thumbnail['url'],
width: thumbnail['width'],
height: thumbnail['height']));
});
return Video(
videoId: map?['gridVideoRenderer']['videoId'],
title: map?['gridVideoRenderer']['title']['runs'][0]['text'],
duration: map?['gridVideoRenderer']['thumbnailOverlays'][0]
['thumbnailOverlayTimeStatusRenderer']['text']['simpleText'],
thumbnails: thumbnails,
views: (simpleText != null) ? simpleText : "???");
} else if (map?.containsKey("playlistVideoRenderer") ?? false) {
var lengthText = map?['playlistVideoRenderer']?['lengthText'];
var simpleText =
map?['playlistVideoRenderer']?['shortViewCountText']?['simpleText'];
thumbnails = [];
map?['playlistVideoRenderer']['thumbnail']['thumbnails']
.forEach((thumbnail) {
thumbnails!.add(Thumbnail(
url: thumbnail['url'],
width: thumbnail['width'],
height: thumbnail['height']));
});
return Video(
videoId: map?['playlistVideoRenderer']?['videoId'],
duration: (lengthText == null) ? "Live" : lengthText?['simpleText'],
title: map?['playlistVideoRenderer']?['title']?['runs']?[0]?['text'],
channelName: '',
thumbnails: thumbnails,
views: (lengthText == null)
? "Views " +
map!['playlistVideoRenderer']['videoInfo']['runs'][0]['text']
: simpleText);
}
return Video();
}
Map<String, dynamic> toJson() {
return {
"videoId": videoId,
"duration": duration,
"title": title,
"channelName": channelName,
"views": views
};
}
}