bencoding-lens: Lenses for bencoded data.

[ bsd3, data, library ] [ Propose Tags ]

A port of lens-aeson for bencoding.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Change log CHANGELOG.md
Dependencies base (>=4.4 && <4.18), bencoding (>=0.4.5.4 && <0.5), bytestring (>=0.10.4.1 && <0.12), lens (>=4 && <5.2) [details]
License BSD-3-Clause
Copyright (c) 2021-2022, Joseph Morag
Author Joseph Morag
Maintainer Joseph Morag <jm@josephmorag.com>
Category Data
Home page https://github.com/jmorag/bencoding-lens
Bug tracker https://github.com/jmorag/bencoding-lens/issues
Source repo head: git clone https://github.com/jmorag/bencoding-lens
Uploaded by jmorag at 2022-03-10T20:25:22Z
Distributions
Downloads 83 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for bencoding-lens-0.1.0.0

[back to package description]

bencoding-lens

Law abiding lenses and traversals for bencoding, ported from lens-aeson. Works with arbitrary bencoded data, although most examples in the wild are torrent files.

{-# LANGUAGE OverloadedStrings #-}

import Control.Lens
import Data.BEncode
import Data.BEncode.Lens

import Network.HTTP.Simple -- from http-conduit
import Crypto.Hash -- from cryptonite

main = do
  -- Get the ubuntu 20.04 live server torrent file with http-conduit
  torrent <- getResponseBody <$> httpBS "https://releases.ubuntu.com/20.04/ubuntu-20.04.4-live-server-amd64.iso.torrent"
  -- Print some interesting bits of the torrent file

  -- Announce link
  print (torrent ^? key "announce") 
  -- Just (BString "https://torrent.ubuntu.com/announce")

  -- Keys of the top level dict
  print (fmap fst (torrent ^@.. members))
  -- ["announce","announce-list","comment","created by","creation date","info"]

  -- Keys of the info dict
  print (fmap fst (torrent ^@.. key "info" . members))
  -- ["length","name","piece length","pieces"]

  -- Piece length
  print (torrent ^? key "info" . key "piece length" . _BInteger)
  -- Just 262144

  -- Compute the info hash (see definition below)
  print (torrent ^?! infoHash)
  -- b44a0e20fa5b7cecb77156333b4268dfd7c30afb
  
infoHash :: (AsBValue t) => Fold t (Digest SHA1)
infoHash = key "info" . to (hashlazy . encode)