Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Youtube

  • Loading branch information...
commit 81a384a9e4ff5ddf09174afdee02cc22f919137b 1 parent faa56a7
Evangenieur authored December 18, 2012
73  lib/video_platforms.coffee
@@ -4,6 +4,7 @@ Q = require "q"
4 4
 _ = require "underscore"
5 5
 cheerio = require "cheerio"
6 6
 request = require "request"
  7
+ytf = require "youtube-feeds"
7 8
 
8 9
 video_platforms = 
9 10
   youtube:
@@ -30,6 +31,7 @@ video_platforms =
30 31
     ]
31 32
     embed: "http://player.vimeo.com/video/{video_id}"
32 33
     default: "http://vimeo.com/{video_id}"
  34
+    api: "http://vimeo.com/api/v2/video/{video_id}.json"
33 35
   dailymotion: 
34 36
     domains: ["dailymotion.com"]
35 37
     samples: [
@@ -80,14 +82,21 @@ module.exports = me =
80 82
       # getVideoInfos
81 83
       _(video).extend vdo
82 84
       video.msg = msg
83  
-      if not video.title or not video.thumbnail
84  
-        me.getVideoMeta(url)
85  
-          .then (meta) ->
86  
-            #console.log "TITLE: #{title}"
87  
-            _(video).extend meta
88  
-            process.send video
89  
-      else
90  
-        process.send video
  85
+      switch video.provider
  86
+        when "youtube"
  87
+          me.getYoutubeMeta(video.video_id)
  88
+            .then (meta) ->
  89
+              #console.log "TITLE: #{title}"
  90
+              _(video).extend meta
  91
+              process.send video
  92
+        when "vimeo"
  93
+          me.getVimeoMeta(video.video_id)
  94
+            .then (meta) ->
  95
+              #console.log "TITLE: #{title}"
  96
+              _(video).extend meta
  97
+              process.send video
  98
+        else
  99
+          process.send video
91 100
       deferred.resolve("ok")
92 101
     else 
93 102
       try
@@ -102,9 +111,51 @@ module.exports = me =
102 111
     
103 112
     deferred.promise
104 113
 
105  
-  getVideoMeta: (url) ->
  114
+  getVimeoMeta: (video_id) ->
106 115
     deferred = Q.defer()
  116
+    console.log "URI : ", URI.generate(video_platforms.vimeo.api, video_id: video_id)
  117
+    request URI.generate(video_platforms.vimeo.api, video_id: video_id), 
  118
+      ( err, res, body ) ->
  119
+        data = JSON.parse(body)[0]
  120
+        if data.embed_privacy isnt "anywhere"
  121
+          deferred.recject "vimeo id #{video_id} cannot be embed #{data.embed_privacy}"
  122
+        deferred.resolve 
  123
+          title: data.title
  124
+          description: data.description
  125
+          duration: data.duration
  126
+          thumbnail: data.thumbnail_small
  127
+          likeCount: data.stats_number_of_likes
  128
+          viewCount: data.stats_number_of_plays
  129
+          commentCount: data.stats_number_of_comments
107 130
 
  131
+
  132
+    deferred.promise
  133
+
  134
+  getYoutubeMeta: (video_id) ->
  135
+    # Needs : 
  136
+    # Title / Thumb / Desc / Categ? / uploaded|updated? / duration / accessControl.(embed|autoPlay)
  137
+    deferred = Q.defer()
  138
+
  139
+    ytf.video(video_id).details (data) ->
  140
+      unless data
  141
+        return deferred.reject "no meta for youtube id #{video_id}"
  142
+      if data.accessControl.embed isnt "allowed"
  143
+        return deferred.reject "youtube id #{video_id} cannot be embed #{data.accessControl.embed}"
  144
+
  145
+      deferred.resolve
  146
+        title: data.title
  147
+        uploaded: data.uploaded
  148
+        category: data.category
  149
+        description: data.description
  150
+        duration: data.duration
  151
+        thumbnail: data.thumbnail.sqDefault
  152
+        rating: data.rating
  153
+        likeCount: data.likeCount
  154
+        ratingCount: data.ratingCount
  155
+        viewCount: data.viewcount
  156
+        favoriteCount: data.favoriteCount
  157
+        commentCount: data.commentCount
  158
+    ###
108 159
     request url, ( err, res, body ) ->
109 160
       if err
110 161
         deferred.reject err
@@ -123,5 +174,7 @@ module.exports = me =
123 174
         vdo_meta.title = doc("title").text()
124 175
 
125 176
       deferred.resolve vdo_meta
  177
+    ###
  178
+    deferred.promise
126 179
 
127  
-    deferred.promise
  180
+#me.getVimeoMeta("18925628").then console.log
3  package.json
@@ -21,7 +21,8 @@
21 21
     "cheerio": "0.10.3",
22 22
     "less-middleware": "*",
23 23
     "jade": "*",
24  
-    "request": "2.12.0"
  24
+    "request": "2.12.0",
  25
+    "youtube-feeds": "*"
25 26
   },
26 27
   "engines": {
27 28
     "node": "0.8.x"

0 notes on commit 81a384a

Please sign in to comment.
Something went wrong with that request. Please try again.