Giter Club home page Giter Club logo

legimi's Introduction

legimi

Warning

This project is no longer active, use new version written in Go.

Unofficial Legimi ebook downloader for Kindle

This is Legimi ebook downloader made for downloading ebooks for Kindle from Legimi.

It is completely unofficial. I created it to be able to download ebooks from my shelf on operating systems not supported by official app (Linux, mostly). I created it based on the traffic exchanged between official Legimi application and server. As such, it does not support many message types, errors and features and probably doesn't handle them well - use at your own risk. It only allows to download a book that is on the user's shelf.

Dependencies

  • Lua 5.3+ (may work with Lua 5.2 also, but it was not tested)
  • http
  • argparse

Installation

luarocks --lua-version <your_lua_version, e.g. 5.4> --tree .luarocks install http
luarocks --lua-version <your_lua_version, e.g. 5.4> --tree .luarocks install argparse

Usage

First you need to install dependencies as described above. Then you need to obtain Device ID associated with your Kindle and save it to deviceid file. Your Kindle should probably be activated first using official Legimi application. Easiest way to get Device ID is to use script's deviceid command and pass your Kindle's serial no. (on your Kindle, go to Settings -> Device Options -> Device Info). First set environment variables LEGIMI_LOGIN and LEGIMI_PASS with your credentials, then invoke:

lua legimi deviceid G000PP12345678XX

Device ID will be stored automatically to deviceid file. Obtaining Device ID should be one-time action.

Once you have your Device ID, you may start downloading books that are on your shelf. To download a book, you have to pass it's id number. Book's id number can be copied from book's page url or you can print a list books that are on your shelf using list command (remember to set login and password variables):

lua legimi list

Once you have book id, you can use it to download an ebook file:

lua download <id>

You can pass multiple book ids to download.

Downloaded book files are saved within script's directory using following format: <bookid>.mobi. You can copy them directly to your Kindle via USB.

Development

Written with TDD (serialization part, at least) using LuaUnit

Dependencies

  • LuaUnit
  • luacov

Installation

luarocks --lua-version 5.4 --tree .luarocks install luaunit
luarocks --lua-version 5.4 --tree .luarocks install luacov

Running tests

lua -e 'require"setup"' {-lluacov} test/suite/all.lua

-lluacov is optional for gathering coverage stats

Live tests

Tests in test/suite/live.lua are actually connecting to Legimi service. You need to set real data in test/data/init.lua before running this suite.

legimi's People

Contributors

tp86 avatar

Stargazers

monk avatar  avatar  avatar Krzysztof Wisznarewski avatar Mateusz Odelga avatar Szymon Kurdziel avatar Jakub avatar Daniel Mroczek avatar Szymon avatar Michał Włodarczyk avatar  avatar  avatar

Watchers

 avatar

legimi's Issues

Error when running deviceId

Hi, I get the following error when I run lua legimi deviceid <device-id>:

	no field package.preload['http.request']
	no file 'src/http/request.lua'
	no file 'src/http/request/init.lua'
	no file 'lib/http/request/src/http/request.lua'
	no file '.luarocks/share/lua/5.3/http/request.lua'
	no file '/usr/local/share/lua/5.3/http/request.lua'
	no file '/usr/local/share/lua/5.3/http/request/init.lua'
	no file '/usr/local/lib/lua/5.3/http/request.lua'
	no file '/usr/local/lib/lua/5.3/http/request/init.lua'
	no file '/usr/share/lua/5.3/http/request.lua'
	no file '/usr/share/lua/5.3/http/request/init.lua'
	no file './http/request.lua'
	no file './http/request/init.lua'
	no file '.luarocks/lib/lua/5.3/http/request.so'
	no file '/usr/local/lib/lua/5.3/http/request.so'
	no file '/usr/lib/x86_64-linux-gnu/lua/5.3/http/request.so'
	no file '/usr/lib/lua/5.3/http/request.so'
	no file '/usr/local/lib/lua/5.3/loadall.so'
	no file './http/request.so'
	no file '.luarocks/lib/lua/5.3/http.so'
	no file '/usr/local/lib/lua/5.3/http.so'
	no file '/usr/lib/x86_64-linux-gnu/lua/5.3/http.so'
	no file '/usr/lib/lua/5.3/http.so'
	no file '/usr/local/lib/lua/5.3/loadall.so'
	no file './http.so'

I installed the dependecies using luarocks --lua-version 5.3 --tree .luarocks install http and got info:

...
http 0.4-0 is now installed in ~/Projects/legimi/.luarocks (license: MIT)
...

I never used lua before and have no idea how to debug it (internet is not really helpful), do you know how to fix this? I don't see any .lua or .so files in the rocks folder.

Screenshot from 2024-03-02 16-13-26
Screenshot from 2024-03-02 16-12-10

Error on geting token

Hey,
i just tried your app and i got error right after connecting to legimi api. My error log is:

stack traceback:
        [C]: in function 'string.unpack'
        src/legimi.lua:71: in local 'unpackin'
        src/legimi.lua:81: in upvalue 'parseresponseparts'
        src/legimi.lua:107: in local 'gettoken'
        src/legimi.lua:160: in main chunk
        [C]: in ?

It seems like the response after trying to get token is bad. The login and password are correct. What am i doing wrong?

Downloaded books are not in *mobi format

I'm trying to use Your application along w/ Kindle Paperwhite 3. While using Windows client (latest from Legimi web page) ebooks placed on Kindle are in correct *.mobi format (w/ encryption):

$ file /media/fidor/Kindle/documents/1018717.azw 
/media/fidor/Kindle/documents/1018717.azw: Mobipocket E-book "Takeshi 3. Pa\305\202ac umar\305\202ych", 1143574 bytes uncompressed, version 6, codepage 65001, encrypted (type 2)

However when I'm trying to use Your app, the files I'm getting are in different format (probably native Legimi encryption for Android/Pocketbook - as header look similiar) - which are not recognized by Kindle:

$ file 1018717.mobi 
1018717.mobi: data

I'm 100% sure I'm using correct device id. I've started comparing mitmproxy dumps from the lua app and from Legimi app and there one difference that look interesting.

Whenever Legimi App is making the request to fetch the book

0000000000 11 00 00 00 c8 00 40 00 00 00 5d 8b 0f 00 00 00   ......@...].....
0000000010 00 00 02 00 00 00 00 00 00 00 36 30 30 35 62 37   ..........6005b7
0000000020 34 39 35 64 65 66 34 37 61 32 39 39 61 38 30 37   495def47a299a807
0000000030 38 65 37 64 39 33 64 39 34 66 00 00 ff ff ff ff   8e7d93d94f......
0000000040 ff ff ff ff 01 00 00 00 00 00                     ..........

The response from legimi server is:

0000000000 11 00 00 00 18 00 e5 08 00 00 00 01 00 00 00 01   ................
0000000010 00 00 00 04 04 00 02 00 08 00 00 00 08 91 16 00   ................
0000000020 00 00 00 00 03 00 3a 00 00 00 00 00 00 08 91 16   ......:.........
0000000030 00 1e 00 00 00 2f 2f 38 2f 2f 65 38 39 37 38 61   .....//8//e8978a
0000000040 39 61 32 65 34 66 65 65 31 63 34 36 38 35 2e 6d   9a2e4fee1c4685.m
0000000050 6f 62 69 10 f2 37 08 1f 03 ed af f0 3b c7 d3 23   obi..7......;..#
0000000060 ea be 7e bd 01 00 19 00 00 00 65 38 39 37 38 61   ..~.......e8978a
0000000070 39 61 32 65 34 66 65 65 31 63 34 36 38 35 2e 6d   9a2e4fee1c4685.m
0000000080 6f 62 69 00 00 56 08 00 00 68 74 74 70 3a 2f 2f   obi..V...http://
0000000090 61 70 70 2e 6c 65 67 69 6d 69 2e 70 6c 2f 73 76   app.legimi.pl/sv
00000000a0 63 2f 73 79 6e 63 2f 6d 6f 62 69 64 6f 77 6e 6c   c/sync/mobidownl
00000000b0 6f 61 64 2e 61 73 70 78 3f 72 3d 65 79 4a 68 62   oad.aspx?r=eyJhb
00000000c0 47 63 69 4f 69 4a 6f 64 48 52 77 4f 69 38 76 64   GciOiJodHRwOi8vd
00000000d0 33 64 33 4c 6e 63 7a 4c 6d 39 79 5a 79 38 79 4d   3d3LnczLm9yZy8yM

When using Your script...

$ lua legimi download 1018717

the request packet look similar (minus auth. cookie at the end):

0000000000 11 00 00 00 c8 00 40 00 00 00 5d 8b 0f 00 00 00   ......@...].....
0000000010 00 00 02 00 00 00 00 00 00 00 36 39 64 32 35 63   ..........69d25c
0000000020 34 61 66 61 65 61 34 64 65 61 62 30 61 64 32 65   4afaea4deab0ad2e
0000000030 37 38 64 32 64 61 33 34 34 65 00 00 ff ff ff ff   78d2da344e......
0000000040 ff ff ff ff 01 00 00 00 00 00                     ..........

The response from Legimi points to different URL (epub instead of mobi and streameddownload.aspx instead of mobidownload.aspx):

0000000000 11 00 00 00 18 00 ae 07 00 00 00 01 00 00 00 01   ................
0000000010 00 00 00 04 04 00 02 00 08 00 00 00 da db 23 00   ..............#.
0000000020 00 00 00 00 03 00 2a 00 00 00 00 00 00 da db 23   ......*........#
0000000030 00 1e 00 00 00 2f 2f 31 2f 2f 65 38 39 37 38 61   .....//1//e8978a
0000000040 39 61 32 65 34 66 65 65 31 63 34 36 38 35 2e 65   9a2e4fee1c4685.e
0000000050 70 75 62 00 01 00 19 00 00 00 65 38 39 37 38 61   pub.......e8978a
0000000060 39 61 32 65 34 66 65 65 31 63 34 36 38 35 2e 65   9a2e4fee1c4685.e
0000000070 70 75 62 00 00 2f 07 00 00 68 74 74 70 3a 2f 2f   pub../...http://
0000000080 61 70 70 2e 6c 65 67 69 6d 69 2e 70 6c 2f 73 76   app.legimi.pl/sv
0000000090 63 2f 73 79 6e 63 2f 73 74 72 65 61 6d 65 64 64   c/sync/streamedd
00000000a0 6f 77 6e 6c 6f 61 64 2e 61 73 70 78 3f 72 3d 65   ownload.aspx?r=e
00000000b0 79 4a 68 62 47 63 69 4f 69 4a 6f 64 48 52 77 4f   yJhbGciOiJodHRwO
00000000c0 69 38 76 64 33 64 33 4c 6e 63 7a 4c 6d 39 79 5a   i8vd3d3LnczLm9yZ
00000000d0 79 38 79 4d 44 41 78 4c 7a 41 30 4c 33 68 74 62   y8yMDAxLzA0L3htb

I can attach whole mitmproxy dump on Legimi native app (I was unable to find 1.5.0 version) as well as Yours if You wish (I'll remove password from it) but maybe its a known bug to You.

Setting device ID ends with error

If I follow readme.md I'm getting the following error:

./legimi deviceid XXYYZZAABBCCDDEE
lua: src/util.lua:3: deviceid: No such file or directory
stack traceback:
	[C]: in function 'assert'
	src/util.lua:3: in function <src/util.lua:2>
	src/config.lua:23: in field '?'
	src/config.lua:48: in metamethod '__index'
	src/flow.lua:17: in function 'flow.getsessionid'
	src/main.lua:4: in local 'withsessionid'
	src/main.lua:18: in main chunk
	[C]: in function 'require'
	./legimi:4: in main chunk
	[C]: in ?

What helps:
commenting lines in main.lua:

local commands = {
  deviceid = {
    func = flow.deviceid,
    args = { "serialno" },
  }--,
--  list = {
--    func = withsessionid(flow.listbooks),
--  },
--  download = {
--    func = withsessionid(function(sessionid, ids)
--      for _, id in ipairs(ids) do
--        flow.downloadbook(sessionid, id)
--      end
--    end),
--    args = { "id" },
--  }
}

After that I can setup deviceid and after that (after restoring above lines) I can list and download mobi files.

mobi format encryption

Since I don't own Kindle reader I'm curios:
Does downloading mobi files for Kindle device ID gets those files in clear format or those files are still encrypted and readable only by that Kindle device?

When supplying android / pocketbook devices S/N - file downloaded by your app are not plain mobi files - there seems to be some DRM on top of them.

Lua on Ubuntu: `unexpected symbol near '<'`

Hi,

I'm trying to use lua 5.3 on Ubuntu w/ Your scripts and I'm getting this error:

lua: legimi.lua:151: unexpected symbol near '<'

local file <close> = assert(io.open(bookid .. '.mobi', 'w+'))

Removing <close> from that line, moves me forward and gives me well known error regarding token (as You explained in second issue) however I'm curious (as I'm no lua expert) - what am I doing wrong? Unfortunately Google gives no answer here.

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.