## TwitterCapture
thread-keeper
**Author**: The Harvard Library Innovation Lab
**License**: MIT
* [TwitterCapture](#utils.module_TwitterCapture)
* _static_
* [.TwitterCapture](#utils.module_TwitterCapture.TwitterCapture)
* [new exports.TwitterCapture(url, options)](#new_utils.module_TwitterCapture.TwitterCapture_new)
* [.defaults](#utils.module_TwitterCapture.TwitterCapture+defaults)
* [.options](#utils.module_TwitterCapture.TwitterCapture+options) : object
* [.url](#utils.module_TwitterCapture.TwitterCapture+url) : string
* [.urlType](#utils.module_TwitterCapture.TwitterCapture+urlType) : string
* [.playwright](#utils.module_TwitterCapture.TwitterCapture+playwright) : Object
* [.interceptedJPEGs](#utils.module_TwitterCapture.TwitterCapture+interceptedJPEGs) : object.<string, Buffer>
* [.capture](#utils.module_TwitterCapture.TwitterCapture+capture) ⇒ Promise.<Buffer>
* [.setup](#utils.module_TwitterCapture.TwitterCapture+setup) ⇒ Promise.<void>
* [.teardown](#utils.module_TwitterCapture.TwitterCapture+teardown)
* [.adjustUIForCapture](#utils.module_TwitterCapture.TwitterCapture+adjustUIForCapture) ⇒ Promise.<void>
* [.runBrowserBehaviors](#utils.module_TwitterCapture.TwitterCapture+runBrowserBehaviors) ⇒ Promise.<void>
* [.resizeViewportToFitDocument](#utils.module_TwitterCapture.TwitterCapture+resizeViewportToFitDocument) ⇒ Promise.<void>
* [.getDocumentDimensions](#utils.module_TwitterCapture.TwitterCapture+getDocumentDimensions) ⇒ Promise.<{width: number, height: number}>
* [.interceptJpegs](#utils.module_TwitterCapture.TwitterCapture+interceptJpegs) ⇒ Promise.<void>
* [.generateRawPDF](#utils.module_TwitterCapture.TwitterCapture+generateRawPDF) ⇒ Promise.<Buffer>
* [.addInterceptedJPEGsToPDF](#utils.module_TwitterCapture.TwitterCapture+addInterceptedJPEGsToPDF) ⇒ Promise.<void>
* [.captureAndAddUrlMapToPDF](#utils.module_TwitterCapture.TwitterCapture+captureAndAddUrlMapToPDF) ⇒ Promise.<void>
* [.captureAndAddVideoToPDF](#utils.module_TwitterCapture.TwitterCapture+captureAndAddVideoToPDF) ⇒ Promise.<void>
* [.cropMarginsOnPDF](#utils.module_TwitterCapture.TwitterCapture+cropMarginsOnPDF)
* [.signPDF](#utils.module_TwitterCapture.TwitterCapture+signPDF) ⇒ Buffer
* [.filterOptions](#utils.module_TwitterCapture.TwitterCapture+filterOptions)
* [.filterUrl](#utils.module_TwitterCapture.TwitterCapture+filterUrl) ⇒ bool
* _inner_
* [~URL_MAP_TEMPLATE](#utils.module_TwitterCapture..URL_MAP_TEMPLATE)
### TwitterCapture.TwitterCapture
Generates a "sealed" PDF out of a twitter.com url using Playwright.
Usage:
```
const tweet = new TwitterCapture(url);
const pdf = await tweet.capture();
fs.writeFileSync("tweet.pdf", pdf);
```
**Kind**: static class of [TwitterCapture
](#utils.module_TwitterCapture)
* [.TwitterCapture](#utils.module_TwitterCapture.TwitterCapture)
* [new exports.TwitterCapture(url, options)](#new_utils.module_TwitterCapture.TwitterCapture_new)
* [.defaults](#utils.module_TwitterCapture.TwitterCapture+defaults)
* [.options](#utils.module_TwitterCapture.TwitterCapture+options) : object
* [.url](#utils.module_TwitterCapture.TwitterCapture+url) : string
* [.urlType](#utils.module_TwitterCapture.TwitterCapture+urlType) : string
* [.playwright](#utils.module_TwitterCapture.TwitterCapture+playwright) : Object
* [.interceptedJPEGs](#utils.module_TwitterCapture.TwitterCapture+interceptedJPEGs) : object.<string, Buffer>
* [.capture](#utils.module_TwitterCapture.TwitterCapture+capture) ⇒ Promise.<Buffer>
* [.setup](#utils.module_TwitterCapture.TwitterCapture+setup) ⇒ Promise.<void>
* [.teardown](#utils.module_TwitterCapture.TwitterCapture+teardown)
* [.adjustUIForCapture](#utils.module_TwitterCapture.TwitterCapture+adjustUIForCapture) ⇒ Promise.<void>
* [.runBrowserBehaviors](#utils.module_TwitterCapture.TwitterCapture+runBrowserBehaviors) ⇒ Promise.<void>
* [.resizeViewportToFitDocument](#utils.module_TwitterCapture.TwitterCapture+resizeViewportToFitDocument) ⇒ Promise.<void>
* [.getDocumentDimensions](#utils.module_TwitterCapture.TwitterCapture+getDocumentDimensions) ⇒ Promise.<{width: number, height: number}>
* [.interceptJpegs](#utils.module_TwitterCapture.TwitterCapture+interceptJpegs) ⇒ Promise.<void>
* [.generateRawPDF](#utils.module_TwitterCapture.TwitterCapture+generateRawPDF) ⇒ Promise.<Buffer>
* [.addInterceptedJPEGsToPDF](#utils.module_TwitterCapture.TwitterCapture+addInterceptedJPEGsToPDF) ⇒ Promise.<void>
* [.captureAndAddUrlMapToPDF](#utils.module_TwitterCapture.TwitterCapture+captureAndAddUrlMapToPDF) ⇒ Promise.<void>
* [.captureAndAddVideoToPDF](#utils.module_TwitterCapture.TwitterCapture+captureAndAddVideoToPDF) ⇒ Promise.<void>
* [.cropMarginsOnPDF](#utils.module_TwitterCapture.TwitterCapture+cropMarginsOnPDF)
* [.signPDF](#utils.module_TwitterCapture.TwitterCapture+signPDF) ⇒ Buffer
* [.filterOptions](#utils.module_TwitterCapture.TwitterCapture+filterOptions)
* [.filterUrl](#utils.module_TwitterCapture.TwitterCapture+filterUrl) ⇒ bool
#### new exports.TwitterCapture(url, options)
| Param | Type | Description |
| --- | --- | --- |
| url | string
| `twitter.com` url to capture. Works best on statuses and threads. |
| options | object
| See `TwitterCapture.defaults` for detailed options. Will use defaults unless overridden. |
#### twitterCapture.defaults
Defaults for options that can be passed to `TwitterCapture`.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| privateKeyPath | string
| Path to `.pem` file containing a private key. |
| certPath | string
| Path to a `.pem` file containing a certificate. |
| tmpFolderPath | string
| Path to a folder in which temporary file can be written. |
| ytDlpPath | string
| Path to the `yt-dlp` executable. |
| templatesFolderPath | string
| Path to the templates folder (t.co resolver summary feature). |
| timestampServerUrl | string
| Timestamping server. |
| networkidleTimeout | number
| Time to wait for "networkidle" state. |
| runBrowserBehaviors | boolean
| If `true`, will try to auto-scroll and open more responses. Set to `false` automatically when trying to capture a profile url. |
| browserBehaviorsTimeout | number
| Maximum browser behaviors execution time. |
| videoCaptureTimeout | number
| Maximum yt-dlp execution time. |
| renderTimeout | number
| Time to wait for re-renders. |
#### twitterCapture.options : object
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.url : string
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.urlType : string
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.playwright : Object
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.interceptedJPEGs : object.<string, Buffer>
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.capture ⇒ Promise.<Buffer>
Captures the current Twitter.com url and makes it a signed PDF.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
**Returns**: Promise.<Buffer>
- - Signed PDF.
#### twitterCapture.setup ⇒ Promise.<void>
Sets up the browser used for capture as well as network interception for images capture.
Populates `this.playwright`.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.teardown
Closes browser used for capture.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.adjustUIForCapture ⇒ Promise.<void>
Adjusts the current page's DOM so the resulting PDF is not affected by UI artifact.
Playwright needs to be ready.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.runBrowserBehaviors ⇒ Promise.<void>
Runs browser behaviors:
- Tries to scroll through the page.
- Tries to click on the next available "Show replies" button.
Playwright needs to be ready.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.resizeViewportToFitDocument ⇒ Promise.<void>
Stretches the viewport to match the document's dimensions.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.getDocumentDimensions ⇒ Promise.<{width: number, height: number}>
Returns the current dimensions of the document.
Playwright needs to be ready.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
#### twitterCapture.interceptJpegs ⇒ Promise.<void>
Uses Playwright's network interception to capture images and add them to `this.interceptedJPEGs`.
Called whenever Playwright processes an HTTP response.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Param | Type |
| --- | --- |
| response | playwright.Response
|
#### twitterCapture.generateRawPDF ⇒ Promise.<Buffer>
Generates a PDF of the current page using Chrome Dev Tools.
Playwright needs to be ready.
Populates `this.pdf`.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
**Returns**: Promise.<Buffer>
- - PDF Bytes
#### twitterCapture.addInterceptedJPEGsToPDF ⇒ Promise.<void>
Adds entries from `this.interceptedJPEGs`
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Type | Description |
| --- | --- |
| PDFDocument
| Editable PDF object from `pdf-lib`. |
#### twitterCapture.captureAndAddUrlMapToPDF ⇒ Promise.<void>
Tries to list and resolve all the `t.co` urls on the page, and add the resulting map as an attachment.
Attachment filename: `url-map.html`.
Playwright needs to be ready.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Type | Description |
| --- | --- |
| PDFDocument
| Editable PDF object from `pdf-lib`. |
#### twitterCapture.captureAndAddVideoToPDF ⇒ Promise.<void>
Tries to capture main video from current Twitter url and add it as attachment to the PDF.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Type | Description |
| --- | --- |
| PDFDocument
| Editable PDF object from `pdf-lib`. |
#### twitterCapture.cropMarginsOnPDF
Tries to remove some of the white space at the bottom of the PDF.
[!] TODO: This is a "let's ship it" hack. We will need to find a better solution.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Param | Type |
| --- | --- |
| editablePDF | PDFDocument
|
#### twitterCapture.signPDF ⇒ Buffer
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
**Returns**: Buffer
- - PDF Bytes
| Param | Type | Description |
| --- | --- | --- |
| editedPDF | Buffer
| PDF Bytes |
#### twitterCapture.filterOptions
Applies some basic filtering to new option objects and fills gaps with defaults.
Replaces `this.options` after filtering.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Param | Type |
| --- | --- |
| newOptions | Promise.<object>
|
#### twitterCapture.filterUrl ⇒ bool
Filters a given URL to ensure it's a `twitter.com` one.
Also asserts it's "type": "status", "search", "profile".
Automatically populates `this.url` and `this.urlType`.
**Kind**: instance property of [TwitterCapture
](#utils.module_TwitterCapture.TwitterCapture)
| Param | Type |
| --- | --- |
| url | string
|
### TwitterCapture~URL\_MAP\_TEMPLATE
Nunjucks template used by `TwitterCapture.captureAndAddUrlMapToPDF`.
**Kind**: inner constant of [TwitterCapture
](#utils.module_TwitterCapture)