InternetArchive
public class InternetArchive : InternetArchiveProtocol
Interact with the InternetArchive API
Example Usage
let query = InternetArchive.Query(
clauses: ["collection": "etree", "mediatype": "collection"])
let archive = InternetArchive()
archive.search(
query: query,
page: 0,
rows: 10,
completion: { (response: InternetArchive.SearchResponse?, error: Error?) in
// handle response
})
archive.itemDetail(
identifier: "sci2007-07-28.Schoeps",
completion: { (item: InternetArchive.Item?, error: Error?) in
// handle item
})
-
Declaration
Swift
public convenience init() -
Declaration
Swift
public init(urlGenerator: InternetArchiveURLGeneratorProtocol, urlSession: URLSession) -
Search the Internet Archive
Declaration
Swift
public func search(query: InternetArchiveURLStringProtocol, page: Int, rows: Int, fields: [String]? = nil, sortFields: [InternetArchiveURLQueryItemProtocol]? = nil, completion: @escaping (InternetArchive.SearchResponse?, Error?) -> Void)Parameters
queryThe search query as an
InternetArchiveURLStringProtocolobjectpageThe results pagination page number
rowsThe number of results to return per page
fieldsAn array of strings specifying the metadata entries you want returned. The default is
nil, which return all metadata fieldssortFieldsThe fields by which you want to sort the results as an
InternetArchiveURLQueryItemProtocolobjectcompletionReturns optional
InternetArchive.SearchResponseandErrorobjects -
Fetch a single item from the Internet Archive
Declaration
Swift
public func itemDetail(identifier: String, completion: @escaping (InternetArchive.Item?, Error?) -> Void)Parameters
identifierThe item identifier
completionReturns optional
InternetArchive.ItemandErrorobjectsReturn Value
No value
-
Declaration
Swift
public func generateItemImageUrl(itemIdentifier: String) -> URL? -
Declaration
Swift
public func generateMetadataUrl(identifier: String) -> URL? -
Declaration
Swift
public func generateDownloadUrl(itemIdentifier: String, fileName: String) -> URL?
-
Errors that may be returned by the InternetArchive class
See moreDeclaration
Swift
public enum InternetArchiveError : Error
-
The main structure for defining search queries.
It can be created with
key: valuepairs like["collection": "etree"]or an array of query clauses. The output is a URL string, such as"collection:(etree) AND -title:(foo)"inasURLString.Basic Usage:
// generate a query for items in the etree collection let query = InternetArchive.Query(clauses: ["collection": "etree"]) // generate a query for items not in the etree collection let query = InternetArchive.Query(clauses: ["-collection": "etree"]) // generate a query for any field with a value of etree let query = InternetArchive.Query(clauses: ["": "etree"])Advanced Usage:
See morelet clause1 = InternetArchive.QueryClause(field: "title", value: "String Cheese", booleanOperator: .and) let clause2 = InternetArchive.QueryClause(field: "foo", value: "bar", booleanOperator: .not) let dateInterval = DateInterval(start: startDate, end: endDate) let dateRangeClause = InternetArchive.QueryDateRange(queryField: "date", dateRange: dateInterval) let query = InternetArchive.Query(clauses: [clause1, clause2, dateRangeClause, sortField])Declaration
Swift
public struct Query : InternetArchiveURLStringProtocol -
A query clause for use in generating a
Query.This is comprised of a
field,value, andbooleanOperator. It will generate a search clause likecollection:(etree).fieldcan be empty to search any fieldExample Usage:
See morelet clause1 = InternetArchive.QueryClause(field: "foo", value: "bar", booleanOperator: .and) let clause2 = InternetArchive.QueryClause(field: "bar", value: "foo", booleanOperator: .not)Declaration
Swift
public struct QueryClause : InternetArchiveQueryClauseProtocol -
A query clause for use in generating a date range query.
This is comprised of a
fieldanddateRange. It will return a query likedate:[2018-01-01T07:23:12Z TO 2018-04-01T17:53:34Z]Example Usage:
See morelet startDate = Date(timeIntervalSince1970: 0) let endDate = Date() let dateInterval = DateInterval(start: startDate, end: endDate) let dateRangeClause = InternetArchive.QueryDateRange(queryField: "date", dateRange: dateInterval)Declaration
Swift
public struct QueryDateRange : InternetArchiveQueryClauseProtocol -
Declaration
Swift
public enum BooleanOperator : String
-
A query item for use in generating a sort field.
This is comprised of a
fieldanddirection.directionis either.ascor.descExample Usage:
See morelet sortField = InternetArchive.SortField(field: "date", direction: .asc)Declaration
Swift
public struct SortField : InternetArchiveURLQueryItemProtocol -
Declaration
Swift
public enum SortDirection : String
-
The top-level response from a search request
See moreDeclaration
Swift
public struct SearchResponse : Decodable -
The response headers from a search request
See moreDeclaration
Swift
public struct ResponseHeader : Decodable -
The response from a search request, containing the search results (
See moredocs)Declaration
Swift
public struct Response : Decodable -
The response parameters from a search request
This contains the query information that you sent in the search request.
See moreDeclaration
Swift
public struct ResponseParams : Decodable
-
Declaration
Swift
public class URLGenerator : InternetArchiveURLGeneratorProtocol
-
An abstraction for Internet Archive-style metadata fields.
Internet Archive metadata fields can be stored as strings or an array of strings. Typically we want to use these fields in a native types (
Int,Double,Date,URL, etc). TheModelFieldstruct does a few things to make handling these values more convenient:- Provides a generic interface to any native type that is parsable from a string
- Converts the fields from strings to their native type
- Normalizes the response to an array of objects
- Provides a convenience
valueaccessor to get the first value of the array since most fields are single values
Native types are wrapped in
ModelFieldProtocolobjects likeIAIntandIADateto handle the string to native conversion.Example Usage:
See morestruct Foo: Decodable { let foo: ModelField<IAInt> let bar: ModelField<IAString> } let json: String = "{ \"foo\": \"3\", \"bar\": [\"boop\", \"bop\"] }" let data = json.data(using: .utf8)! let results: Foo = try! JSONDecoder().decode(Foo.self, from: data) results.foo.values => [3] results.foo.value => 3 results.bar.values => ["boop", "bop"] results.bar.value => "boop"Declaration
Swift
public struct ModelField<T> : Decodable where T : ModelFieldProtocol
-
Internet Archive
IntfieldExample Usage
See morelet intField = IAInt(fromString: "3") intField.value => 3Declaration
Swift
public class IAInt : ModelFieldProtocol -
Internet Archive
StringfieldExample Usage
See morelet stringField = IAInt(fromString: "Foo") stringField.value => "Foo"Declaration
Swift
public class IAString : ModelFieldProtocol -
Internet Archive
DoublefieldExample Usage
See morelet doubleField = IADouble(fromString: "13.54") doubleField.value => 13.54Declaration
Swift
public class IADouble : ModelFieldProtocol -
Internet Archive
BoolfieldExample Usage
See morelet boolField = IABool(fromString: "true") boolField.value => trueDeclaration
Swift
public class IABool : ModelFieldProtocol -
Internet Archive
URLfieldExample Usage
See morelet urlField = IAURL(fromString: "https://archive.org") urlField.value => URL "https://archive.org"Declaration
Swift
public class IAURL : ModelFieldProtocol -
Internet Archive
DatefieldParses the following formats:
- ISO8601 (
2018-11-15T08:23:41Z,2018-11-15T08:23:41-07:00, etc)Date
Date Time (2018-03-25 14:51:24)Date
Date (2018-09-03)- Year (
2018)- Year Month (
2018-09)- Approximate Year (
[2018])- Circa Year (
c.a. 2018)Note: The
approximate
andcirca
formats do not have a representation that they’re approximate, since theDatetype has no way of representing it.Example Usage
See morelet dateField = IADate(fromString: "2018-11-15T08:23:41Z") dateField.value => Date "2018-11-15T08:23:41Z"Declaration
Swift
public class IADate : ModelFieldProtocol - ISO8601 (
-
Internet Archive
TimeIntervalfield. Used for fields likelengthof an audio file.Parses the following formats:
- Seconds.Milliseconds (
323.4) - Duration (
5:23.4)
Example Usage
See morelet timeIntervalField1 = IATimeInterval(fromString: "12:37.4") timeIntervalField1.value => TimeInterval 757.4 let timeIntervalField2 = IATimeInterval(fromString: "526.7") timeIntervalField2.value => TimeInterval 526.7Declaration
Swift
public class IATimeInterval : ModelFieldProtocol - Seconds.Milliseconds (
-
An Internet Archive File
This will be returned in the
filesproperty from anInternetArchive().itemDetail()request.Note: The properties are all type
ModelField<T>exceptname, which is aString. This means you need to access all values by their.valueor.valuesproperties, except foridentifier, which you can access directly.Some Background: All other fields can be a string or array of strings so we can’t access them directly. See the
ModelFieldclass for a more thorough explanation.For example:
let file = File(...some file...) file.name = "SCIRedRocksConcert.track1.mp3" // `name` is always a String, it's like the primary key for the file file.length.value = TimeInterval object // we want to cast all other fields to their native typeSee the Internet Archive’s Python API Reference for a description of the properties.
Note: This is not an exhaustive list of properties. If you need some that are missing, please open a pull request.
See moreDeclaration
Swift
public struct File : Decodable
-
An Internet Archive Item, containing
ItemMetadata, an array ofFileobjects, and additional properties.This will be returned from an
See moreInternetArchive().itemDetail()request.Declaration
Swift
public struct Item : Decodable
-
Internet Archive Item Metadata
This will be returned from
itemDetail()andsearch()requests.Note: The properties are all type
ModelField<T>exceptidentifier, which is aString. This means you need to access all values by their.valueor.valuesproperties, except foridentifier, which you can access directly.Some Background: All other fields can be a string or array of strings so we can’t access them directly. See the
ModelFieldclass for a more thorough explanation.For example:
let metadata = ItemMetadata(...some metadata...) metadata.identifier = "SCIRedRocksConcert" // `identifier` is always a String metadata.venue.value = "Red Rocks" // other fields can be a string or array of strings so you can't access directlySee the Internet Archive’s Python API Reference for a description of the properties.
Note: This is not an exhaustive list of metadata properties. If you need some that are missing, please open a pull request.
See moreDeclaration
Swift
public struct ItemMetadata : Decodable
View on GitHub
InternetArchive Class Reference