search: async(function*(query) {
         let response = yield this._qs.queueRequest(baseURL+"search?"+querystring.stringify(
             {
                 "part": "id",
                 "fields": "items(id/videoId)",
                 "eventType": "live",
                 "type": "video",
                 "order": "relevance",
                 "relevanceLanguage": getPreferedLocales(true)[0].substr(0,2),
                 "safeSearch": this._mature ? "moderate" : "strict",
                 "q": query,
                 "key": apiKey
             }
         ), headers);

         let streamIds = [];
         if(response.json && response.json.items && response.json.items.length)
              streamIds = response.json.items.map((entry) => entry.id.videoId);
         else
            throw "No search results found for " + this.name + " with " + query;

         let videos = yield this._qs.queueRequest(baseURL+"videos?"+querystring.stringify(
             {
                 "part": "id, snippet, liveStreamingDetails",
                 "id": streamIds.join(","),
                 "fields": "items(id,snippet(channelId,title,thumbnails/medium/url,categoryId),liveStreamingDetails/concurrentViewers)",
                 "key": apiKey,
                 "hl": getLocale()
             }
         ), headers);

         if(videos.json && videos.json.items) {
             return yield Promise.all(videos.json.items.map(async(function*(video) {
                 var channel = yield this._getChannelById(video.snippet.channelId);
                 channel.live.setLive(true);
                 channel.url = [
                     "https://youtube.com/watch?v="+video.id,
                     "https://gaming.youtube.com/watch?v="+video.id
                 ];
                 channel.title = video.snippet.title;
                 channel.thumbnail = video.snippet.thumbnails.medium.url;
                 if("liveStreamingDetails" in video)
                    channel.viewers = video.liveStreamingDetails.concurrentViewers;
                 channel.category = yield this._getCategory(video.snippet.categoryId);

                 return channel;
             }).bind(this)));
         }

         throw "None of the searchresults exist for " + this.name;
     })
Ejemplo n.º 2
0
exports.search_keyword = function(keyword, onComplete) {
    var search_url = conf.api_root + conf.search;
    console.log(search_url + "?" + querystring.stringify({word: keyword}));
    Request({
            url: search_url + "?" + querystring.stringify({word: keyword}),
            onComplete: function(response) {
                console.log(response.json);
                var data = response.json;
                if (data.status_code == 0) {
                    onComplete(data.data);
                }
            }
    }).get();
};
 getChannelDetails: function(username) {
     return this._qs.queueRequest(baseURL+"channels?"+querystring.stringify(
         {
             "part": "id,snippet",
             "forUsername": username,
             "fields": "items(id,snippet/title,snippet/thumbnails)",
             "key": apiKey
         }), headers).then((data) => {
         if(data.json && data.json.items && data.json.items.length) {
             var ch = new Channel(data.json.items[0].id, this._type);
             ch.url.push("https://youtube.com/channel/"+ch.login);
             ch.archiveUrl = "https://youtube.com/channel/"+ch.login+"/videos";
             ch.chatUrl = "https://youtube.com/channel/"+ch.login+"/discussion";
             ch.image = { "88": data.json.items[0].snippet.thumbnails.default.url,
                          "240": data.json.items[0].snippet.thumbnails.high.url
                        };
             ch.uname = data.json.items[0].snippet.title;
             return ch;
         }
         else {
             // Try to get the channel by ID if we can't get it by username.
             return this._getChannelById(username);
         }
     });
 },
 var getLiveStreams = (ids) => {
     return this._qs.queueRequest(baseURL+"videos?"+querystring.stringify(
         {
             "part": "id, snippet, liveStreamingDetails",
             "id": ids,
             "fields": "items(id,snippet(channelId,title,thumbnails/medium/url,categoryId),liveStreamingDetails/concurrentViewers)",
             "key": apiKey,
             "hl": getLocale()
         }
     ), headers).then((videos) => {
         if(videos.json && videos.json.items) {
             return Promise.all(videos.json.items.map((video) => {
                 return this._getCategory(video.snippet.categoryId).then(function(category) {
                     var channel = channels.find((channel) => channel.login == video.snippet.channelId);
                     channel.live.setLive(true);
                     channel.url = [
                         "https://youtube.com/watch?v="+video.id,
                         "https://gaming.youtube.com/watch?v="+video.id,
                         "https://youtube.com/channel/"+channel.login+"/live",
                         "https://gaming.youtube.com/channel/"+channel.login+"/live"
                     ];
                     channel.title = video.snippet.title;
                     channel.thumbnail = video.snippet.thumbnails.medium.url;
                     channel.viewers = video.liveStreamingDetails.concurrentViewers;
                     channel.category = category;
                     return channel;
                 });
             }));
         }
         else {
             throw "Could not find the given stream";
         }
     });
 };
        this._qs.queueUpdateRequest(urls, this._qs.LOW_PRIORITY, (data) => {
            if(data.json && data.json.items && data.json.items.length) {
                var ch = new User(data.json.items[0].id, this._type), page = 0,
                subsOptions = {
                    "part": "snippet",
                    "channelId": data.json.items[0].id,
                    "maxResults": 50,
                    "key": apiKey
                };
                ch.image = { "88": data.json.items[0].snippet.thumbnails.default.url,
                             "240": data.json.items[0].snippet.thumbnails.high.url
                           };
                ch.uname = data.json.items[0].snippet.title;
                new PaginationHelper({
                    url: baseURL+"subscriptions?"+querystring.stringify(subsOptions),
                    pageSize: subsOptions.maxResults,
                    initialPage: "",
                    request: (url) => {
                        return this._qs.queueRequest(url, headers);
                    },
                    getPageNumber: function(page, pageSize, data) {
                        return data.json.nextPageToken;
                    },
                    fetchNextPage: function(data) {
                        return data.json && data.json.items && data.json.pageInfo.totalResults > data.json.pageInfo.resultsPerPage*++page;
                    },
                    getItems: function(data) {
                        if(data.json && data.json.items)
                            return data.json.items;
                        else
                            return [];
                    },
                    onComplete: (subscriptions) => {
                        if(subscriptions.length) {
                            var oldUser = users.find((usr) => usr.login === ch.login);
                            ch.id = oldUser.id;
                            ch.favorites = subscriptions.map((sub) => sub.snippet.resourceId.channelId);
                            emit(this, "updateduser", ch);
                            emit(this, "newchannels", subscriptions.filter(function(follow) {
                                return !oldUser.favorites.some((fav) => fav === follow.snippet.resourceId.channelId);
                            }).map((sub) => {
                                var ret = new Channel(sub.snippet.resourceId.channelId, this._type);
                                ret.archiveUrl = "https://youtube.com/channel/"+ch.login+"/videos";
                                ret.chatUrl = "https://youtube.com/channel/"+ch.login+"/discussion";
                                ret.image = { "88": sub.snippet.thumbnails.default.url,
                                             "240": sub.snippet.thumbnails.high.url };
                                ret.uname = sub.snippet.title;
                                return ret;
                            }));

                            oldUser.favorites = ch.favorites;
                        }
                        else {
                            /** @todo Sometimes needs oAuth for some reason, I guess privacy settings. */
                            console.warn("Can't get favorites for youtube user "+ch.uname+" without oAuth as somebody with reading rights of this user's subs.");
                        }
                    }
                });
            }
        }, headers);
Ejemplo n.º 6
0
  initialize: function(requirer, name, fn) {
    var that = ns(this);

    if (arguments.length === 2) {
      fn = page;
      name = "page"+anonymousPageCounter++;
    } else if (arguments.length !== 3) {
      throw new Error("Invalid Arguments");
    }

    var ctx = pivot[name];
    var to_pivot = [];
    if (ctx) {
      if (ctx instanceof Array) {
        to_pivot = ctx;
      } else {
        throw new Error("Page already defined");
      }
    }

    // Create context
    ctx = pivot[name] = {
      require: requirer,
      code: fn.toSource(),
      options: options,
      args: {}
    }
    to_pivot.forEach(function(callback) {
      callback(ctx);
    });

    this.name = name;
    this.url = 'chrome://secure-form/content/page.html?'
                + querystring.stringify({name: name});
  },
    updateChannels: async(function*(channels) {
        let streamIds = yield Promise.all(channels.map((channel) => {
            return this._qs.queueRequest(baseURL+"search?"+querystring.stringify(
                {
                    "part": "id",
                    "channelId": channel.login,
                    "fields": "items(id/videoId)",
                    "maxResults": 1,
                    "eventType": "live",
                    "type": "video",
                    "key": apiKey
                }), headers).then(function(response) {
                if(response.json && response.json.items) {
                    if(response.json.items.length) {
                        return response.json.items[0].id.videoId;
                    }
                    else {
                        channel.live.setLive(false);
                        channel.url = [ "https://youtube.com/channel/"+channel.login ];
                        return null;
                    }
                }
                // silently ignore errors
            });
        }));

        streamIds = streamIds.filter((id) => id !== null);

        let videos = yield this._qs.queueRequest(baseURL+"videos?"+querystring.stringify(
            {
                "part": "id, snippet, liveStreamingDetails",
                "id": streamIds.join(","),
                "fields": "items(id,snippet(channelId,title,thumbnails/medium/url,categoryId),liveStreamingDetails/concurrentViewers)",
                "key": apiKey,
                "hl": getLocale()
            }
        ), headers);

        if(videos.json && videos.json.items) {
            yield Promise.all(videos.json.items.map((video) => {
                return this._getCategory(video.snippet.categoryId).then(function(category) {
                    var channel = channels.find((channel) => channel.login == video.snippet.channelId);
                    channel.live.setLive(true);
                    channel.url = [
                        "https://youtube.com/watch?v="+video.id,
                        "https://gaming.youtube.com/watch?v="+video.id,
                        "https://youtube.com/channel/"+channel.login+"/live",
                        "https://gaming.youtube.com/channel/"+channel.login+"/live"
                    ];
                    channel.title = video.snippet.title;
                    channel.thumbnail = video.snippet.thumbnails.medium.url;
                    channel.viewers = video.liveStreamingDetails.concurrentViewers;
                    channel.category = category;
                    return channel;
                });
            }));
        }

        return channels;
    }),
Ejemplo n.º 8
0
function getParams(title) {
  return querystring.stringify({
    utm_source: 'testpilot-addon',
    utm_medium: 'firefox-browser',
    utm_campaign: 'testpilot-doorhanger',
    utm_content: title
  });
}
Ejemplo n.º 9
0
  urlWithArgs: function() {
    var argIdx = ns(this).argCounter++;
    pivot[this.name].args[argIdx] = arguments;

    return this.url + '&' + querystring.stringify({
      args: argIdx
    });
  }
Ejemplo n.º 10
0
 var urls = users.map(function(user) {
     return baseURL+"channels?"+querystring.stringify(
         {
             "part": "id,snippet",
             "id": user.login,
             "fields": "items(id,snippet/title,snippet/thumbnails)",
             "key": apiKey
         });
 });
Ejemplo n.º 11
0
  function ratingListener(evt) {
    const rating = Number(evt.target.getAttribute('data-score'), 10);

    // send data to SurveyGizmo via querystrings
    notifyBox.removeNotification(box);
    const params = querystring.stringify({id: addonId,
                                          rating: rating,
                                          interval: interval});
    tabs.open(`${surveyUrl}?${params}`);
  }
Ejemplo n.º 12
0
 var urls = channels.map(function(channel) {
     return baseURL + "search?" + querystring.stringify(
         {
             "part": "id",
             "channelId": channel.login,
             "fields": "items(id/videoId)",
             "maxResults": 1,
             "eventType": "live",
             "type": "video",
             "key": apiKey
         }
     );
 });
Ejemplo n.º 13
0
    return new Promise(function (resolve, reject) {
        var client = new XMLHttpRequest();
        var uri = route.apiType + route.path;
        var data = null;

        if (route.method === "POST") {
            params.st_encoded = true;
            data = querystring.unescape(querystring.stringify(params));
        }
        else {
            uri += "?" + querystring.stringify(params);
        }
        if (config.DEBUG) {
            uri += (route.method === "POST")?("?"):("&");
            uri += config.XDEBUG;
        }

        client.open(route.method, uri);
        client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        client.timeout = 10000;

        client.send(data);

        client.onload = function () {
            if (this.status >= 200 && this.status < 300) {
                resolve(JSON.parse(this.response));
            } else {
                reject(this.statusText);
            }
        };

        client.onerror = function () {
            reject(this.statusText);
        };

        client.ontimeout = function () {
            reject(0);
        };
    });
Ejemplo n.º 14
0
 .then(clicked => {
   if (clicked) {
     const urlParams = {
       id: experiment.addon_id,
       cid: store.clientUUID,
       installed: Object.keys(store.installedAddons),
       rating,
       interval
     };
     const url = `${experiment.survey_url}?${querystring.stringify(urlParams)}`;
     tabs.open(url);
   }
 });
Ejemplo n.º 15
0
    query: function(url, method, data, headers, partial) {
        method = method || "GET";
        data = data || null;
        headers = headers || {};

        var _headers = {},
            xr = this.Request(),
            i;

        if (xr === null) {
            throw "No suitable XMLHTTPRequest object found.";
        }

        var result = Q.defer();
        xr.open(method, url, true);
        xr.onreadystatechange = function() {
            try {
                if (partial && xr.readyState === 2) {
                    xr.abort();
                    return;
                }
                if (xr.readyState === 4) {
                    result.resolve(this.result(xr));
                    return;
                }
            } catch(e) {
                result.reject(e);
            }
        }.bind(this);

        for (i in headers) {
            _headers[i.toLowerCase()] = headers[i];
        }
        headers = _headers;

        if (data && typeof(headers["content-type"]) === "undefined") {
            headers["content-type"] = "application/x-www-form-urlencoded";
        }

        for (i in headers) {
            xr.setRequestHeader(i, headers[i]);
        }

        xr.send(stringify(data));
        return result.promise;
    },
Ejemplo n.º 16
0
    updateChannel: async(function*(channellogin) {
         let [ ch, response ] = yield Promise.all([
            this._getChannelById(channellogin),
            this._qs.queueRequest(baseURL+"search?"+querystring.stringify(
                {
                    "part": "id",
                    "channelId": channellogin,
                    "fields": "items(id/videoId)",
                    "maxResults": 1,
                    "eventType": "live",
                    "type": "video",
                    "key": apiKey
                }), headers)
        ]);

        if(response.json && response.json.items) {
            if(response.json.items.length) {
                ch.live.setLive(true);
                ch.url.push("https://youtube.com/watch?v="+response.json.items[0].id.videoId);
                ch.url.push("https://gaming.youtube.com/watch?v="+response.json.items[0].id.videoId);

                let video = yield this._qs.queueRequest(baseURL+"videos?"+querystring.stringify(
                    {
                        "part": "snippet, liveStreamingDetails",
                        "id": response.json.items[0].id.videoId,
                        "fields": "items(snippet(categoryId,title,thumbnails/medium/url),liveStreamingDetails/concurrentViewers)",
                        "key": apiKey,
                        "hl": getLocale()
                    }
                ), headers);
                if(video.json && video.json.items) {
                    ch.title = video.json.items[0].snippet.title;
                    ch.thumbnail = video.json.items[0].snippet.thumbnails.medium.url;
                    ch.viewers = video.json.items[0].liveStreamingDetails.concurrentViewers;
                    ch.category = yield this._getCategory(video.json.items[0].snippet.categoryId);
                }
            }
            else {
                ch.live.setLive(false);
                ch.url.push("https://youtube.com/channel/"+ch.login);
            }

            return ch;
        }
    }),
    search: function(query) {
        return this._qs.queueRequest(baseURL+"/media/live/list?" + querystring.stringify({
            publicOnly: true,
            filter: "popular",
            search: query
        })).then((data) => {
            if(data.status == 200 && data.json && data.json.livestream && data.json.livestream.length) {
                let chans = data.json.livestream;
                if(!this._mature)
                    chans = chans.filter((m) => m.media_mature !== "1");

                return chans.map((chan) => getChannelFromJson(chan));
            }
            else {
                throw "Couldn't find any channels for the search on "+this.name+" that match "+query;
            }
        });
    }
Ejemplo n.º 18
0
  lookup : function lookup(coords) {
    var url = null,
        privateAPI = namespace(this),
        self = this,
        deferred = defer(),
        req = new XMLHttpRequest();
    try {
      url = new URL(this.GOOGLE_GEOCODING_API + "?" + 
                    stringify({ latlng: coords.latitude + "," + coords.longitude, 
                                sensor: this.sensor }));

      req.open("GET", url.toString());
      req.onreadystatechange = function () {
        var response = null;
        if (this.readyState === 4 && this.status === 200) {
          try {
            response = JSON.parse(this.responseText);
            if (response.status && response.status === "OK") {
              // reset the cache when we have new results
              privateAPI.cache = { single : null, multiple : null };
              privateAPI.results = response.results;
              deferred.resolve(response.results);
              emit(self, "geocode", response.results);
            }
          } catch (exception) {
            // reset the cache when we have issues getting results
            privateAPI.cache = { single : null, multiple : null };
            deferred.reject(exception);
          }
        } else if (this.readyState === 4 && this.status !== 200) {
          // reset the cache when we have issues getting results
          privateAPI.cache = { single : null, multiple : null };
          deferred.reject(this.status);
        }
      };
      req.send(null);
    } catch (e) {
      // reset the cache when we have issues getting results
      privateAPI.cache = { single : null, multiple : null };
      deferred.reject(e);
      emit(self, "error", e);
    }
    return deferred.promise;
  }
Ejemplo n.º 19
0
exports.authorize = function() {
    console.log('auth');
    if (tabs.activeTab.id == oauth.tabID || token_valid()) {
        return false;
    }

    var authorize_url = conf.api_root + conf.auth_url;
    tabs.open({
            url: authorize_url + "?" + 
                querystring.stringify({
                        "response_type": "token", 
                        "client_id": client_id
                }),
            onReady: function onReady(tab) {
                console.log(tab.id);
                oauth.tabID = tab.id;
            }
    });
};
// open a survey.
function survey(xconfig, extraQueryArgs={}) {
  let url = xconfig.surveyUrl;
  // get user info.
  extraQueryArgs = merge({},
    extraQueryArgs,
    {
      variation: xconfig.variation,
      xname: xconfig.name,
      who: xconfig.who,
      updateChannel: Services.appinfo.defaultUpdateChannel,
      fxVersion: Services.appinfo.version,
    }
  );
  if (extraQueryArgs) {
    url += "?" + querystring.stringify(extraQueryArgs);
  }

  console.log(url);
  require("sdk/tabs").open(url);
}
Ejemplo n.º 21
0
    /**
     * POST data to Google Analytics.
     *
     * Args:
     *      type (string): type of measurement.
     *      context (obj): object with info to send to Google Analytics.
     */
    function _postData (type, context) {
        var content = {
            v: 1,
            t: type,
            tid: trackerId,
            cid: _getClientId(),
            uid: _getUserId(),
        };

        for (var attrName in context) {
            content[attrName] = context[attrName];
        }

        var content = querystring.stringify(content);
        console.log('Post to GA: ' + content);

        new Request({
            url: googleAnalyticsUrl,
            content: content
        }).post();
    }
    updateChannels: async(function*(channels) {
        let response = yield promisedPaginationHelper({
            url: baseUrl + "users?" + qs.stringify({
                ids: channels.map((ch) => ch.login).join(","),
                fields: USER_FIELDS,
                limit: 100
            })+"&page=",
            pageSize: 1,
            initialPage: 1,
            request: (url) => this._qs.queueRequest(url),
            fetchNextPage(data) {
                return data.json && data.json.has_more;
            },
            getItems(data) {
                if(data.json && data.json.list)
                    return data.json.list;
                else
                    return [];
            }
        });

        return Promise.all(response.map((ch) => this._getStreamDetailsForChannel(getChannelFromJSON(ch))));
    }),
Ejemplo n.º 23
0
const getRequest = (type, url)  => {
    if(type === "youtube") {
        const u = url.split("?");
        u[1] = qs.parse(u[1]);
        delete u[1].part;
        if("fields" in u[1])
            delete u[1].fields;
        if("hl" in u[1])
            delete u[1].hl;
        if("relevanceLanguage" in u[1])
            delete u[1].relevanceLanguage;
        delete u[1].key;

        u[1] = qs.stringify(u[1]);

        url = u.join("?");
    }
    else if(type == "douyutv") {
        url = url.split("?")[0];
    }

    console.log("Getting", url);
    if(type in mockAPIEnpoints && url in mockAPIEnpoints[type]) {
        return {
            status: 200,
            json: mockAPIEnpoints[type][url],
            text: typeof mockAPIEnpoints[type][url] === "string" ?
                mockAPIEnpoints[type][url] :
                JSON.stringify(mockAPIEnpoints[type][url])
        };
    }
    else {
        return {
            status: 404
        };
    }
};
Ejemplo n.º 24
0
 _getChannelById: async(function*(channelId) {
     let data = yield this._qs.queueRequest(baseURL+"channels?"+querystring.stringify(
         {
             "part": "snippet",
             "id": channelId,
             "fields": "items(snippet/title,snippet/thumbnails)",
             "key": apiKey
         }), headers);
     if(data.json && data.json.items && data.json.items.length) {
         var ch = new Channel(channelId, this._type);
         ch.url.push("https://youtube.com/channel/"+ch.login+"/live");
         ch.url.push("https://gaming.youtube.com/channel/"+ch.login+"/live");
         ch.archiveUrl = "https://youtube.com/channel/"+ch.login+"/videos";
         ch.chatUrl = "https://youtube.com/channel/"+ch.login+"/discussion";
         ch.image = { "88": data.json.items[0].snippet.thumbnails.default.url,
                      "240": data.json.items[0].snippet.thumbnails.high.url
                    };
         ch.uname = data.json.items[0].snippet.title;
         return ch;
     }
     else {
         throw "Getting channel details failed: "+data.status;
     }
 }),
Ejemplo n.º 25
0
    spec.filter = function (originalURL) {
        let rules = spec.rules.get(originalURL.spec);
        if (!rules["set_query_parameter"]) {
            return originalURL; // nothing to modify
        }

        let newURI = urlUtils.newURI(originalURL.spec);
        let newURL = newURI.QueryInterface(Ci.nsIURL);
        let parsedQuery = querystring.parse(newURL.query) || {};
        let modified = false; // was anything modified?
        let params = rules["set_query_parameter"];

        Object.keys(params).forEach(function (name) {
            if (params[name] === null) {
                // deleting
                if (name in parsedQuery) {
                    modified = true;
                    delete parsedQuery[name];
                }
            } else {
                // adding - check if it already existed with the same value
                // so it's possible to tell if the URL was modified.
                if (parsedQuery[name] !== params[name]) {
                    modified = true;
                    parsedQuery[name] = params[name];
                }
            }
        });

        if (modified) {
            newURL.query = querystring.stringify(parsedQuery);
            return newURL;
        } else {
            return originalURL;
        }
    };
Ejemplo n.º 26
0
 let streamIds = yield Promise.all(channels.map((channel) => {
     return this._qs.queueRequest(baseURL+"search?"+querystring.stringify(
         {
             "part": "id",
             "channelId": channel.login,
             "fields": "items(id/videoId)",
             "maxResults": 1,
             "eventType": "live",
             "type": "video",
             "key": apiKey
         }), headers).then(function(response) {
         if(response.json && response.json.items) {
             if(response.json.items.length) {
                 return response.json.items[0].id.videoId;
             }
             else {
                 channel.live.setLive(false);
                 channel.url = [ "https://youtube.com/channel/"+channel.login ];
                 return null;
             }
         }
         // silently ignore errors
     });
 }));
Ejemplo n.º 27
0
	return function(req) {
		if (req.module === 'internal') {
			// these methods are not part of the public Fathom API and only
			// available to the addon pages (./data/*)

			console.info("internal method called by " + worker.tab.url +
			" method=" + req.method +
			" winid=" + winid);

			switch (req.method) {
				case 'init':
				// parse and validate the requested manifest
				var manifest = sec.parseManifest(req.params);
				if (manifest.error) {
					// problem with the manifest
					return sendresp(req, manifest, true);
				}

				// window information
				manifest.winid = winid;
				manifest.taburl = worker.tab.url;
				manifest.tabtitle = worker.tab.title;

				var handleuserres = function(userok) {
					if (!userok) {
						ss.storage['fathom']['webpage_denied'] += 1;
						return senderrresp(req,"notallowed");
					}

					pageworkers[winid] = {
						manifest : manifest,
						created : new Date().getTime()
					};

					return sendresp(req, {manifest : manifest}, true);
				};

				if (!manifest.isaddon) {
					// webpage is requesting fathom APIs - ask the user
					ss.storage['fathom']['webpage'] += 1;
					dialogs.showSecurityDialog(handleuserres, manifest);

				} else {
					if (manifest.taburl.indexOf('monitoring.html')>=0) {
						manifest.tool = 'baseline';
						ss.storage['fathom']['baseline'] += 1;
					} else if (manifest.taburl.indexOf('debugtool.html')>=0) {
						manifest.tool = 'debugtool';
						ss.storage['fathom']['debugtool'] += 1;
					} else if (manifest.taburl.indexOf('homenet.html')>=0) {
						manifest.tool = 'homenet';
						ss.storage['fathom']['homenet'] += 1;
					}

					// no security dialog on addon pages - always allow
					handleuserres(true);
				}
				break;

				case 'close':
				if (pageworkers[winid]) {
					if (socketapi)
						socketapi.windowclose(winid);
					delete pageworkers[winid];
				}

				sendresp(req,true,true);
				break;

				case 'upload':
				// tools page uploading data
				var obj = req.params;
				var manifest = pageworkers[winid].manifest;
				dialogs.showUploadDialog(function(userok) {
					if (userok) {
						upload.addUploadItem(manifest.tool, obj);
					}
					sendresp(req,userok,true);
				}, manifest.tool);
				break;

				case 'getuserpref':
				// various tools may ask for user prefs
				if (_.isArray(req.params)) {
					sendresp(req,
						_.map(req.params,
							function(p) { return userPrefs[p]; }),
							true);
						} else {
							sendresp(req,userPrefs[req.params],true);
						}
						break;

						case 'getjson':
						// TODO: offer as public API ?
						baselineapi.exec(function(res) {
							sendresp(req,res,true);
						}, req);
						break;

						case 'setenvlabel':
						// name env with user label
						env.setenvlabel(function(res) {
							sendresp(req,res,true);
						}, req);
						break;

						case 'setuserpref':
						// from welcome.js
						var obj = req.params;

						if (obj[consts.BASELINE_UPLOAD] !== undefined) {
							userPrefs[consts.BASELINE_UPLOAD] = (obj[consts.BASELINE_UPLOAD] ? consts.UPLOAD_ALWAYS : consts.UPLOAD_NEVER);
						}
						if (obj[consts.PAGELOAD_UPLOAD] !== undefined) {
							userPrefs[consts.PAGELOAD_UPLOAD] = (obj[consts.PAGELOAD_UPLOAD] ? consts.UPLOAD_ALWAYS : consts.UPLOAD_NEVER);
						}
						if (obj[consts.MEDIALOAD_UPLOAD] !== undefined) {
							userPrefs[consts.MEDIALOAD_UPLOAD] = (obj[consts.MEDIALOAD_UPLOAD] ? consts.UPLOAD_ALWAYS : consts.UPLOAD_NEVER);
						}
						if (obj.enablebaseline !== undefined)
						userPrefs[consts.BASELINE] = obj.enablebaseline;
						if (obj.enablepageload !== undefined)
						userPrefs[consts.PAGELOAD] = obj.enablepageload;
						if (obj.enablemediaload !== undefined)
						userPrefs[consts.MEDIALOAD] = obj.enablemediaload;
						if (obj.enablefathomapi !== undefined)
						userPrefs[consts.FATHOMAPI] = obj.enablefathomapi;

						sendresp(req,true,true);
						break;

						case 'forceupload':
						// add a stats object to the queue for debugging
						var ts = new Date();
						upload.addUploadItem("fathomstats", {
							ts : ts.getTime(),
							timezoneoffset : ts.getTimezoneOffset(),
							action : "debugupload",
							stats : {
								'fathom' : ss.storage['fathom'],
								'security' : ss.storage['security'],
								'baseline' : ss.storage['baseline'],
								'upload' : ss.storage['upload']
							}
						}, function() {
							upload.uploadItems();
							sendresp(req,true,true);
						});
						break;

						case 'purgeupload':
						upload.purgeUploadItems();
						sendresp(req,true,true);
						break;

						case 'getstats':
						// from debug.js: get stats
						var stats = {
							'instance' : {
								'fathom_version' : self.version,
								'fathom_uuid' : ss.storage['uuid'],
								'fathom_public_uuid' : ss.storage['public_uuid'],
								'fathom_config_version' : ss.storage['config']['config']['version'],
								'fathom_config_updated' : ss.storage['config']['config']['last_update']
							},
							'fathom' : ss.storage['fathom'],
							'security' : ss.storage['security'],
							'baseline' : ss.storage['baseline'],
							'upload' : ss.storage['upload']
						};
						sendresp(req,stats,true);
						break;

						case 'getwhitelist':
						// from whitelist.js: get whitelist
						var blacklist = ss.storage['blacklist'] || [];
						var res = _.map(config.get('pageload', 'whitelist'), function(v) {
							return { host : v, disabled : _.contains(blacklist, v) };
						});
						sendresp(req,res,true);
						break;

						case 'setwhitelist':
						// from whitelist.js: remove selected entries from whitelist
						var blacklist = [];
						_.each(req.params, function(v) {
							if (v.disabled)
							blacklist.push(v.host)
						});
						ss.storage['blacklist'] = blacklist;
						sendresp(req,true,true);
						break;

						case 'debugtool':
						// user requests fathom debugtool, passing the
						// neterror querystring on to the debugtool
						// for custom tests
						var toolurl = consts.DEBUGURL;
						if (req && req.params.indexOf('extensions-dummy')<0) {
							let q = qstring.parse(req.params.split('?')[1]);
							let u = new url.URL(q.u);
							console.log(u);
							toolurl += '?' + qstring.stringify({
								protocol:u.protocol.replace(':',''),
								hostname:u.hostname,
								pathname:u.pathname
							});
							console.log(toolurl);
						}
						tabs.open({	url :  toolurl });
						ss.storage['fathom']['debugtool_neterror'] += 1;
						break;

						default:
						console.warn("fathom no such method: internal." + req.method);
						senderrresp(req,"nosuchmethod",req.method);
					}

				} else if (pageworkers[winid]) {
					// API method call
					var manifest = pageworkers[winid].manifest;
					pageworkers[winid].lastactive = new Date().getTime();

					console.info("'" + req.module + "." + req.method +
					"' called by window=" + winid +
					", reqid=" + req.id +
					", isaddon="+manifest.isaddon);

					var cb = function(res, done) {
						return sendresp(req, res, done);
					};

					if (req.module.indexOf('.')>=0) {
						let tmp = req.module.split('.');
						req.module = tmp[0];    // main namespace
						req.submodule = tmp[1]; // subnamespace
					}

					switch (req.module) {
						case 'system':
						systemapi.exec(cb, req, manifest);
						break;

						case 'baseline':
						baselineapi.exec(cb, req, manifest);
						break;

						case 'socket':
						socketapi.exec(cb, req, manifest);
						break;

						case 'proto':
						protoapi.exec(cb, req, manifest);
						break;

						case 'tools':
						toolsapi.exec(cb, req, manifest);
						break;

						default:
						senderrresp(req,"nosuchmodule",req.module);
					}

				} else {
					return senderrresp(req,"noinit",worker.tab.url);
				}
			}; // handlefunc
        ch.archiveUrl = json.url;
    }
    ch.uname = json.screenname;
    ch.image = AVATAR_SIZES.reduce((p, c) => {
        p[c] = json['avatar_'+c+'_url'];
        return p;
    }, {});

    return ch;
};

const Dailymotion = newClass({
    extends: GenericProvider,
    _getChannelByID(id, doUser = false) {
        return this._qs.queueRequest(baseUrl + "user/" + id + "?" + qs.stringify({
            fields: USER_FIELDS
        })).then((result) => {
            if(result.status == 200 && result.json) {
                if("list" in result.json)
                    return getChannelFromJSON(result.json.list[0], doUser);
                else
                    return getChannelFromJSON(result.json, doUser);
            }
            else
                throw "Could not get details for " + id + " on " + this._type;
        });
    },
    _getStreamDetailsForChannel(channel) {
        return this._qs.queueRequest(baseUrl + "user/" + channel.login + "/videos?" + qs.stringify({
            id: channel.login,
            fields: "chat_embed_url,title,url,channel.name,onair,thumbnail_240_url",
Ejemplo n.º 29
0
    _getCategory: memoize(async(function*(categoryId) {
        console.info(this.name+"._getCategory(", categoryId, ")");
        let data = yield this._qs.queueRequest(baseURL + "videoCategories?" + querystring.stringify({
            "part": "snippet",
            "id": categoryId,
            "hl": getLocale(),
            "key": apiKey
        }), headers);

        if(data.json && "items" in data.json && data.json.items.length) {
            return data.json.items[0].snippet.title;
        }
        else {
            return '';
        }
    }), (id) => id+"|"+getLocale()),
Ejemplo n.º 30
0
    getUserFavorites: async(function*(username) {
        let data = yield this._qs.queueRequest(baseURL+"channels?"+querystring.stringify(
            {
                "part": "id,snippet",
                "forUsername": username,
                "fields": "items(id,snippet/title,snippet/thumbnails)",
                "key": apiKey
            }), headers);

        if(data.json && data.json.items && data.json.items.length) {
            var ch = new User(data.json.items[0].id, this._type), page = 0,
            subsOptions = {
                "part": "snippet",
                "channelId": data.json.items[0].id,
                "maxResults": 50,
                "key": apiKey
            };
            ch.image = { "88": data.json.items[0].snippet.thumbnails.default.url,
                         "240": data.json.items[0].snippet.thumbnails.high.url
                       };
            ch.uname = data.json.items[0].snippet.title;

            let subscriptions = yield promisedPaginationHelper({
                url: baseURL+"subscriptions?"+querystring.stringify(subsOptions),
                pageSize: subsOptions.maxResults,
                initialPage: "",
                request: (url) => {
                    return this._qs.queueRequest(url, headers);
                },
                getPageNumber: function(page, pageSize, data) {
                    return data.json.nextPageToken;
                },
                fetchNextPage: function(data) {
                    return data.json && data.json.items && data.json.pageInfo.totalResults > data.json.pageInfo.resultsPerPage*++page;
                },
                getItems: function(data) {
                    if(data.json && data.json.items)
                        return data.json.items;
                    else
                        return [];
                }
            });

            if(subscriptions.length) {
                ch.favorites = subscriptions.map((sub) => sub.snippet.resourceId.channelId);

                let channels = subscriptions.map((sub) => {
                    var ret = new Channel(sub.snippet.resourceId.channelId, this._type);
                    ret.archiveUrl = "https://youtube.com/channel/"+ret.login+"/videos";
                    ret.chatUrl = "https://youtube.com/channel/"+ret.login+"/discussion";
                    ret.image = { "88": sub.snippet.thumbnails.default.url,
                                 "240": sub.snippet.thumbnails.high.url };
                    ret.uname = sub.snippet.title;
                    return ret;
                });

                return [ ch, channels ];
            }
            else {
                /** @todo Sometimes needs oAuth for some reason, I guess privacy
                  * settings. This also triggers when the user follows noone. */
                throw "Can't get favorites for youtube user "+username+" without oAuth as somebody with reading rights of this user's subs.";
            }
        }
        else {
            throw "Error getting details for youtube user "+username;
        }
    }),