Code Monkey home page Code Monkey logo

youtube-watch-history-scraper's Introduction

Youtube History Scraper

A command line scraper written in Python 2.7 using scrapy and lxml. Scrapes your YouTube History into a searchable SQLite database, so you can search for something you've previously watched. Allowing search by video length, date published, video description, video title, etc. Uses pre logged in session cookies extracted from chrome.

This is just an experimental project. The database design isn't well thought out and there are not implemented methods for getting information back out of the database.

Python library dependencies

How to use

Ensure dependencies are met:

pip install scrapy lxml sqlalchemy

Windows users: You will need to install pywin32 manually; i.e. follow the link and get the installer.

You'll need a session cookie from your web broweser, so the scraper can use your account to get your history.

Getting session cookies:

  • Install the browser extension editThisCookie.

  • Using the same browser, goto Youtube.com and ensure you are logged in.

  • Click the "editThisCookie" button, then click export. The cookies are now on you clipboard.

  • Create the file "youtube_cookies.json" in this directory, then paste and save.

Alternitive (cookies) method:

  • Open chrome / chromium

  • Make sure your logged into youtube.com

  • Open the inspector (F12)

  • Open the network tab, check the "Preserve History" box

  • Visit https://youtube.com/feed/history

  • Click the request for "/feed/history" in the network tab

  • Copy the section "Request Headers", below the title "Request Headers"

  • Paste into a new file called "youtube_request_headers.txt" in this directory

  • In the settings.py file comment the "COOKIES_JSON" line and uncomment the "CHROME_HEADERS_FILE" line.

Run Scrapy:

scrapy crawl yth_spider

Profit?

You can search it manually in your favorite database browser See DB Browser for SQLite. This way you can see the results in a table and use SQL statements to search it.

Or use ipython and sqlalchemy, this example outputs all videos > 7 minutes in length:

from db_api import *
db = AppDatabase()
with db._session_scope() as s:
	h_entries = s.query(HistoryEntry).filter(HistoryEntry.time > 7 * 60).limit(5)
for he in h_entries.all():                                            
	print "{t} (http://youtube.com/watch?v={v})".format(t=he.title, v=he.vid) 

The db_api module is just a wrapper around a sqlalchemy model and session. db._session_scope() gives you a sqlalchemy session you can run queries on, as shown above.

You can avoid the database output entirely

From the settings.py file just remove the line:

'youtube_history.pipelines.DbOutputPipeline': 901,

youtube-watch-history-scraper's People

Contributors

zvodd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.