├── .gitignore ├── LICENSE ├── README.md ├── action.js ├── common.js ├── icon.png ├── info.plist ├── package.json ├── xto.alfredworkflow └── xto.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | *.swp 31 | 32 | -------------------------------------------------------------------------------- /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 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Workflow XTO 2 | 3 | Alfred workflow for XTO. 4 | 5 | > Be sure you have your Node.js / io.js installed. 6 | 7 | ## Screenshot 8 | 9 | ![XTO](https://dm.nbut.ac.cn/github/xto.gif) 10 | 11 | ## Download 12 | 13 | [Click me!](https://github.com/XadillaX/workflow-xto/blob/master/xto.alfredworkflow?raw=true) 14 | 15 | -------------------------------------------------------------------------------- /action.js: -------------------------------------------------------------------------------- 1 | /** 2 | * XadillaX created at 2015-02-13 16:32:36 3 | * 4 | * Copyright (c) 2015 Huaban.com, all rights 5 | * reserved 6 | */ 7 | require("sugar"); 8 | var fs = require("fs-extra"); 9 | var spawn = require("child_process").spawn; 10 | var common = require("./common"); 11 | 12 | function clean(company) { 13 | console.log(company); 14 | 15 | if(!company) { 16 | return fs.remove(common.HISTORY_PATH, function(err) { 17 | // 有 error 也没事... 18 | err = null; 19 | }); 20 | } 21 | 22 | fs.readJson(common.HISTORY_PATH, function(err, json) { 23 | if(err) { 24 | return; 25 | } 26 | 27 | if(!json) return; 28 | 29 | delete json[company]; 30 | fs.writeJson(common.HISTORY_PATH, json, function(err) { 31 | // 有 error 也没事... 32 | err = null; 33 | }); 34 | }); 35 | } 36 | 37 | module.exports = function(arg) { 38 | var arg = JSON.parse(arg); 39 | 40 | switch(arg.cmd) { 41 | case "open": { 42 | spawn("open", arg.args); 43 | break; 44 | } 45 | 46 | case "clean": { 47 | clean(arg.code, arg.query); 48 | break; 49 | } 50 | 51 | default: break; 52 | } 53 | }; 54 | 55 | -------------------------------------------------------------------------------- /common.js: -------------------------------------------------------------------------------- 1 | /** 2 | * XadillaX created at 2015-02-13 16:53:54 3 | * 4 | * Copyright (c) 2015 Huaban.com, all rights 5 | * reserved 6 | */ 7 | var path = require("path"); 8 | 9 | exports.HISTORY_PATH = path.join(process.env.HOME, ".alfred/xto/history"); 10 | exports.SHOW_MAX_HISTORY = 8; 11 | 12 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XadillaX/workflow-xto/bb20f5f65279e841b205dddaa633a58061b734a7/icon.png -------------------------------------------------------------------------------- /info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | bundleid 6 | xto 7 | category 8 | Tools 9 | connections 10 | 11 | 2775D1AB-8ED9-4B79-A660-32B408C434B2 12 | 13 | 14 | destinationuid 15 | icon 16 | modifiers 17 | 0 18 | modifiersubtext 19 | 20 | 21 | 22 | icon 23 | 24 | 25 | destinationuid 26 | 075C692E-9785-4164-B62A-2475F05179AC 27 | modifiers 28 | 0 29 | modifiersubtext 30 | 31 | 32 | 33 | 34 | createdby 35 | XadillaX 36 | description 37 | Query for express. 38 | disabled 39 | 40 | name 41 | XTO 42 | objects 43 | 44 | 45 | config 46 | 47 | argumenttype 48 | 0 49 | escaping 50 | 63 51 | keyword 52 | xto 53 | runningsubtext 54 | 主人请骚等... 55 | script 56 | /usr/local/bin/node <<-'CODE' 57 | require("./xto")("{query}"); 58 | CODE 59 | subtext 60 | Nyaa~ 61 | title 62 | 查查 {query} 到哪了 63 | type 64 | 0 65 | withspace 66 | 67 | 68 | type 69 | alfred.workflow.input.scriptfilter 70 | uid 71 | icon 72 | version 73 | 0 74 | 75 | 76 | config 77 | 78 | escaping 79 | 63 80 | script 81 | /usr/local/bin/node <<-'CODE' 82 | require("./action")("{query}"); 83 | CODE 84 | type 85 | 0 86 | 87 | type 88 | alfred.workflow.output.script 89 | uid 90 | 075C692E-9785-4164-B62A-2475F05179AC 91 | version 92 | 0 93 | 94 | 95 | config 96 | 97 | action 98 | 0 99 | argument 100 | 0 101 | leftcursor 102 | 103 | modsmode 104 | 0 105 | relatedAppsMode 106 | 0 107 | 108 | type 109 | alfred.workflow.trigger.hotkey 110 | uid 111 | 2775D1AB-8ED9-4B79-A660-32B408C434B2 112 | version 113 | 1 114 | 115 | 116 | readme 117 | 118 | uidata 119 | 120 | 075C692E-9785-4164-B62A-2475F05179AC 121 | 122 | ypos 123 | 10 124 | 125 | 2775D1AB-8ED9-4B79-A660-32B408C434B2 126 | 127 | ypos 128 | 10 129 | 130 | F3408545-6500-4333-AEDB-AC51882C6A65 131 | 132 | ypos 133 | 10 134 | 135 | icon 136 | 137 | 138 | webaddress 139 | http://xcoder.in/ 140 | 141 | 142 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "workerflow-xto", 3 | "version": "1.0.6", 4 | "description": "The Alfred workflow of xto.", 5 | "main": "xto.js", 6 | "dependencies": { 7 | "alfred-item": "^1.0.1", 8 | "async": "^1.4.2", 9 | "fs-extra": "^0.23.1", 10 | "han": "^0.0.7", 11 | "sugar": "^1.4.1", 12 | "xto": "^1.1.7" 13 | }, 14 | "devDependencies": {}, 15 | "scripts": { 16 | "test": "echo \"Error: no test specified\" && exit 1" 17 | }, 18 | "author": "XadillaX", 19 | "license": "GPLv2" 20 | } 21 | -------------------------------------------------------------------------------- /xto.alfredworkflow: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XadillaX/workflow-xto/bb20f5f65279e841b205dddaa633a58061b734a7/xto.alfredworkflow -------------------------------------------------------------------------------- /xto.js: -------------------------------------------------------------------------------- 1 | /** 2 | * XadillaX created at 2015-02-12 12:41:10 3 | * 4 | * Copyright (c) 2015 Huaban.com, all rights 5 | * reserved 6 | */ 7 | require("sugar"); 8 | var xto = require("xto"); 9 | var companyMap = require("xto/const/namemapper"); 10 | var AlfredItem = require("alfred-item"); 11 | var han = require("han"); 12 | var async = require("async"); 13 | var fs = require("fs-extra"); 14 | var common = require("./common"); 15 | 16 | var _rawQuery; 17 | 18 | function writeResult(company, num, err, info) { 19 | if(err && (err.message.indexOf("不存在") < 0 || err.message.indexOf("过期") < 0)) { 20 | return; 21 | } 22 | 23 | async.waterfall([ 24 | function(callback) { 25 | fs.ensureFile(common.HISTORY_PATH, function(err) { 26 | callback(err); 27 | }); 28 | }, 29 | 30 | function(callback) { 31 | fs.readJson(common.HISTORY_PATH, function(err, json) { 32 | return callback(undefined, json || {}); 33 | }); 34 | }, 35 | 36 | function(orig, callback) { 37 | var code = company.code; 38 | if(undefined === orig[code]) orig[code] = []; 39 | var inner = orig[code].reduce(function(res, info) { 40 | if(info.num !== num) { 41 | res.push(info); 42 | } 43 | 44 | return res; 45 | }, []); 46 | 47 | var cur = { 48 | num: num 49 | }; 50 | var updateTime = Date.create().format("{yyyy}年{MM}月{dd}日 {HH}:{mm}:{ss}"); 51 | if(err) { 52 | cur.title = num + ":" + err.message; 53 | } else if(info !== undefined && info !== null) { 54 | cur.title = num + ":" + info.data[0].context; 55 | } 56 | 57 | if(!cur.title) return callback(undefined, orig); 58 | 59 | cur.subtitle = "最后更新:" + updateTime; 60 | if(info) { 61 | cur.subtitle = xto.stateToText(info.state) + "," + cur.subtitle; 62 | } 63 | cur.time = updateTime; 64 | cur.complete = company.shortname; 65 | if(cur.complete.indexOf(" ") >= 0) { 66 | cur.complete = company.code; 67 | } 68 | cur.complete += " " + num; 69 | inner.unshift(cur); 70 | 71 | orig[code] = inner; 72 | 73 | // while(orig[code].length > common.SHOW_MAX_HISTORY) orig[code].pop(); 74 | 75 | callback(undefined, orig); 76 | }, 77 | 78 | function(json, callback) { 79 | fs.writeJson(common.HISTORY_PATH, json, callback); 80 | } 81 | ], function(err) { 82 | // 有 err 也没办法 83 | if(err) { 84 | /** ignore */ 85 | err = null; 86 | } 87 | }); 88 | } 89 | 90 | function allHistoryToArray(json) { 91 | if(!json) return []; 92 | 93 | var arr = []; 94 | for(var key in json) { 95 | if(!json.hasOwnProperty(key)) continue; 96 | arr.add(json[key]); 97 | } 98 | return arr; 99 | } 100 | 101 | function outputWait() { 102 | var item = new AlfredItem(); 103 | item.addItem(0 + Math.random(), "查查 ... 到哪了", "Nyaa", "icon.png"); 104 | 105 | fs.readJson(common.HISTORY_PATH, function(err, json) { 106 | var arr = allHistoryToArray(json); 107 | 108 | arr = arr.sort(function(a, b) { 109 | return (a.time < b.time) ? 1 : (a.time > b.time ? -1 : 0); 110 | }); 111 | 112 | for(var i = 0; i < Math.min(arr.length, 7); i++) { 113 | item.addItem( 114 | i + 2 + Math.random(), 115 | "【" + arr[i].complete.split(" ")[0] + "】" + arr[i].title, 116 | arr[i].subtitle, 117 | "icon.png", { 118 | autocomplete: " " + arr[i].complete 119 | }); 120 | } 121 | 122 | item.addItem(1 + Math.random(), "清除所有历史运单记录...", "喵~", "icon.png", { 123 | $arg: [ 124 | { text: JSON.stringify({ cmd: "clean" }) } 125 | ] 126 | }); 127 | 128 | console.log(item); 129 | }); 130 | } 131 | 132 | function outputNoCompany(name) { 133 | var item = new AlfredItem(); 134 | item.addItem(0 + Math.random(), "快递公司 " + name + " 貌似不存在哦~", "请输入正确的快递公司", "icon.png"); 135 | console.log(item); 136 | } 137 | 138 | function outputError(err) { 139 | var item = new AlfredItem(); 140 | item.addItem(0 + Math.random(), err.message, "查询出错", "icon.png"); 141 | console.log(item); 142 | } 143 | 144 | function pinyinMatched(src, dist) { 145 | var _src = src.split(" ").compact(true); 146 | var pinyins = []; 147 | for(var i = 0; i < src.length; i++) { 148 | var temp = han.pinyin(_src[i]); 149 | for(var j = 0; j < temp.length; j++) { 150 | pinyins.push(temp[j]); 151 | } 152 | } 153 | 154 | if(pinyins.length < dist.length) return false; 155 | dist = dist.toLowerCase(); 156 | for(var i = 0; i < dist.length; i++) { 157 | if(typeof pinyins[i] === "string") { 158 | if(dist[i] !== pinyins[i][0].toLowerCase()) return false; 159 | continue; 160 | } 161 | 162 | var fit = false; 163 | for(var j = 0; j < pinyins[i].length; j++) { 164 | var first = pinyins[i][j][0]; 165 | if(first.toLowerCase() === dist[i]) { 166 | fit = true; 167 | break; 168 | } 169 | } 170 | if(!fit) return false; 171 | } 172 | 173 | return true; 174 | } 175 | 176 | function searchCompanies(name) { 177 | var results = []; 178 | for(var i = 0; i < companyMap.length; i++) { 179 | var company = companyMap[i]; 180 | if(company.companyname.startsWith(name) || 181 | company.code.startsWith(name) || 182 | company.shortname.startsWith(name) || 183 | company.url.startsWith(name)) { 184 | results.push(company); 185 | } else if(pinyinMatched(company.companyname, name) || 186 | pinyinMatched(company.shortname, name)) { 187 | results.push(company); 188 | } 189 | } 190 | 191 | return results; 192 | } 193 | 194 | function completeCompanies(name) { 195 | var results = searchCompanies(name); 196 | if(!results.length) return outputNoCompany(name); 197 | 198 | var item = new AlfredItem(); 199 | for(var i = 0; i < results.length; i++) { 200 | var company = results[i]; 201 | var displayName = company.shortname; 202 | if(displayName.indexOf(" ") >= 0) displayName = company.url; 203 | item.addItem( 204 | i + 1 + Math.random(), 205 | "【" + company.companyname + "】查查 ... 到哪了", 206 | company.companyname + " - " + company.shortname + " - " + company.code + " - " + company.url, 207 | "icon.png", { 208 | autocomplete: " " + displayName + " " 209 | }); 210 | } 211 | 212 | console.log(item); 213 | } 214 | 215 | function addClean(item, company) { 216 | item.addItem( 217 | item.count() + 1 + Math.random(), 218 | "清除【" + company.companyname + "】查询数据...", 219 | "要做好隐私保护工作哦~", 220 | "icon.png", { 221 | $arg: [{ 222 | text: JSON.stringify({ 223 | cmd: "clean", 224 | code: company.code, 225 | query: _rawQuery 226 | }) 227 | }] 228 | }); 229 | } 230 | 231 | function outputHistory(company, item, maxItems, prefix) { 232 | if(!maxItems) return console.log(item); 233 | 234 | prefix = prefix.toLowerCase(); 235 | fs.readJson(common.HISTORY_PATH, function(err, json) { 236 | if(json && json[company.code]) { 237 | var c = json[company.code]; 238 | for(var i = 0; i < Math.min(c.length, maxItems); i++) { 239 | if(!c[i].num.toLowerCase().startsWith(prefix)) continue; 240 | item.addItem( 241 | i + 2 + Math.random(), 242 | "【" + company.companyname + "】" + c[i].title, 243 | c[i].subtitle, 244 | "icon.png", { 245 | autocomplete: " " + c[i].complete 246 | }); 247 | } 248 | } 249 | 250 | addClean(item, company); 251 | 252 | return console.log(item); 253 | }); 254 | } 255 | 256 | module.exports = function(query) { 257 | _rawQuery = query; 258 | var rawQuery = query; 259 | var query = query.split(" ").compact(true); 260 | var item = new AlfredItem(); 261 | 262 | // 先查找快递公司 263 | if(!query.length) { 264 | return outputWait(); 265 | } 266 | 267 | var companyName = query[0]; 268 | var company = xto.getCompanyInfo(companyName); 269 | if(!company) { 270 | if(query.length === 1) { 271 | return completeCompanies(companyName); 272 | } else if(query.length > 1) { 273 | var companies = searchCompanies(companyName); 274 | if(companies.length !== 1) return outputNoCompany(companyName); 275 | company = companies[0]; 276 | } else { 277 | return outputNoCompany(companyName); 278 | } 279 | } 280 | 281 | if(query.length === 1) { 282 | var displayName = company.shortname; 283 | if(displayName.indexOf(" ") >= 0) displayName = company.code; 284 | item.addItem( 285 | 1 + Math.random(), 286 | "【" + company.companyname + "】查查 ... 到哪了", 287 | "请输入运单号", 288 | "icon.png", { 289 | autocomplete: " " + displayName + " " 290 | }); 291 | 292 | return outputHistory(company, item, rawQuery.endsWith(" ") ? common.SHOW_MAX_HISTORY : 0, ""); 293 | } 294 | 295 | var num = query[1]; 296 | 297 | // 检验正则 298 | if(!xto.isNumberValid(num, company)) { 299 | item.addItem( 300 | 1 + Math.random(), 301 | "【" + company.companyname + "】查查 " + num + " 到哪了", 302 | "等待输入完整运单号", 303 | "icon.png"); 304 | 305 | return outputHistory(company, item, common.SHOW_MAX_HISTORY - 1, num); 306 | } 307 | 308 | // 查询快递 309 | xto.query(num, company.code, function(err, info) { 310 | writeResult(company, num, err, info); 311 | 312 | if(err) { 313 | return outputError(err); 314 | } 315 | 316 | var state = xto.stateToText(info.state); 317 | item.addItem( 318 | 1 + Math.random(), 319 | company.companyname + ":" + state, 320 | "当前运单状态", 321 | "icon.png"); 322 | 323 | for(var i = 0; i < info.data.length; i++) { 324 | var data = info.data[i]; 325 | item.addItem((i + 2) + Math.random(), data.context, data.time, "icon.png"); 326 | } 327 | 328 | console.log(item); 329 | }); 330 | }; 331 | --------------------------------------------------------------------------------