├── .gitignore ├── index.js ├── package.json ├── README.md ├── LICENSE └── strava.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | test.js -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./strava'); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strava", 3 | "version": "0.0.2", 4 | "description": "Node.js library for the Strava V3 API", 5 | "main": "index.js", 6 | "author": "Micah Fitzgerald", 7 | "license": "GPL v2", 8 | "dependencies": { 9 | "underscore": "latest", 10 | "request": "latest", 11 | "form-data": "latest", 12 | "async": "latest" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git://github.com/mcfitz2/node-strava.git" 17 | }, 18 | "keywords": [ 19 | "strava" 20 | ], 21 | "bugs": { 22 | "url": "https://github.com/mcfitz2/node-strava/issues" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | node-strava is in the NPM repository. You can install it by running the command below: 3 | ``` 4 | npm install strava 5 | ``` 6 | To get started, first initialize the client with your OAuth credentials: 7 | ``` 8 | var strava = new require("./strava")({ 9 | client_id: "", 10 | client_secret: "", 11 | redirect_uri: "", 12 | access_token: "" 13 | }); 14 | ``` 15 | Then you can access the API: 16 | ``` 17 | strava.athlete.get(function(err, res) { 18 | console.log(res); 19 | }); 20 | { 21 | "id": 227615, 22 | "resource_state": 3, 23 | "firstname": "John", 24 | "lastname": "Applestrava", 25 | "profile_medium": "http://pics.com/227615/medium.jpg", 26 | "profile": "http://pics.com/227615/large.jpg", 27 | "city": "San Francisco", 28 | "state": "California", 29 | "country": "United States", 30 | "sex": "M", 31 | ... 32 | } 33 | ``` 34 | 35 | ## Conventions 36 | For all methods, `params` is an object with keys corresponding to the parameters given in the Strava API. Some methods have other special parameters. 37 | ## athlete 38 | 39 | ### get(params, callback) 40 | 41 | --------------------------------------- 42 | 43 | ### update(params, callback) 44 | 45 | --------------------------------------- 46 | 47 | ### followers.get(params, callback) 48 | 49 | __Parameters__ 50 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 51 | 52 | --------------------------------------- 53 | 54 | ### friends.get(params, callback) 55 | 56 | __Parameters__ 57 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 58 | 59 | --------------------------------------- 60 | 61 | ### activities.get(params, callback) 62 | 63 | __Parameters__ 64 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 65 | 66 | --------------------------------------- 67 | 68 | ### clubs.get(params, callback) 69 | 70 | __Parameters__ 71 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 72 | 73 | --------------------------------------- 74 | 75 | ## athletes 76 | 77 | ### get(id, params, callback) 78 | 79 | --------------------------------------- 80 | 81 | ### update(id, params, callback) 82 | 83 | --------------------------------------- 84 | 85 | ### koms.get(id, params, callback) 86 | 87 | __Parameters__ 88 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 89 | 90 | --------------------------------------- 91 | 92 | ### friends.get(id, params, callback) 93 | 94 | __Parameters__ 95 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 96 | 97 | --------------------------------------- 98 | 99 | ### followers.get(id, params, callback) 100 | 101 | __Parameters__ 102 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 103 | 104 | --------------------------------------- 105 | 106 | ### bothfollowing.get(id, params, callback) 107 | 108 | __Parameters__ 109 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 110 | 111 | --------------------------------------- 112 | 113 | ## activities 114 | 115 | ### get(id, params, callback) 116 | 117 | --------------------------------------- 118 | 119 | ### create(params, callback) 120 | 121 | --------------------------------------- 122 | 123 | ### update(id, params, callback) 124 | 125 | --------------------------------------- 126 | 127 | ### delete(id, params, callback) 128 | 129 | --------------------------------------- 130 | 131 | ### comments.get(id, params, callback) 132 | 133 | __Parameters__ 134 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 135 | 136 | --------------------------------------- 137 | 138 | ### kudos.get(id, params, callback) 139 | 140 | __Parameters__ 141 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 142 | 143 | --------------------------------------- 144 | 145 | ### photos.get(id, params, callback) 146 | 147 | --------------------------------------- 148 | 149 | ### following.get(params, callback) 150 | 151 | __Parameters__ 152 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 153 | 154 | --------------------------------------- 155 | 156 | ### zones.get(id, params, callback) 157 | 158 | --------------------------------------- 159 | 160 | ### laps.get(id, params, callback) 161 | 162 | --------------------------------------- 163 | 164 | ### streams.get(id, params, callback) 165 | 166 | __Parameters__ 167 | * `types` - List of data types to request. Should be an array. 168 | 169 | --------------------------------------- 170 | 171 | ## clubs 172 | 173 | ### get(id, params, callback) 174 | 175 | --------------------------------------- 176 | 177 | ### members.get(id, params, callback) 178 | 179 | __Parameters__ 180 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 181 | 182 | --------------------------------------- 183 | 184 | ### activities.get(id, params, callback) 185 | 186 | __Parameters__ 187 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 188 | 189 | --------------------------------------- 190 | 191 | ## gear 192 | 193 | ### get(id, params, callback) 194 | 195 | --------------------------------------- 196 | 197 | ## segments 198 | 199 | ### get(id, params, callback) 200 | 201 | --------------------------------------- 202 | 203 | ### streams.get(id, params, callback) 204 | 205 | __Parameters__ 206 | * `types` - List of data types to request. Should be an array. 207 | 208 | --------------------------------------- 209 | 210 | ### starred.get(params, callback) 211 | 212 | __Parameters__ 213 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 214 | 215 | --------------------------------------- 216 | 217 | ### explore.get(params, callback) 218 | 219 | --------------------------------------- 220 | 221 | ### leaderboard.get(id, params, callback) 222 | 223 | __Parameters__ 224 | * `paginate` - Automatically fetch all results. Currently cannot restrict number of pages 225 | 226 | --------------------------------------- 227 | 228 | ## segmentefforts 229 | 230 | ### get(id, params, callback) 231 | 232 | --------------------------------------- 233 | 234 | ### streams.get(id, params, callback) 235 | 236 | __Parameters__ 237 | * `types` - List of data types to request. Should be an array. 238 | 239 | --------------------------------------- 240 | 241 | ## uploads 242 | 243 | ### upload(params, callback) 244 | 245 | __Parameters__ 246 | * `wait` - Poll API for upload status and return only after an error or a successful upload 247 | * `poll_interval` - Amount of time (in milliseconds) to wait between polls 248 | * `poll_callback` - Function that is called after each poll with the arguments `err`, and `res` 249 | * `data` - GPX, FIT, or TCX file as a buffer, string, or stream 250 | * `filename` - Specify filename of GPX, FIT, or TCX file. If `data` is not set, this filename will be used to open the file. Otherwise, it has no real meaning. 251 | 252 | __Examples__ 253 | 254 | As a stream: 255 | ``` 256 | strava.uploads.upload({wait:true, data:fs.createReadStream("test.gpx")}, function(err, body) { 257 | console.log(err, body); 258 | }); 259 | ``` 260 | As a string: 261 | ``` 262 | fs.readFile("test.gpx", function(err, gpx) { 263 | strava.uploads.upload({wait:true, data:gpx}, function(err, body) { 264 | console.log(err, body); 265 | }); 266 | }); 267 | ``` 268 | Using just a filename: 269 | ``` 270 | strava.uploads.upload({wait:true, filename:"test.gpx"}, function(err, body) { 271 | console.log(err, body); 272 | }); 273 | ``` 274 | Uploading a TCX file: 275 | ``` 276 | strava.uploads.upload({wait:true, filename:"test.tcx", "data_type":"tcx"}, function(err, body) { 277 | console.log(err, body); 278 | }); 279 | ``` 280 | --------------------------------------- 281 | 282 | ### poll(id, callback) 283 | 284 | --------------------------------------- 285 | ## Contributing 286 | node-strava is the work of a single developer. If you want to contribute, feel free to send a pull request! 287 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /strava.js: -------------------------------------------------------------------------------- 1 | var _ = require("underscore"); 2 | var request = require("request"); 3 | var formdata = require("form-data"); 4 | var async = require("async"); 5 | var fs = require("fs"); 6 | var stream = require("stream"); 7 | var path = require("path"); 8 | 9 | function responseHandler(callback) { 10 | return function handler(err, res, body) { 11 | if (err) { 12 | err.res = res; 13 | return callback(err, body); 14 | } 15 | if (body === undefined) { 16 | body = {}; 17 | } 18 | body.statusCode = res.statusCode; 19 | if (res.statusCode >= 400 && res.statusCode <= 599) { 20 | 21 | err = new Error("HTTP error " + res.statusCode); 22 | return callback(err, body); 23 | } 24 | if (body && body.errors) { 25 | err = new Error("API Error " + res.statusCode); 26 | return callback(err, body); 27 | } 28 | return callback(null, body); 29 | }; 30 | } 31 | var Strava = function(config_obj) { 32 | if (!(this instanceof Strava)) { 33 | return new Strava(config_obj); 34 | } 35 | 36 | var config = { 37 | oauth_base: 'https://www.strava.com/oauth/authorize', 38 | api_base: 'https://www.strava.com/api/v3', 39 | authorize_url: '/authorize' 40 | }; 41 | 42 | this.config = _.extend(config, config_obj); 43 | this.http = request; 44 | if (!this.config.client_id) { 45 | throw new Error('Missing Client ID'); 46 | } 47 | var self = this; 48 | this.athlete = { 49 | get: function(params, callback) { 50 | if (typeof params == 'function') { 51 | callback = params; 52 | params = {}; 53 | } 54 | self._get("/athlete", params, responseHandler(callback)); 55 | }, 56 | update: function(params, callback) { 57 | if (typeof params == 'function') { 58 | callback = params; 59 | params = {}; 60 | } 61 | throw new Error("not implemented"); 62 | }, 63 | followers: { 64 | get: function(params, callback) { 65 | if (typeof params == 'function') { 66 | callback = params; 67 | params = {}; 68 | } 69 | if (params.paginate) { 70 | self._paged_get("/athlete/followers", params, callback); 71 | } else { 72 | self._get("/athlete/followers", params, responseHandler(callback)); 73 | } 74 | } 75 | }, 76 | friends: { 77 | get: function(params, callback) { 78 | if (typeof params == 'function') { 79 | callback = params; 80 | params = {}; 81 | } 82 | if (params.paginate) { 83 | self._paged_get("/athlete/friends", params, callback); 84 | } else { 85 | self._get("/athlete/friends", params, responseHandler(callback)); 86 | } 87 | } 88 | }, 89 | /* bothfollowing: { 90 | get: function(params, callback) { 91 | if (typeof params == 'function') { 92 | callback = params; 93 | params = {}; 94 | } 95 | self._get("/athlete", params, responseHandler(callback)); 96 | } 97 | },*/ 98 | activities: { 99 | get: function(params, callback) { 100 | if (typeof params == 'function') { 101 | callback = params; 102 | params = {}; 103 | } 104 | if (params.paginate) { 105 | self._paged_get("/athlete/activities", params, callback); 106 | } else { 107 | self._get("/athlete/activities", params, responseHandler(callback)); 108 | } 109 | } 110 | }, 111 | clubs: { 112 | get: function(params, callback) { 113 | if (typeof params == 'function') { 114 | callback = params; 115 | params = {}; 116 | } 117 | self._get("/athlete/clubs", params, responseHandler(callback)); 118 | } 119 | } 120 | }; 121 | this.athletes = { 122 | get: function(id, params, callback) { 123 | if (typeof params == 'function' && arguments.length == 2) { 124 | callback = params; 125 | params = {}; 126 | } 127 | if (typeof id == 'function' && arguments.length == 1) { 128 | callback = id; 129 | params = {}; 130 | return self.athlete.get(params, callback); 131 | } 132 | self._get("/athletes/" + id, params, responseHandler(callback)); 133 | }, 134 | update: function(id, params, callback) { 135 | if (typeof id == 'function' && arguments.length == 1) { 136 | callback = id; 137 | params = {}; 138 | return self.athlete.update(params, callback); 139 | } 140 | //self.put 141 | }, 142 | koms: { 143 | get: function(id, params, callback) { 144 | if (typeof params == 'function' && arguments.length == 2) { 145 | callback = params; 146 | params = {}; 147 | } 148 | if (typeof id == 'function' && arguments.length == 1) { 149 | callback = id; 150 | params = {}; 151 | return self.athlete.koms(params, callback); 152 | } 153 | if (params.paginate) { 154 | self._paged_get("/athletes/" + id + "/koms", params, callback); 155 | } else { 156 | self._get("/athletes/" + id + "/koms", params, responseHandler(callback)); 157 | } 158 | } 159 | }, 160 | friends: { 161 | get: function(id, params, callback) { 162 | if (typeof params == 'function' && arguments.length == 2) { 163 | callback = params; 164 | params = {}; 165 | } 166 | if (typeof id == 'function' && arguments.length == 1) { 167 | callback = id; 168 | params = {}; 169 | return self.athlete.friends(params, callback); 170 | } 171 | if (params.paginate) { 172 | self._paged_get("/athletes/" + id + "/friends", params, callback); 173 | } else { 174 | self._get("/athletes/" + id + "/friends", params, responseHandler(callback)); 175 | } 176 | } 177 | }, 178 | followers: { 179 | get: function(id, params, callback) { 180 | if (typeof params == 'function' && arguments.length == 2) { 181 | callback = params; 182 | params = {}; 183 | } 184 | if (typeof id == 'function' && arguments.length == 1) { 185 | callback = id; 186 | params = {}; 187 | return self.athlete.folllowers(params, callback); 188 | } 189 | if (params.paginate) { 190 | self._paged_get("/athletes/" + id + "/followers", params, callback); 191 | } else { 192 | self._get("/athletes/" + id + "/followers", params, responseHandler(callback)); 193 | } 194 | } 195 | }, 196 | bothfollowing: { 197 | get: function(id, params, callback) { 198 | if (typeof params == 'function' && arguments.length == 2) { 199 | callback = params; 200 | params = {}; 201 | } 202 | if (typeof id == 'function' && arguments.length == 1) { 203 | callback = id; 204 | params = {}; 205 | return self.athlete.bothfolllowing(params, callback); 206 | } 207 | if (params.paginate) { 208 | self._paged_get("/athletes/" + id + "/both-following", params, callback); 209 | } else { 210 | self._get("/athletes/" + id + "/both-following", params, responseHandler(callback)); 211 | } 212 | } 213 | } 214 | }; 215 | this.activities = { 216 | get: function(id, params, callback) { 217 | if (typeof params == 'function' && arguments.length == 2) { 218 | callback = params; 219 | params = {}; 220 | } 221 | if (typeof id == 'function' && arguments.length == 1) { 222 | throw new Error("Activity ID is required"); 223 | 224 | } 225 | self._get("/activities/" + id, params, responseHandler(callback)); 226 | }, 227 | create: function(params, callback) { 228 | self._post("/activities", params, responseHandler(callback)); 229 | }, 230 | update: function(id, params, callback) { 231 | throw new Error("not implemented"); 232 | }, 233 | delete: function(id, params, callback) { 234 | if (typeof params == 'function' && arguments.length == 2) { 235 | callback = params; 236 | params = {}; 237 | } 238 | if (typeof id == 'function' && arguments.length == 1) { 239 | throw new Error("Activity ID is required"); 240 | 241 | } 242 | self._delete("/activities/" + id, params, responseHandler(callback)); 243 | }, 244 | comments: { 245 | get: function(id, params, callback) { 246 | if (typeof params == 'function' && arguments.length == 2) { 247 | callback = params; 248 | params = {}; 249 | } 250 | if (typeof id == 'function' && arguments.length == 1) { 251 | throw new Error("Activity ID is required"); 252 | 253 | } 254 | if (params.paginate) { 255 | self._paged_get("/activities/" + id + "/comments", params, callback); 256 | } else { 257 | self._get("/activities/" + id + "/comments", params, responseHandler(callback)); 258 | } 259 | } 260 | }, 261 | kudos: { 262 | get: function(id, params, callback) { 263 | if (typeof params == 'function' && arguments.length == 2) { 264 | callback = params; 265 | params = {}; 266 | } 267 | if (typeof id == 'function' && arguments.length == 1) { 268 | throw new Error("Activity ID is required"); 269 | 270 | } 271 | if (params.paginate) { 272 | self._paged_get("/activities/" + id + "/kudos", params, callback); 273 | } else { 274 | self._get("/activities/" + id + "/kudos", params, responseHandler(callback)); 275 | } 276 | } 277 | }, 278 | photos: { 279 | get: function(id, params, callback) { 280 | if (typeof params == 'function' && arguments.length == 2) { 281 | callback = params; 282 | params = {}; 283 | } 284 | if (typeof id == 'function' && arguments.length == 1) { 285 | throw new Error("Activity ID is required"); 286 | 287 | } 288 | self._get("/activities/" + id + "/photos", params, responseHandler(callback)); 289 | } 290 | }, 291 | following: { 292 | get: function(params, callback) { 293 | if (typeof params == 'function' && arguments.length == 2) { 294 | callback = params; 295 | params = {}; 296 | } 297 | 298 | if (params.paginate) { 299 | self._paged_get("/activities/following", params, callback); 300 | } else { 301 | self._get("/activities/following", params, responseHandler(callback)); 302 | } 303 | } 304 | }, 305 | zones: { 306 | get: function(id, params, callback) { 307 | if (typeof params == 'function' && arguments.length == 2) { 308 | callback = params; 309 | params = {}; 310 | } 311 | if (typeof id == 'function' && arguments.length == 1) { 312 | throw new Error("Activity ID is required"); 313 | 314 | } 315 | self._get("/activities/" + id + "/zones", params, responseHandler(callback)); 316 | } 317 | }, 318 | laps: { 319 | get: function(id, params, callback) { 320 | if (typeof params == 'function' && arguments.length == 2) { 321 | callback = params; 322 | params = {}; 323 | } 324 | if (typeof id == 'function' && arguments.length == 1) { 325 | throw new Error("Activity ID is required"); 326 | 327 | } 328 | self._get("/activities/" + id + "/laps", params, responseHandler(callback)); 329 | } 330 | }, 331 | streams: { 332 | get: function(id, params, callback) { 333 | if (typeof params == 'function' && arguments.length == 3) { 334 | callback = params; 335 | params = {}; 336 | } 337 | if (typeof id == 'function' && arguments.length == 2) { 338 | throw new Error("Activity ID is required"); 339 | } 340 | if (arguments.length < 3) { 341 | throw new Error("Invalid arguments"); 342 | } 343 | var types = params.types.join(","); 344 | 345 | // types = "time"; 346 | // console.log("ST",types, id, params); 347 | self._get("/activities/" + id + "/streams/" + types, params, responseHandler(callback)); 348 | } 349 | } 350 | }; 351 | this.clubs = { 352 | get: function(id, params, callback) { 353 | if (typeof params == 'function' && arguments.length == 2) { 354 | callback = params; 355 | params = {}; 356 | } 357 | if (typeof id == 'function' && arguments.length == 1) { 358 | throw new Error("Club ID is required"); 359 | 360 | } 361 | self._get("/clubs/" + id, params, responseHandler(callback)); 362 | }, 363 | members: { 364 | get: function(id, params, callback) { 365 | if (typeof params == 'function' && arguments.length == 2) { 366 | callback = params; 367 | params = {}; 368 | } 369 | if (typeof id == 'function' && arguments.length == 1) { 370 | throw new Error("Club ID is required"); 371 | 372 | } 373 | if (params.paginate) { 374 | self._paged_get("/clubs/" + id + "/members", params, callback); 375 | } else { 376 | self._get("/clubs/" + id + "/members", params, responseHandler(callback)); 377 | } 378 | } 379 | }, 380 | activities: { 381 | get: function(id, params, callback) { 382 | if (typeof params == 'function' && arguments.length == 2) { 383 | callback = params; 384 | params = {}; 385 | } 386 | if (typeof id == 'function' && arguments.length == 1) { 387 | throw new Error("Club ID is required"); 388 | 389 | } 390 | if (params.paginate) { 391 | self._paged_get("/clubs/" + id + "/activities", params, callback); 392 | } else { 393 | self._get("/clubs/" + id + "/activities", params, responseHandler(callback)); 394 | } 395 | } 396 | } 397 | }; 398 | this.gear = { 399 | get: function(id, params, callback) { 400 | if (typeof params == 'function' && arguments.length == 2) { 401 | callback = params; 402 | params = {}; 403 | } 404 | if (typeof id == 'function' && arguments.length == 1) { 405 | throw new Error("Gear ID is required"); 406 | 407 | } 408 | self._get("/gear/" + id, params, responseHandler(callback)); 409 | 410 | } 411 | }; 412 | this.segments = { 413 | get: function(id, params, callback) { 414 | if (typeof params == 'function' && arguments.length == 2) { 415 | callback = params; 416 | params = {}; 417 | } 418 | if (typeof id == 'function' && arguments.length == 1) { 419 | throw new Error("Segment ID is required"); 420 | 421 | } 422 | self._get("/segments/" + id, params, responseHandler(callback)); 423 | 424 | }, 425 | streams: { 426 | get: function(id, params, callback) { 427 | if (typeof params == 'function' && arguments.length == 2) { 428 | callback = params; 429 | params = {}; 430 | } 431 | if (typeof id == 'function' && arguments.length == 1) { 432 | throw new Error("Segment ID is required"); 433 | 434 | } 435 | var types = params.types.join(","); 436 | 437 | // types = "time"; 438 | self._get("/segments/" + id + "/streams/" + types, params, responseHandler(callback)); 439 | 440 | } 441 | }, 442 | starred: { 443 | get: function(params, callback) { 444 | if (typeof params == 'function' && arguments.length == 2) { 445 | callback = params; 446 | params = {}; 447 | } 448 | if (params.paginate) { 449 | self._paged_get("/segments/starred", params, callback); 450 | } else { 451 | self._get("/segments/starred", params, responseHandler(callback)); 452 | } 453 | 454 | } 455 | }, 456 | explore: { 457 | get: function(params, callback) { 458 | if (typeof params == 'function' && arguments.length == 2) { 459 | callback = params; 460 | params = {}; 461 | } 462 | self._get("/segments/explore", params, responseHandler(callback)); 463 | } 464 | 465 | }, 466 | leaderboard: { 467 | get: function(id, params, callback) { 468 | if (typeof params == 'function' && arguments.length == 2) { 469 | callback = params; 470 | params = {}; 471 | } 472 | if (typeof id == 'function' && arguments.length == 1) { 473 | throw new Error("Segment ID is required"); 474 | 475 | } 476 | if (params.paginate) { 477 | self._paged_get("/segments/" + id + "/leaderboard", params, callback); 478 | } else { 479 | self._get("/segments/" + id + "/leaderboard", params, responseHandler(callback)); 480 | } 481 | } 482 | } 483 | 484 | 485 | }; 486 | this.segmentefforts = { 487 | get: function(id, params, callback) { 488 | if (typeof params == 'function' && arguments.length == 2) { 489 | callback = params; 490 | params = {}; 491 | } 492 | if (typeof id == 'function' && arguments.length == 1) { 493 | throw new Error("Segment ID is required"); 494 | 495 | } 496 | self._get("/segment_efforts/" + id, params, responseHandler(callback)); 497 | }, 498 | streams: { 499 | get: function(id, params, callback) { 500 | if (typeof params == 'function' && arguments.length == 2) { 501 | callback = params; 502 | params = {}; 503 | } 504 | if (typeof id == 'function' && arguments.length == 1) { 505 | throw new Error("Segment ID is required"); 506 | 507 | } 508 | var types = params.types.join(","); 509 | self._get("/segment_efforts/" + id + "/streams/" + types, params, responseHandler(callback)); 510 | } 511 | } 512 | }; 513 | //accepts gpx,fit,or tcx file as filename, stream, buffer, or string 514 | 515 | this.uploads = { 516 | upload: function(params, callback) { 517 | function pollUntil(id, callback) { 518 | var res = {}; 519 | var interval = Math.max(params.poll_interval, 1000) || 2000; 520 | async.doWhilst(function(callback) { 521 | setTimeout(function() { 522 | self.uploads.poll(id, function(err, body) { 523 | res = body; 524 | callback(err); 525 | }); 526 | }, interval); 527 | }, function() { 528 | return !res.done; 529 | }, function(err) { 530 | // console.log("CB", callback); 531 | callback(err, res); 532 | }); 533 | } 534 | 535 | function upload(buffer, callback) { 536 | var form = new formdata(); 537 | ["name", "description", "private", "external_id", "trainer"].forEach(function(key) { 538 | if (params[key]) { 539 | form.append(key, params[key]); 540 | } 541 | }); 542 | form.append("file", buffer, { 543 | filename: path.basename(params.filename) || "strava.gpx", 544 | knownLength: buffer.length 545 | }); 546 | form.append("activity_type", params.activity_type || "ride"); 547 | form.append("data_type", params.data_type || "gpx"); 548 | form.getLength(function(err, length) { 549 | self.http.post({ 550 | json: true, 551 | uri: self.config.api_base + "/uploads", 552 | headers: { 553 | "Authorization": "Bearer " + self.config.access_token, 554 | 'Content-Length': length 555 | }, 556 | }, responseHandler(function(err, body) { 557 | if (params.wait && (!self._checkStatus(body))) { 558 | console.log("Polling..."); 559 | pollUntil(body.id, callback); 560 | } else { 561 | callback(err, body); 562 | } 563 | }))._form = form; 564 | }); 565 | } 566 | if (!params.data) { 567 | if (!params.filename) { 568 | throw new Error("missing data and filename params"); 569 | } 570 | fs.readFile(params.filename, function(err, data) { 571 | if (err) { 572 | throw err; 573 | } 574 | upload(data, callback); 575 | }); 576 | } else { 577 | if (params.data instanceof stream.Readable) { 578 | var arr = []; 579 | params.data.on("data", function(chunk) { 580 | arr.push(chunk); 581 | }); 582 | params.data.on("end", function() { 583 | var buff = Buffer.concat(arr); 584 | upload(buff, callback); 585 | }); 586 | } else if (params.data instanceof String || typeof(params.data) === 'string') { 587 | upload(new Buffer(params.data), callback); 588 | } else if (params.data instanceof Buffer) { 589 | upload(params.data, callback); 590 | } else { 591 | console.log(params.data); 592 | console.log("data type not supported"); 593 | } 594 | } 595 | 596 | }, 597 | poll: function(id, callback) { 598 | request.get({ 599 | json: true, 600 | url: self.config.api_base + "/uploads/" + id, 601 | headers: { 602 | "Authorization": "Bearer " + self.config.access_token, 603 | }, 604 | }, responseHandler(function(err, body) { 605 | body.done = self._checkStatus(body); 606 | callback(err, body); 607 | })); 608 | } 609 | }; 610 | this.authenticate = function(params, callback) { 611 | if (typeof params == 'function' && arguments.length == 1) { 612 | callback = params; 613 | params = {}; 614 | } 615 | 616 | // if (typeof params.code === 'undefined') { 617 | // throw new Error("params must include a code value retrieved from Strava") 618 | // } 619 | var callParams = { 620 | client_id: this.config.client_id, 621 | client_secret: this.config.client_secret, 622 | code: params.code 623 | } 624 | 625 | self._post("/oauth/token", callParams, responseHandler(function(err, body) { 626 | self._setAccessToken(body.access_token) 627 | callback(err, body); 628 | })) 629 | }; 630 | }; 631 | Strava.prototype._setAccessToken = function(access_token) { 632 | this.config.access_token = access_token 633 | return this.config.access_token 634 | } 635 | 636 | Strava.prototype._checkStatus = function(body) { 637 | switch (body.status) { 638 | case "Your activity is still being processed.": 639 | return false; 640 | case "The created activity has been deleted.": 641 | return true; 642 | case "There was an error processing your activity.": 643 | return true; 644 | case "Your activity is ready.": 645 | return true; 646 | } 647 | }; 648 | 649 | Strava.prototype._get = function(call, params, callback) { 650 | if (!call) { 651 | throw new Error('call is required'); 652 | } 653 | if (!this.config.access_token) { 654 | throw new Error('Valid access token is required'); 655 | } 656 | 657 | var url = this.config.api_base + call; 658 | 659 | params.access_token = this.config.access_token; 660 | 661 | this.http.get({ 662 | uri: url, 663 | json: true, 664 | qs: params 665 | }, callback); 666 | }; 667 | Strava.prototype._post = function(call, params, callback) { 668 | if (!call) { 669 | throw new Error('call is required'); 670 | } 671 | if (!this.config.access_token && call != "/oauth/token") { 672 | throw new Error('Valid access token is required'); 673 | } 674 | 675 | var url = this.config.api_base + call; 676 | 677 | params.access_token = this.config.access_token; 678 | console.log("POST call") 679 | console.log(url) 680 | console.log(params) 681 | this.http.post({ 682 | uri: url, 683 | json: true, 684 | qs: params 685 | }, callback); 686 | }; 687 | Strava.prototype._delete = function(call, params, callback) { 688 | if (!call) { 689 | throw new Error('call is required'); 690 | } 691 | if (!this.config.access_token) { 692 | throw new Error('Valid access token is required'); 693 | } 694 | 695 | var url = this.config.api_base + call; 696 | 697 | params.access_token = this.config.access_token; 698 | 699 | this.http.del({ 700 | uri: url, 701 | json: true, 702 | qs: params 703 | }, callback); 704 | }; 705 | Strava.prototype._paged_get = function(call, params, callback) { 706 | var per_page = 100; 707 | var pages = []; 708 | var page = []; 709 | var self = this; 710 | var pagenum = 1; 711 | async.doWhilst(function(callback) { 712 | self._get(call, { 713 | per_page: per_page, 714 | page: pagenum 715 | }, responseHandler(function(err, res) { 716 | if (err) { 717 | callback(err, res); 718 | } else { 719 | page = res; 720 | page.forEach(function(item) { 721 | pages.push(item); 722 | }); 723 | pagenum++; 724 | callback(); 725 | } 726 | })); 727 | }, function() { 728 | return page.length == per_page; 729 | }, function(err) { 730 | callback(err, pages); 731 | }); 732 | }; 733 | 734 | module.exports = Strava; --------------------------------------------------------------------------------