Skip to content

Instantly share code, notes, and snippets.

@johanhermansson
Created August 7, 2019 07:04
Show Gist options
  • Save johanhermansson/aca79bd3d1171cbce50960065fa9ea74 to your computer and use it in GitHub Desktop.
Save johanhermansson/aca79bd3d1171cbce50960065fa9ea74 to your computer and use it in GitHub Desktop.
Import Letterboxd scores into Taste.io

Letterboxd -> Taste.io import script

This script imports your Letterboxd ratings into taste.io.

Lazy update of rahatarmanahmed Gist: https://gist.github.com/rahatarmanahmed/87acb61c9922ecec967380552d03d117

;)

Usage

Too lazy to make this into an npm package so here's how you could use it. You'll need nodejs and npm.

  1. Save the script
  2. Run npm i through2 JSONStream got cookie x-ray in the script's directory
  3. Login to taste.io and go to your home page. Open your browser's developer tools to the network tab. Refresh the page and look for the request to the /me endpoint. Copy the Authorization token from the headers.
  4. Edit the script at the top with your own letterboxd username and taste.io token.
  5. Run node letterboxd2taste.js
  6. cool ur done now
'use strict'
// CONFIGURE YOUR SETTINGS HERE
// Letterboxd username
const LETTERBOXD_USERNAME = 'oan'
// Which page should the script start on?
// (if the importer fails somewhere)
const LETTERBOXD_START_PAGE = 0
// Taste.io Authorization token
// Too lazy to impl auth considering my account is facebook oauth'd
const TOKEN = ''
// Maps letterboxd 10 point rating scale to taste.io 4 point scale.
// Adjust if desired
const mapRating = function(rating) {
// 1 2 3 4 5 6 7 8, 9, 10
return [null, 1, 1, 1, 2, 2, 3, 3, 4, 4, 4][rating]
}
// Change to true if ratings should be updated
const UPDATE_RATINGS = false
// =================================================================
// DOING A CODE HERE DON'T BE LOOKIN DOWN THERE LESS U LOOKIN FOR IT
const through2 = require('through2')
const JSONStream = require('JSONStream')
const got = require('got')
const cookie = require('cookie')
const Xray = require('x-ray')
const x = Xray()
const serializeCookie = function(obj) {
return Object.keys(obj).map((key) => cookie.serialize(key, obj[key])).join('; ')
}
// Scrape my letterboxd watchlist
const pageParam = (LETTERBOXD_START_PAGE > 0) ? `page/${LETTERBOXD_START_PAGE}/` : ''
const url = `http://letterboxd.com/${LETTERBOXD_USERNAME}/films/${pageParam}`
x(url, '.film-list', x('.poster-container', [{
rating: '@data-owner-rating',
title: '.film-poster img@alt',
}]))
.paginate('.paginate-nextprev .next@href')
.write()
// Stream the scraped titles one by one
.pipe(JSONStream.parse('*'))
.pipe(through2.obj(function({title, rating}, enc, cb){
if(rating == 0) {
console.log(`Skipping ${title}`)
return cb(null)
}
console.log(`Rating ${title} as ${rating} -> ${mapRating(rating)}...`)
got('https://www.taste.io/api/movies/search?q=' + encodeURIComponent(title), { headers: {
Authorization: 'Bearer ' + TOKEN
}, json: true })
.then(res => {
if(!res.body.movies.length) return
const movie = res.body.movies[0]
if(!UPDATE_RATINGS && movie.user && movie.user.rating) {
console.log(`Skipping ${title} (already rated)`)
return
}
const cook = cookie.parse(res.headers['set-cookie'].join('; '))
return new Promise(resolve => {
const req = got.post('https://www.taste.io/api/movies/' + movie.slug + '/rating', {
headers: {
Authorization: 'Bearer ' + TOKEN,
Cookie: serializeCookie(cook),
'X-XSRF-TOKEN': cook['XSRF-TOKEN']
},
body: {
rating: mapRating(rating)
},
json: true
}).then(res => resolve(res)).catch(err => console.error(err, req.body))
})
})
.then(_ => {
cb(null, 'Success!\n')
})
.catch(err => { console.error(err); cb(err) })
}))
// Pipe results to stdout!
.pipe(process.stdout)
.on('error', err => console.error(err))
@pedromgapt
Copy link

yeah i can understand that =) its all good.
btw, it would be easier / i think / to use trakt to import because you can create ur own app inside your user account.

@johanhermansson
Copy link
Author

@pedromgapt Full rewrite of the script could be found here https://github.com/johanhermansson/letterboxd-exporter

It's now separated into two separated scripts, one for exporting from Letterboxd, and another to import into Taste.io

@pedromgapt
Copy link

@johanhermansson thank you =) nice late Xmas present hehe :)
happy 2020, going to test it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment