2
* Copyright (C) 2011, Marco Trevisan (Treviño) <mail@3v1n0.net>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
using Unity, GData, Dee;
20
class YoutubeScope : Scope
22
private const string DEVELOPER_KEY = "AI39si7ywcqI6NfSfJ38YjqqTNDUuhWAG9LFFUp2hr6ew6iNuzX4hhXKHnJqL3E90K6cXlk3lmq2Znv4o9TLaKNK2qroi_QpAw";
23
private GDataFixes.YouTubeService service;
24
private Cancellable cancellable_query;
28
base("/net/3v1n0/dev/unity/scope/video/youtube");
30
//XXX Show most popular when the search is ""
32
service = new GDataFixes.YouTubeService(DEVELOPER_KEY);
33
service.set_locale("it"); //XXX
34
cancellable_query = null;
36
notify["active-search"].connect(() => {
37
//add a timeout to prevent fast typing!
39
var search = active_search;
40
debug(@"Search active changeed to $(search.search_string)");
43
if (cancellable_query != null) {
44
cancellable_query.cancel();
45
cancellable_query = null;
48
cancellable_query = new Cancellable();
49
results_model.clear();
51
if (search.search_string == null || search.search_string == "") {
53
service.query_standard_feed_async.begin(YouTubeStandardFeedType.MOST_POPULAR_FEED, null,
54
cancellable_query, (entry, en_key, entry_count) => {
55
debug(@"Progress call back! $(entry.get_title()) -> $((entry as YouTubeVideo).get_player_uri()) $((entry as YouTubeVideo).get_description()), $en_key, $entry_count");
56
add_video_to_model(entry as YouTubeVideo);
57
}, null, (obj, res) => {
58
var vids = service.query_async.end(res);
59
message("Vids are %p".printf(vids));
61
//foreach (var entry in vids.get_entries())
62
//message(@"Found entry $(entry.get_title ()) of $(vids.get_entries().length())");
69
var ytquery = new YouTubeQuery(search.search_string); //query_standard_feed YouTubeStandardFeedType.MOST_POPULAR_FEED
70
/*var videos = service.query_videos(ytquery, cancellable_query, (entry, en_key, entry_count) => {
71
debug(@"Progress call back! $(entry.get_content()), $en_key, $entry_count");
75
/*var videos = service.query_standard_feed(YouTubeStandardFeedType.MOST_POPULAR_FEED,
76
ytquery, cancellable_query, (entry, en_key, entry_count) => {
77
debug(@"Progress call back! $(entry.get_content()), $en_key, $entry_count");
81
service.query_videos_async.begin(ytquery, cancellable_query, (entry, en_key, entry_count) => {
82
if (cancellable_query.is_cancelled())
85
debug(@"Progress call back! $(entry.get_title()), $en_key, $entry_count");
86
add_video_to_model(entry as YouTubeVideo);
87
}, null, (obj, res) => {
88
var vids = service.query_async.end(res);
90
//foreach (var entry in vids.get_entries())
91
//message(@"Found entry $(entry.get_title ()) of $(vids.get_entries().length())");
94
/*foreach (var entry in videos.get_entries())
95
message(@"Found entry $(entry.get_title ()) of $(videos.get_entries().length())");*/
98
filters_changed.connect(() => { debug("Filters changed"); });
100
active_search.finished.connect(() => {
101
debug("Search active FINISHED");
104
activate_uri.connect((uri) => {debug(@"Clicked over me! $uri"); return null;});
105
preview_uri.connect(() => {debug("Preview URI"); return null; });
108
private void add_video_to_model(YouTubeVideo video)
110
MediaThumbnail icon_thumb = null;
111
int min_delta = int.MAX;
113
foreach (var thumb in video.get_thumbnails())
115
message(@"Found thumb $(thumb.get_width())x$(thumb.get_height()): $(icon_thumb.get_uri())");
116
int delta = ((int)(thumb.get_width() - 64)).abs();
118
if (delta < min_delta) {
124
if (icon_thumb != null) {
125
message(@"Selected thumb is $(icon_thumb.get_width())x$(icon_thumb.get_height()): $(icon_thumb.get_uri())");
128
results_model.append(video.get_player_uri(),
129
"http://i.ytimg.com/vi/"+video.get_video_id()+"/default.jpg", //icon
133
video.get_description(),
134
video.get_player_uri());