├── .npmrc ├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── index.d.ts ├── test.js ├── .gitignore ├── browser.js ├── LICENSE.md ├── CHANGELOG.md ├── index.js ├── package.json ├── README.md └── CODE_OF_CONDUCT.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: weekly 7 | open-pull-requests-limit: 10 8 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare function youtubeSuggest (query: string, opts?: youtubeSuggest.SuggestOptions): Promise 2 | declare namespace youtubeSuggest { 3 | type SuggestOptions = { 4 | /** Optionally specify an ISO639-1 code to return suggestions most relevant for that language. */ 5 | locale?: string 6 | } 7 | } 8 | 9 | export = youtubeSuggest 10 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const youtubeSuggest = require('.') 4 | const assert = require('assert') 5 | 6 | youtubeSuggest('query').then(function (results) { 7 | assert(Array.isArray(results)) 8 | assert(typeof results[0] === 'string') 9 | }).catch(function (err) { 10 | console.error(err) 11 | process.exit(1) 12 | }) 13 | 14 | // Should return pet-related suggestions in the US, 15 | // but "petit xyz" in France 16 | Promise.all([ 17 | youtubeSuggest('pet', { locale: 'en-us' }), 18 | youtubeSuggest('pet', { locale: 'fr' }) 19 | ]).then(function (results) { 20 | assert.notDeepEqual(results[0], results[1]) 21 | }) 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-var */ 2 | 'use strict' 3 | 4 | var jsonp = require('smol-jsonp') 5 | 6 | // `client` is a required parameter. `client=firefox` returns the smallest result. 7 | var baseUrl = 'https://suggestqueries.google.com/complete/search?client=firefox&ds=yt' 8 | 9 | function unwrapResults (data) { return data[1] } 10 | 11 | function youtubeSuggest (query, opts) { 12 | var url = baseUrl + '&q=' + encodeURIComponent(query) 13 | if (opts && opts.locale) { 14 | url += '&hl=' + encodeURIComponent(opts.locale) 15 | } 16 | 17 | return jsonp(url) 18 | .then(unwrapResults) 19 | } 20 | 21 | module.exports = youtubeSuggest 22 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # [Apache License 2.0](https://spdx.org/licenses/Apache-2.0) 2 | 3 | Copyright 2019 Renée Kooi 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | > http://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # youtube-suggest change log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## 1.2.0 8 | * Add typescript types. 9 | * Add `locale` option to request completions tailored for a specific locale. 10 | 11 | ## 1.1.2 12 | * Change user agent to get UTF-8 encoded responses. ([@Svallinn](https://github.com/Svallinn) in [#6](https://github.com/goto-bus-stop/youtube-suggest/pull/7)) 13 | 14 | ## 1.1.1 15 | * Factor out JSONP requests to a `smol-jsonp` module. 16 | * Use HTTPS. ([@Svallinn](https://github.com/Svallinn) in [#6](https://github.com/goto-bus-stop/youtube-suggest/pull/6)) 17 | 18 | ## 1.1.0 19 | * Use JSONP in the browser to bypass CORS restrictions. 20 | 21 | ## 1.0.0 22 | * Initial release. 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fetch = require('node-fetch') 4 | 5 | // `client` is a required parameter. `client=firefox` returns the smallest result. 6 | const baseUrl = 'https://suggestqueries.google.com/complete/search?client=firefox&ds=yt' 7 | const headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0' } 8 | 9 | function parseJson (res) { return res.json() } 10 | function unwrapResults (data) { return data[1] } 11 | 12 | function youtubeSuggest (query, opts) { 13 | const url = new URL(baseUrl) 14 | url.searchParams.set('q', query) 15 | if (opts && opts.locale) { 16 | url.searchParams.set('hl', String(opts.locale)) 17 | } 18 | return fetch(url, { headers }) 19 | .then(parseJson) 20 | .then(unwrapResults) 21 | } 22 | 23 | module.exports = youtubeSuggest 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "youtube-suggest", 3 | "description": "get youtube search suggestions for a query, in node.js and the browser", 4 | "version": "1.2.0", 5 | "author": "Renée Kooi ", 6 | "browser": "browser.js", 7 | "bugs": { 8 | "url": "https://github.com/goto-bus-stop/youtube-suggest/issues" 9 | }, 10 | "dependencies": { 11 | "node-fetch": "^2.6.0", 12 | "smol-jsonp": "^1.0.0" 13 | }, 14 | "devDependencies": { 15 | "standard": "^17.0.0" 16 | }, 17 | "homepage": "https://github.com/goto-bus-stop/youtube-suggest", 18 | "keywords": [ 19 | "autocomplete", 20 | "autosuggest", 21 | "completion", 22 | "search", 23 | "suggestions", 24 | "youtube" 25 | ], 26 | "license": "Apache-2.0", 27 | "main": "./index.js", 28 | "types": "./index.d.ts", 29 | "repository": { 30 | "type": "git", 31 | "url": "https://github.com/goto-bus-stop/youtube-suggest.git" 32 | }, 33 | "scripts": { 34 | "lint": "standard", 35 | "tests-only": "node test", 36 | "test": "npm run lint && npm run tests-only" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | name: Tests 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | node-version: 13 | - '4.x' 14 | - '6.x' 15 | - '7.x' 16 | - '8.x' 17 | - '9.x' 18 | - '10.x' 19 | - '11.x' 20 | - '12.x' 21 | - '13.x' 22 | - '14.x' 23 | - '15.x' 24 | - '16.x' 25 | - '17.x' 26 | - '18.x' 27 | - '19.x' 28 | - '20.x' 29 | - '21.x' 30 | - '22.x' 31 | 32 | steps: 33 | - uses: actions/checkout@v4 34 | - name: Use Node.js ${{ matrix.node-version }} 35 | uses: actions/setup-node@v4 36 | with: 37 | node-version: ${{ matrix.node-version }} 38 | - run: npm install 39 | - run: npm run tests-only 40 | 41 | lint: 42 | name: Code style 43 | runs-on: ubuntu-latest 44 | 45 | steps: 46 | - uses: actions/checkout@v4 47 | - name: Use Node.js 48 | uses: actions/setup-node@v4 49 | with: 50 | node-version: lts/* 51 | - run: npm install 52 | - run: npm run lint 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # youtube-suggest 2 | 3 | get youtube search suggestions for a query, in node.js and the browser 4 | 5 | [Install](#install) - [Usage](#usage) - [License: Apache-2.0](#license) 6 | 7 | [![npm][npm-image]][npm-url] 8 | [![ci status][actions-image]][actions-url] 9 | [![standard][standard-image]][standard-url] 10 | 11 | [npm-image]: https://img.shields.io/npm/v/youtube-suggest.svg?style=flat-square 12 | [npm-url]: https://www.npmjs.com/package/youtube-suggest 13 | [actions-image]: https://github.com/goto-bus-stop/youtube-suggest/actions/workflows/ci.yml/badge.svg 14 | [actions-url]: https://github.com/goto-bus-stop/youtube-suggest/actions/workflows/ci.yml 15 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 16 | [standard-url]: http://npm.im/standard 17 | 18 | ## Install 19 | 20 | ``` 21 | npm install youtube-suggest 22 | ``` 23 | 24 | ## Usage 25 | 26 | ```js 27 | var youtubeSuggest = require('youtube-suggest') 28 | var assert = require('assert') 29 | 30 | youtubeSuggest('query').then(function (results) { 31 | assert(Array.isArray(results)) 32 | assert(typeof results[0] === 'string') 33 | }) 34 | ``` 35 | 36 | ## API 37 | 38 | ### `youtubeSuggest(query: string, opts?: { locale?: string }): Promise` 39 | Do a network request for suggestions, returning the suggested completion strings as an array. 40 | 41 | Optionally specify an [ISO639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code to get 42 | completions intended for that locale: 43 | ```js 44 | youtubeSuggest('pet', { locale: 'en' }) // returns people named "peter", pet video titles, etc 45 | youtubeSuggest('pet', { locale: 'fr' }) // returns various small things 46 | ``` 47 | 48 | In Node.js it uses `node-fetch`. In the browser it uses JSONP. 49 | 50 | ## License 51 | 52 | [Apache-2.0](LICENSE.md) 53 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at renee@kooi.me. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [https://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: https://contributor-covenant.org 74 | [version]: https://contributor-covenant.org/version/1/4/ 75 | --------------------------------------------------------------------------------