You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
8.8 KiB

8 years ago
8 years ago
8 years ago
  1. XRay
  2. ====
  3. XRay parses structured content from a URL.
  4. ## Discovering Content
  5. The contents of the URL is checked in the following order:
  6. * A silo URL from one of the following websites:
  7. ** Instagram
  8. ** Twitter
  9. ** (more coming soon)
  10. * h-entry, h-event, h-card
  11. * OEmbed (coming soon)
  12. * OGP (coming soon)
  13. ## Parse API
  14. To parse a page and return structured data for the contents of the page, simply pass a url to the parse route.
  15. ```
  16. GET /parse?url=https://aaronparecki.com/2016/01/16/11/
  17. ```
  18. To conditionally parse the page after first checking if it contains a link to a target URL, also include the target URL as a parameter. This is useful when using XRay to verify an incoming webmention.
  19. ```
  20. GET /parse?url=https://aaronparecki.com/2016/01/16/11/&target=http://example.com
  21. ```
  22. In both cases, the response will be a JSON object containing a key of "type". If there was an error, "type" will be set to the string "error", otherwise it will refer to the kind of content that was found at the URL, most often "entry".
  23. You can also make a POST request with the same parameter names.
  24. ### Authentication
  25. If the URL you are fetching requires authentication, include the access token in the parameter "token", and it will be included in an "Authorization" header when fetching the URL. (It is recommended to use a POST request in this case, to avoid the access token potentially being logged as part of the query string.) This is useful for [Private Webmention](https://indieweb.org/Private-Webmention) verification.
  26. ```
  27. POST /parse
  28. url=https://aaronparecki.com/2016/01/16/11/
  29. &target=http://example.com
  30. &token=12341234123412341234
  31. ```
  32. ### Twitter Authentication
  33. XRay uses the Twitter API to fetch posts, and the Twitter API requires authentication. In order to keep XRay stateless, it is required that you pass in Twitter credentials to the parse call. You can register an application on the Twitter developer website, and generate an access token for your account without writing any code, and then use those credentials when making an API request to XRay.
  34. You should only send Twitter credentials when the URL you are trying to parse is a Twitter URL, so you'll want to check for whether the hostname is `twitter.com` before you include credentials in this call.
  35. * twitter_api_key - Your application's API key
  36. * twitter_api_secret - Your application's API secret
  37. * twitter_access_token - Your Twitter access token
  38. * twitter_access_token_secret - Your Twitter secret access token
  39. ### Error Response
  40. ```json
  41. {
  42. "error": "not_found",
  43. "error_description": "The URL provided was not found"
  44. }
  45. ```
  46. Possible errors are listed below:
  47. * `not_found`: The URL provided was not found. (Returned 404 when fetching)
  48. * `ssl_cert_error`: There was an error validating the SSL certificate. This may happen if the SSL certificate has expired.
  49. * `ssl_unsupported_cipher`: The web server does not support any of the SSL ciphers known by the service.
  50. * `timeout`: The service timed out trying to connect to the URL.
  51. * `invalid_content`: The content at the URL was not valid. For example, providing a URL to an image will return this error.
  52. * `no_link_found`: The target link was not found on the page. When a target parameter is provided, this is the error that will be returned if the target could not be found on the page.
  53. * `no_content`: No usable content could be found at the given URL.
  54. * `unauthorized`: The URL returned HTTP 401 Unauthorized.
  55. * `forbidden`: The URL returned HTTP 403 Forbidden.
  56. ### Response Format
  57. ```json
  58. {
  59. "data": {
  60. "type": "entry",
  61. "author": {
  62. "type": "card",
  63. "name": "Aaron Parecki",
  64. "photo": "https://aaronparecki.com/images/aaronpk-256.jpg",
  65. "url": "https://aaronparecki.com/"
  66. },
  67. "url": "https://aaronparecki.com/2016/01/16/11/",
  68. "published": "2016-01-16T16:26:43-08:00",
  69. "photo": [
  70. "https://aaronparecki.com/2016/01/16/11/photo.png"
  71. ],
  72. "syndication": [
  73. "https://twitter.com/aaronpk/status/688518372170977280"
  74. ],
  75. "summary": "Now that @MozillaPersona is shutting down, the only good way to do email-based login is how @poetica does it.",
  76. "content": {
  77. "html": "Now that <a href=\"https://twitter.com/MozillaPersona\">@MozillaPersona</a> is shutting down, the only good way to do email-based login is how <a href=\"https://twitter.com/poetica\">@poetica</a> does it.",
  78. "text": "Now that @MozillaPersona is shutting down, the only good way to do email-based login is how @poetica does it."
  79. },
  80. },
  81. "url": "https://aaronparecki.com/2016/01/16/11/"
  82. }
  83. ```
  84. #### Primary Data
  85. The primary object on the page is returned in the `data` property. This will indicate the type of object (e.g. `entry`), and will contain the vocabulary's properties that it was able to parse from the page.
  86. If a property supports multiple values, it will always be returned as an array. The following properties support multiple values:
  87. * in-reply-to
  88. * syndication
  89. * photo (of entry, not of a card)
  90. The content will be an object that always contains a "text" property and may contain an "html" property if the source documented published HTML content. The "text" property must always be HTML escaped before displaying it as HTML, as it may include unescaped characters such as `<` and `>`.
  91. The author will always be set in the entry if available. The service follows the [authorship discovery](http://indiewebcamp.com/authorship) algorithm to try to find the author information elsewhere on the page if it is not inside the entry in the source document.
  92. All URLs provided in the output are absolute URLs. If the source document contains a relative URL, it will be resolved first.
  93. In a future version, replies, likes, reposts, etc. of this post will be included if they are listed on the page.
  94. ```json
  95. {
  96. "data": {
  97. "type": "entry",
  98. ...
  99. "like": [
  100. {
  101. "type": "cite",
  102. "author": {
  103. "type": "card",
  104. "name": "Thomas Dunlap",
  105. "photo": "https://s3-us-west-2.amazonaws.com/aaronparecki.com/twitter.com/9055c458a67762637c0071006b16c78f25cb610b224dbc98f48961d772faff4d.jpeg",
  106. "url": "https://twitter.com/spladow"
  107. },
  108. "url": "https://twitter.com/aaronpk/status/688518372170977280#favorited-by-16467582"
  109. }
  110. ],
  111. "comment": [
  112. {
  113. "type": "cite",
  114. "author": {
  115. "type": "card",
  116. "name": "Poetica",
  117. "photo": "https://s3-us-west-2.amazonaws.com/aaronparecki.com/twitter.com/192664bb706b2998ed42a50a860490b6aa1bb4926b458ba293b4578af599aa6f.png",
  118. "url": "http://poetica.com/"
  119. },
  120. "url": "https://twitter.com/poetica/status/689045331426803712",
  121. "published": "2016-01-18T03:23:03-08:00",
  122. "content": {
  123. "text": "@aaronpk @mozillapersona thanks very much! :)"
  124. }
  125. }
  126. ]
  127. }
  128. }
  129. ```
  130. #### Other Properties
  131. Other properties are returned in the response at the same level as the `data` property.
  132. * `url` - The effective URL that the document was retrieved from. This will be the final URL after following any redirects.
  133. * `code` - The HTTP response code returned by the URL. Typically this will be 200, but if the URL returned an alternate HTTP code that also included an h-entry (such as a 410 deleted notice with a stub h-entry), you can use this to find out that the original URL was actually deleted.
  134. ## Token API
  135. When verifying [Private Webmentions](https://indieweb.org/Private-Webmention#How_to_Receive_Private_Webmentions), you will need to exchange a code for an access token at the token endpoint specified by the source URL.
  136. XRay provides an API that will do this in one step. You can provide the source URL and code you got from the webmention, and XRay will discover the token endpoint, and then return you an access token.
  137. ```
  138. POST /token
  139. source=http://example.com/private-post
  140. &code=1234567812345678
  141. ```
  142. The response will be the response from the token endpoint, which will include an `access_token` property, and possibly an `expires_in` property.
  143. ```
  144. {
  145. "access_token": "eyJ0eXAXBlIjoI6Imh0dHB8idGFyZ2V0IjoraW0uZGV2bb-ZO6MV-DIqbUn_3LZs",
  146. "token_type": "bearer",
  147. "expires_in": 3600
  148. }
  149. ```
  150. If there was a problem fetching the access token, you will get one of the errors below in addition to the HTTP related errors returned by the parse API:
  151. * `no_token_endpoint` - Unable to find an HTTP header specifying the token endpoint.
  152. ## Installation
  153. ### From Source
  154. ```
  155. # Clone this repository
  156. git clone git@github.com:aaronpk/XRay.git
  157. cd XRay
  158. # Install dependencies
  159. composer install
  160. ```
  161. ### From Zip Archive
  162. * Download the latest release from https://github.com/aaronpk/XRay/releases
  163. * Extract to a folder on your web server
  164. ### Web Server Configuration
  165. Configure your web server to point to the `public` folder.
  166. Make sure all requests are routed to `index.php`. XRay ships with `.htaccess` files for Apache. For nginx, you'll need a rule like the following in your server config block.
  167. ```
  168. try_files $uri /index.php?$args;
  169. ```