Giter Site home page Giter Site logo

bencode's Introduction

Bencode

Bencode is a general purpose bencode encoder & decoder written in swift 4.

Inspired by VFK SwiftyBencode & arvidsigvardsson/Bencode

Installation

⚠️ Xcode 9 is required ⚠️

Bencode is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Bencode'

Usage

Initialize with URL:

let bencode: Bencode? = Bencode(file: torrentUrl)

Initialize with bencoded string:

let bencode: Bencode? = Bencode(bencodedString: content)

Would you like to know more of the parsing failures, use the decoder:

do {
    let bencode = Bencoder().decode(from: fileURL)
} catch let error {
    print(error)
}

Accessing properties:

Accessing properties is very handy with subscripts & accessors. Value accessors .int, .string, .list & .dict are optional. Subscripts produces BencodeOptional enums, by doing so you can chain optional subscripts without having to write ? or ! behind every one of them. Bencode also comfort to Sequence protocol, so you can use map, filter, foreach etc. on them. :)

let bencode = Bencode(file: torrentUrl)

let info = bencode["info"]
let files = info["files"]

// You can use them as a sequence
// .values is a shorthand for .map{ $0.value }
files.values.forEach {
    print($0["path"][0].string!)
}

// Easy optional chaning without unwapping every stage:
let filePath: String? = info["files"][0]["path"][0].string
let fileLength: Int? = bencode["info"]["files"]["length"].int

Help

  • Post any issues you find
  • Post new feature requests
  • Pull requests are welcome

Author

danieltmbr, [email protected]

License

Bencode is available under the MIT license. See the LICENSE file for more info.

bencode's People

Contributors

danieltmbr avatar douknow avatar

Watchers

James Cloos avatar  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.