~chipaca/unity-lens-video/recently-glanced-at-from-afar

« back to all changes in this revision

Viewing changes to src/youtube-scope.vala

  • Committer: David Callé
  • Date: 2012-02-02 19:43:07 UTC
  • Revision ID: davidc@framli.eu-20120202194307-rnvkhkvldcbdvj5t
Move to a decent branch name and lp project. Thanks a lot Marco\!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2011, Marco Trevisan (Treviño) <mail@3v1n0.net>
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 *
16
 
 */
17
 
 
18
 
using Unity, GData, Dee;
19
 
 
20
 
class YoutubeScope : Scope
21
 
{
22
 
  private const string DEVELOPER_KEY = "AI39si7ywcqI6NfSfJ38YjqqTNDUuhWAG9LFFUp2hr6ew6iNuzX4hhXKHnJqL3E90K6cXlk3lmq2Znv4o9TLaKNK2qroi_QpAw";
23
 
  private GDataFixes.YouTubeService service;
24
 
  private Cancellable cancellable_query;
25
 
 
26
 
  public YoutubeScope()
27
 
  {
28
 
    base("/net/3v1n0/dev/unity/scope/video/youtube");
29
 
 
30
 
//XXX Show most popular when the search is ""
31
 
 
32
 
    service = new GDataFixes.YouTubeService(DEVELOPER_KEY);
33
 
    service.set_locale("it"); //XXX
34
 
    cancellable_query = null;
35
 
 
36
 
    notify["active-search"].connect(() => {
37
 
      //add a timeout to prevent fast typing!
38
 
 
39
 
      var search = active_search;
40
 
      debug(@"Search active changeed to  $(search.search_string)");
41
 
 
42
 
 
43
 
      if (cancellable_query != null) {
44
 
        cancellable_query.cancel();
45
 
        cancellable_query = null;
46
 
      }
47
 
 
48
 
      cancellable_query = new Cancellable();
49
 
      results_model.clear();
50
 
 
51
 
      if (search.search_string == null || search.search_string == "") {
52
 
 
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));
60
 
 
61
 
                                                //foreach (var entry in vids.get_entries()) 
62
 
        //message(@"Found entry $(entry.get_title ()) of $(vids.get_entries().length())");
63
 
                                              });
64
 
        
65
 
        return;
66
 
      }
67
 
 
68
 
  
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");
72
 
                                              });*/
73
 
 
74
 
 
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");
78
 
                                              });*/
79
 
 
80
 
      
81
 
      service.query_videos_async.begin(ytquery, cancellable_query, (entry, en_key, entry_count) => {
82
 
                                                if (cancellable_query.is_cancelled())
83
 
                                                  return;
84
 
 
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);
89
 
 
90
 
                                                //foreach (var entry in vids.get_entries()) 
91
 
        //message(@"Found entry $(entry.get_title ()) of $(vids.get_entries().length())");
92
 
                                              });
93
 
 
94
 
      /*foreach (var entry in videos.get_entries()) 
95
 
        message(@"Found entry $(entry.get_title ()) of $(videos.get_entries().length())");*/
96
 
    });
97
 
 
98
 
    filters_changed.connect(() => { debug("Filters changed"); });
99
 
 
100
 
    active_search.finished.connect(() => {
101
 
      debug("Search active FINISHED");
102
 
    });
103
 
 
104
 
    activate_uri.connect((uri) => {debug(@"Clicked over me! $uri"); return null;});
105
 
    preview_uri.connect(() => {debug("Preview URI"); return null; });
106
 
  }
107
 
 
108
 
  private void add_video_to_model(YouTubeVideo video)
109
 
  {
110
 
    MediaThumbnail icon_thumb = null;
111
 
    int min_delta = int.MAX;
112
 
 
113
 
    foreach (var thumb in video.get_thumbnails())
114
 
    {
115
 
      message(@"Found thumb $(thumb.get_width())x$(thumb.get_height()): $(icon_thumb.get_uri())");
116
 
      int delta = ((int)(thumb.get_width() - 64)).abs();
117
 
 
118
 
      if (delta < min_delta) {
119
 
        min_delta = delta;
120
 
        icon_thumb = thumb;
121
 
      }
122
 
    }
123
 
 
124
 
    if (icon_thumb != null) {
125
 
      message(@"Selected thumb is $(icon_thumb.get_width())x$(icon_thumb.get_height()): $(icon_thumb.get_uri())");
126
 
    }
127
 
 
128
 
    results_model.append(video.get_player_uri(),
129
 
                         "http://i.ytimg.com/vi/"+video.get_video_id()+"/default.jpg", //icon
130
 
                         0, //category index
131
 
                         "text/html",
132
 
                         video.get_title(),
133
 
                         video.get_description(),
134
 
                         video.get_player_uri());
135
 
  }
136
 
}