├── .classpath ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs ├── org.eclipse.m2e.core.prefs └── org.maven.ide.eclipse.prefs ├── README.md ├── phantomjs-1.9.7-windows ├── LICENSE.BSD ├── README.md ├── chromedriver.exe ├── examples │ ├── arguments.coffee │ ├── arguments.js │ ├── child_process-examples.coffee │ ├── child_process-examples.js │ ├── colorwheel.coffee │ ├── colorwheel.js │ ├── countdown.coffee │ ├── countdown.js │ ├── detectsniff.coffee │ ├── detectsniff.js │ ├── direction.coffee │ ├── direction.js │ ├── echoToFile.coffee │ ├── echoToFile.js │ ├── features.coffee │ ├── features.js │ ├── fibo.coffee │ ├── fibo.js │ ├── follow.coffee │ ├── follow.js │ ├── hello.coffee │ ├── hello.js │ ├── imagebin.coffee │ ├── imagebin.js │ ├── injectme.coffee │ ├── injectme.js │ ├── ipgeocode.coffee │ ├── ipgeocode.js │ ├── loadspeed.coffee │ ├── loadspeed.js │ ├── loadurlwithoutcss.coffee │ ├── loadurlwithoutcss.js │ ├── modernizr.js │ ├── module.coffee │ ├── module.js │ ├── movies.coffee │ ├── movies.js │ ├── netlog.coffee │ ├── netlog.js │ ├── netsniff.coffee │ ├── netsniff.js │ ├── outputEncoding.coffee │ ├── outputEncoding.js │ ├── page_events.coffee │ ├── page_events.js │ ├── pagecallback.coffee │ ├── pagecallback.js │ ├── phantomwebintro.coffee │ ├── phantomwebintro.js │ ├── pizza.coffee │ ├── pizza.js │ ├── post.coffee │ ├── post.js │ ├── postserver.coffee │ ├── postserver.js │ ├── printenv.coffee │ ├── printenv.js │ ├── printheaderfooter.coffee │ ├── printheaderfooter.js │ ├── printmargins.coffee │ ├── printmargins.js │ ├── rasterize.coffee │ ├── rasterize.js │ ├── render_multi_url.coffee │ ├── render_multi_url.js │ ├── run-jasmine.coffee │ ├── run-jasmine.js │ ├── run-qunit.coffee │ ├── run-qunit.js │ ├── scandir.coffee │ ├── scandir.js │ ├── seasonfood.coffee │ ├── seasonfood.js │ ├── server.coffee │ ├── server.js │ ├── serverkeepalive.coffee │ ├── serverkeepalive.js │ ├── simpleserver.coffee │ ├── simpleserver.js │ ├── sleepsort.coffee │ ├── sleepsort.js │ ├── stdin-stdout-stderr.coffee │ ├── stdin-stdout-stderr.js │ ├── technews.coffee │ ├── technews.js │ ├── tweets.coffee │ ├── tweets.js │ ├── universe.js │ ├── unrandomize.coffee │ ├── unrandomize.js │ ├── useragent.coffee │ ├── useragent.js │ ├── version.coffee │ ├── version.js │ ├── waitfor.coffee │ ├── waitfor.js │ ├── walk_through_frames.coffee │ ├── walk_through_frames.js │ ├── weather.coffee │ └── weather.js ├── phantomjs.exe └── third-party.txt ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── unbank │ │ ├── Constants.java │ │ ├── Spider.java │ │ ├── berkeleydb │ │ └── JeTools.java │ │ ├── entity │ │ ├── News.java │ │ └── Weixin.java │ │ ├── fetch │ │ ├── Fetcher.java │ │ ├── HttpClientBuilder.java │ │ ├── PhantomjsFetcher.java │ │ └── SougouWeiXinFetcher.java │ │ ├── filter │ │ └── url │ │ │ └── SimpleBloomFilter.java │ │ ├── mina │ │ ├── clent │ │ │ ├── TLSClentHandler.java │ │ │ ├── TLSClinet.java │ │ │ ├── keeplive │ │ │ │ ├── ClentKeepAliveMessageFactoryImpl.java │ │ │ │ └── KeepAliveRequestTimeoutHandlerImpl.java │ │ │ └── listener │ │ │ │ └── IoListener.java │ │ ├── server │ │ │ ├── TLSServer.java │ │ │ ├── TLSServerHandler.java │ │ │ └── keeplive │ │ │ │ ├── KeepAliveMessageFactoryImpl.java │ │ │ │ └── KeepAliveRequestTimeoutHandlerImpl.java │ │ └── ssl │ │ │ ├── BogusSslContextFactory.java │ │ │ ├── BogusTrustManagerFactory.java │ │ │ ├── SslServerSocketFactory.java │ │ │ └── SslSocketFactory.java │ │ ├── mybatis │ │ ├── entity │ │ │ └── SQLAdapter.java │ │ ├── factory │ │ │ └── DynamicConnectionFactory.java │ │ └── mapper │ │ │ ├── SQLAdapterMapper.java │ │ │ └── SQLAdapterMapper.xml │ │ ├── quartz │ │ ├── SimpleBloomFilter.java │ │ ├── SougouSpider.java │ │ ├── SougouSpiderByChrome.java │ │ ├── StartWeiXinGongZhonghaoSpider.java │ │ └── WeixinImageFeatch.java │ │ ├── queue │ │ ├── BaseQueue.java │ │ └── InformationConsume.java │ │ ├── redis │ │ ├── JedisUtil.java │ │ └── RedisClient.java │ │ ├── spider │ │ └── weixinbigV │ │ │ ├── WeiXinGongZhongHaoPagePaser.java │ │ │ ├── WeiXinGongZhonghaoProducter.java │ │ │ ├── WeiXinGongZhonghaoSpider.java │ │ │ ├── WeixinGongZhonghaoListPaser.java │ │ │ └── WeixinGongZhonghaoReader.java │ │ ├── store │ │ ├── WeiXinStore.java │ │ ├── WeiXinStoreBySql.java │ │ └── WeiXinStoreToPlantform.java │ │ ├── tools │ │ └── MD5.java │ │ └── view │ │ ├── RightCornerPopMessage.java │ │ └── TranslucentFrame.java └── resources │ ├── applicationContext.xml │ ├── log4j.properties │ ├── mybatis.xml │ ├── userDefine.dic │ └── weixingongzhonghao.txt └── test └── java └── com └── unbank └── test ├── AppTest.java ├── CipherMessage.java ├── FetchTest.java ├── SougouTest.java ├── T.java ├── UbkAESUtil.java └── WeixinSpiderDemo.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ubk_weixinbysogou 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.common.project.facet.core.builder 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.m2e.core.maven2Builder 20 | 21 | 22 | 23 | 24 | 25 | org.eclipse.m2e.core.maven2Nature 26 | org.eclipse.jdt.core.javanature 27 | org.eclipse.wst.common.project.facet.core.nature 28 | 29 | 30 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.7 13 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=false 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.maven.ide.eclipse.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | fullBuildGoals=process-test-resources 4 | resolveWorkspaceProjects=true 5 | resourceFilterGoals=process-resources resources\:testResources 6 | skipCompilerPlugin=true 7 | version=1 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##ubk_weixinbysogou 2 | 一个根据搜狗微信进行微信公众号采集的程序 3 | 4 | 5 | ###2015年11月12日 6 | 7 | 整理了下代码,测试可用 8 | 9 | ###2015年9月15日 10 | 11 | 由于搜狗微信,内容页链接加密,导致httpclient 获取到的内容页链接不是微信原始链接,所以修改使用webdriver 进行爬取。 用httpclient 获取列表页还是可以的。 12 | 13 | ###以前 14 | 15 | 采用httpclient进行采集 -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/LICENSE.BSD: -------------------------------------------------------------------------------- 1 | Redistribution and use in source and binary forms, with or without 2 | modification, are permitted provided that the following conditions are met: 3 | 4 | * Redistributions of source code must retain the above copyright 5 | notice, this list of conditions and the following disclaimer. 6 | * Redistributions in binary form must reproduce the above copyright 7 | notice, this list of conditions and the following disclaimer in the 8 | documentation and/or other materials provided with the distribution. 9 | * Neither the name of the nor the 10 | names of its contributors may be used to endorse or promote products 11 | derived from this software without specific prior written permission. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 17 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/README.md: -------------------------------------------------------------------------------- 1 | # [PhantomJS](http://phantomjs.org) - Scriptable Headless WebKit 2 | 3 | PhantomJS ([www.phantomjs.org](http://phantomjs.org)) is a headless WebKit scriptable with JavaScript or CoffeeScript. It is used by hundreds of [developers](https://github.com/ariya/phantomjs/wiki/Buzz) and dozens of [organizations](https://github.com/ariya/phantomjs/wiki/Users) for web-related development workflow. 4 | 5 | The latest [stable release](http://phantomjs.org/release-1.9.html) is version 1.9 (codenamed "Sakura"). Follow the official Twitter stream [@PhantomJS](http://twitter.com/PhantomJS) to get the frequent development updates. 6 | 7 | **Note**: Please **do not** create a GitHub pull request **without** reading the [Contribution Guide](https://github.com/ariya/phantomjs/blob/master/CONTRIBUTING.md) first. Failure to do so may result in the rejection of the pull request. 8 | 9 | ## Use Cases 10 | 11 | - **Headless web testing**. Lightning-fast testing without the browser is now possible! Various [test frameworks](https://github.com/ariya/phantomjs/wiki/Headless-Testing) such as Jasmine, Capybara, QUnit, Mocha, WebDriver, YUI Test, BusterJS, FuncUnit, Robot Framework, and many others are supported. 12 | - **Page automation**. [Access and manipulate](https://github.com/ariya/phantomjs/wiki/Page-Automation) web pages with the standard DOM API, or with usual libraries like jQuery. 13 | - **Screen capture**. Programmatically [capture web contents](https://github.com/ariya/phantomjs/wiki/Screen-Capture), including CSs, SVG and Canvas. Build server-side web graphics apps, from a screenshot service to a vector chart rasterizer. 14 | - **Network monitoring**. Automate performance analysis, track [page loading](https://github.com/ariya/phantomjs/wiki/Network-Monitoring) and export as standard HAR format. 15 | 16 | ## Features 17 | 18 | - **Multiplatform**, available on major operating systems: Windows, Mac OS X, Linux, other Unices. 19 | - **Fast and native implementation** of web standards: DOM, CSS, JavaScript, Canvas, SVG. No emulation! 20 | - **Pure headless (no X11) on Linux**, ideal for continuous integration systems. Also runs on Amazon EC2, Heroku, Iron.io. 21 | - **Easy to install**: [Download](http://phantomjs.org/download.html), unpack, and start having fun in just 5 minutes. 22 | 23 | ## Ecosystem 24 | 25 | PhantomJS needs not be used only as a stand-alone tool. Check also some excellent related projects: 26 | 27 | - [CasperJS](http://casperjs.org) enables easy navigation scripting and common high-level testing. 28 | - [Poltergeist](https://github.com/jonleighton/poltergeist) allows running Capybara tests headlessly. 29 | - [Guard::Jasmine](https://github.com/netzpirat/guard-jasmine) automatically tests Jasmine specs on Rails when files are modified. 30 | - [GhostDriver](http://github.com/detro/ghostdriver/) complements Selenium tests with a PhantomJS WebDriver implementation. 31 | - [PhantomRobot](https://github.com/datakurre/phantomrobot) runs Robot Framework acceptance tests in the background via PhantomJS. 32 | - [Mocha-PhantomJS](https://github.com/metaskills/mocha-phantomjs) run Mocha tests using PhantomJS. 33 | 34 | and many others [related projects](https://github.com/ariya/phantomjs/wiki/Related-Projects). 35 | 36 | ## Questions? 37 | 38 | - Explore the complete [documentation](https://github.com/ariya/phantomjs/wiki) 39 | - Read tons of [user articles](https://github.com/ariya/phantomjs/wiki/Buzz) on using PhantomJS. 40 | - Join the [mailing-list](http://groups.google.com/group/phantomjs) and discuss with other PhantomJS fans. 41 | 42 | PhantomJS is free software/open source, and is distributed under the [BSD license](http://opensource.org/licenses/BSD-3-Clause). It contains third-party code, see the included `third-party.txt` file for the license information on third-party code. 43 | 44 | PhantomJS is created and maintained by [Ariya Hidayat](http://ariya.ofilabs.com/about) (Twitter: [@ariyahidayat](http://twitter.com/ariyahidayat)), with the help of [many contributors](https://github.com/ariya/phantomjs/contributors). 45 | 46 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liangyangtao/ubk_weixinbysogou/93fff8b83d702f222a7a93a0e1fba24cb88174b8/phantomjs-1.9.7-windows/chromedriver.exe -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/arguments.coffee: -------------------------------------------------------------------------------- 1 | system = require 'system' 2 | if system.args.length is 1 3 | console.log 'Try to pass some args when invoking this script!' 4 | else 5 | for arg, i in system.args 6 | console.log i + ': ' + arg 7 | phantom.exit() 8 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/arguments.js: -------------------------------------------------------------------------------- 1 | var system = require('system'); 2 | if (system.args.length === 1) { 3 | console.log('Try to pass some args when invoking this script!'); 4 | } else { 5 | system.args.forEach(function (arg, i) { 6 | console.log(i + ': ' + arg); 7 | }); 8 | } 9 | phantom.exit(); 10 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/child_process-examples.coffee: -------------------------------------------------------------------------------- 1 | {spawn, execFile} = require "child_process" 2 | 3 | child = spawn "ls", ["-lF", "/rooot"] 4 | 5 | child.stdout.on "data", (data) -> 6 | console.log "spawnSTDOUT:", JSON.stringify data 7 | 8 | child.stderr.on "data", (data) -> 9 | console.log "spawnSTDERR:", JSON.stringify data 10 | 11 | child.on "exit", (code) -> 12 | console.log "spawnEXIT:", code 13 | 14 | #child.kill "SIGKILL" 15 | 16 | execFile "ls", ["-lF", "/usr"], null, (err, stdout, stderr) -> 17 | console.log "execFileSTDOUT:", JSON.stringify stdout 18 | console.log "execFileSTDERR:", JSON.stringify stderr 19 | 20 | setTimeout (-> phantom.exit 0), 2000 21 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/child_process-examples.js: -------------------------------------------------------------------------------- 1 | var spawn = require("child_process").spawn 2 | var execFile = require("child_process").execFile 3 | 4 | var child = spawn("ls", ["-lF", "/rooot"]) 5 | 6 | child.stdout.on("data", function (data) { 7 | console.log("spawnSTDOUT:", JSON.stringify(data)) 8 | }) 9 | 10 | child.stderr.on("data", function (data) { 11 | console.log("spawnSTDERR:", JSON.stringify(data)) 12 | }) 13 | 14 | child.on("exit", function (code) { 15 | console.log("spawnEXIT:", code) 16 | }) 17 | 18 | //child.kill("SIGKILL") 19 | 20 | execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) { 21 | console.log("execFileSTDOUT:", JSON.stringify(stdout)) 22 | console.log("execFileSTDERR:", JSON.stringify(stderr)) 23 | }) 24 | 25 | setTimeout(function () { 26 | phantom.exit(0) 27 | }, 2000) 28 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/colorwheel.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | 3 | page.viewportSize = { width: 400, height : 400 } 4 | page.content = '' 5 | 6 | page.evaluate -> 7 | el = document.getElementById 'surface' 8 | context = el.getContext '2d' 9 | width = window.innerWidth 10 | height = window.innerHeight 11 | cx = width / 2 12 | cy = height / 2 13 | radius = width / 2.3 14 | i = 0 15 | 16 | el.width = width 17 | el.height = height 18 | imageData = context.createImageData(width, height) 19 | pixels = imageData.data 20 | 21 | for y in [0...height] 22 | for x in [0...width] 23 | i = i + 4 24 | rx = x - cx 25 | ry = y - cy 26 | d = rx * rx + ry * ry 27 | if d < radius * radius 28 | hue = 6 * (Math.atan2(ry, rx) + Math.PI) / (2 * Math.PI) 29 | sat = Math.sqrt(d) / radius 30 | g = Math.floor(hue) 31 | f = hue - g 32 | u = 255 * (1 - sat) 33 | v = 255 * (1 - sat * f) 34 | w = 255 * (1 - sat * (1 - f)) 35 | pixels[i] = [255, v, u, u, w, 255, 255][g] 36 | pixels[i + 1] = [w, 255, 255, v, u, u, w][g] 37 | pixels[i + 2] = [u, u, w, 255, 255, v, u][g] 38 | pixels[i + 3] = 255 39 | 40 | context.putImageData imageData, 0, 0 41 | document.body.style.backgroundColor = 'white' 42 | document.body.style.margin = '0px' 43 | 44 | page.render('colorwheel.png') 45 | 46 | phantom.exit() 47 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/colorwheel.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(); 2 | page.viewportSize = { width: 400, height : 400 }; 3 | page.content = ''; 4 | page.evaluate(function() { 5 | var el = document.getElementById('surface'), 6 | context = el.getContext('2d'), 7 | width = window.innerWidth, 8 | height = window.innerHeight, 9 | cx = width / 2, 10 | cy = height / 2, 11 | radius = width / 2.3, 12 | imageData, 13 | pixels, 14 | hue, sat, value, 15 | i = 0, x, y, rx, ry, d, 16 | f, g, p, u, v, w, rgb; 17 | 18 | el.width = width; 19 | el.height = height; 20 | imageData = context.createImageData(width, height); 21 | pixels = imageData.data; 22 | 23 | for (y = 0; y < height; y = y + 1) { 24 | for (x = 0; x < width; x = x + 1, i = i + 4) { 25 | rx = x - cx; 26 | ry = y - cy; 27 | d = rx * rx + ry * ry; 28 | if (d < radius * radius) { 29 | hue = 6 * (Math.atan2(ry, rx) + Math.PI) / (2 * Math.PI); 30 | sat = Math.sqrt(d) / radius; 31 | g = Math.floor(hue); 32 | f = hue - g; 33 | u = 255 * (1 - sat); 34 | v = 255 * (1 - sat * f); 35 | w = 255 * (1 - sat * (1 - f)); 36 | pixels[i] = [255, v, u, u, w, 255, 255][g]; 37 | pixels[i + 1] = [w, 255, 255, v, u, u, w][g]; 38 | pixels[i + 2] = [u, u, w, 255, 255, v, u][g]; 39 | pixels[i + 3] = 255; 40 | } 41 | } 42 | } 43 | 44 | context.putImageData(imageData, 0, 0); 45 | document.body.style.backgroundColor = 'white'; 46 | document.body.style.margin = '0px'; 47 | }); 48 | 49 | page.render('colorwheel.png'); 50 | 51 | phantom.exit(); 52 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/countdown.coffee: -------------------------------------------------------------------------------- 1 | t = 10 2 | interval = setInterval -> 3 | if t > 0 4 | console.log t-- 5 | else 6 | console.log 'BLAST OFF!' 7 | phantom.exit() 8 | , 1000 9 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/countdown.js: -------------------------------------------------------------------------------- 1 | var t = 10, 2 | interval = setInterval(function(){ 3 | if ( t > 0 ) { 4 | console.log(t--); 5 | } else { 6 | console.log("BLAST OFF!"); 7 | phantom.exit(); 8 | } 9 | }, 1000); 10 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/detectsniff.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | system = require 'system' 3 | 4 | page.onInitialized = -> 5 | page.evaluate -> 6 | userAgent = window.navigator.userAgent 7 | platform = window.navigator.platform 8 | window.navigator = 9 | appCodeName: 'Mozilla' 10 | appName: 'Netscape' 11 | cookieEnabled: false 12 | sniffed: false 13 | 14 | window.navigator.__defineGetter__ 'userAgent', -> 15 | window.navigator.sniffed = true 16 | userAgent 17 | 18 | window.navigator.__defineGetter__ 'platform', -> 19 | window.navigator.sniffed = true 20 | platform 21 | 22 | if system.args.length is 1 23 | console.log 'Usage: detectsniff.coffee ' 24 | phantom.exit 1 25 | else 26 | address = system.args[1] 27 | console.log 'Checking ' + address + '...' 28 | page.open address, (status) -> 29 | if status isnt 'success' 30 | console.log 'FAIL to load the address' 31 | phantom.exit() 32 | else 33 | window.setTimeout -> 34 | sniffed = page.evaluate(-> 35 | navigator.sniffed 36 | ) 37 | if sniffed 38 | console.log 'The page tried to sniff the user agent.' 39 | else 40 | console.log 'The page did not try to sniff the user agent.' 41 | phantom.exit() 42 | , 1500 43 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/detectsniff.js: -------------------------------------------------------------------------------- 1 | // Detect if a web page sniffs the user agent or not. 2 | 3 | var page = require('webpage').create(), 4 | system = require('system'), 5 | sniffed, 6 | address; 7 | 8 | page.onInitialized = function () { 9 | page.evaluate(function () { 10 | 11 | (function () { 12 | var userAgent = window.navigator.userAgent, 13 | platform = window.navigator.platform; 14 | 15 | window.navigator = { 16 | appCodeName: 'Mozilla', 17 | appName: 'Netscape', 18 | cookieEnabled: false, 19 | sniffed: false 20 | }; 21 | 22 | window.navigator.__defineGetter__('userAgent', function () { 23 | window.navigator.sniffed = true; 24 | return userAgent; 25 | }); 26 | 27 | window.navigator.__defineGetter__('platform', function () { 28 | window.navigator.sniffed = true; 29 | return platform; 30 | }); 31 | })(); 32 | }); 33 | }; 34 | 35 | if (system.args.length === 1) { 36 | console.log('Usage: detectsniff.js '); 37 | phantom.exit(1); 38 | } else { 39 | address = system.args[1]; 40 | console.log('Checking ' + address + '...'); 41 | page.open(address, function (status) { 42 | if (status !== 'success') { 43 | console.log('FAIL to load the address'); 44 | phantom.exit(); 45 | } else { 46 | window.setTimeout(function () { 47 | sniffed = page.evaluate(function () { 48 | return navigator.sniffed; 49 | }); 50 | if (sniffed) { 51 | console.log('The page tried to sniff the user agent.'); 52 | } else { 53 | console.log('The page did not try to sniff the user agent.'); 54 | } 55 | phantom.exit(); 56 | }, 1500); 57 | } 58 | }); 59 | } 60 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/direction.coffee: -------------------------------------------------------------------------------- 1 | # Get driving direction using Google Directions API. 2 | 3 | page = require('webpage').create() 4 | system = require 'system' 5 | 6 | if system.args.length < 3 7 | console.log 'Usage: direction.coffee origin destination' 8 | console.log 'Example: direction.coffee "San Diego" "Palo Alto"' 9 | phantom.exit 1 10 | else 11 | origin = system.args[1] 12 | dest = system.args[2] 13 | page.open encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin + 14 | '&destination=' + dest + '&units=imperial&mode=driving&sensor=false'), 15 | (status) -> 16 | if status isnt 'success' 17 | console.log 'Unable to access network' 18 | else 19 | steps = page.content.match(/(.*)<\/html_instructions>/ig) 20 | if not steps 21 | console.log 'No data available for ' + origin + ' to ' + dest 22 | else 23 | for ins in steps 24 | ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>') 25 | ins = ins.replace(/\
/g, '') 27 | console.log(ins) 28 | console.log '' 29 | console.log page.content.match(/.*<\/copyrights>/ig).join('').replace(/<.*?>/g, '') 30 | phantom.exit() 31 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/direction.js: -------------------------------------------------------------------------------- 1 | // Get driving direction using Google Directions API. 2 | 3 | var page = require('webpage').create(), 4 | system = require('system'), 5 | origin, dest, steps; 6 | 7 | if (system.args.length < 3) { 8 | console.log('Usage: direction.js origin destination'); 9 | console.log('Example: direction.js "San Diego" "Palo Alto"'); 10 | phantom.exit(1); 11 | } else { 12 | origin = system.args[1]; 13 | dest = system.args[2]; 14 | page.open(encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin + 15 | '&destination=' + dest + '&units=imperial&mode=driving&sensor=false'), function (status) { 16 | if (status !== 'success') { 17 | console.log('Unable to access network'); 18 | } else { 19 | steps = page.content.match(/(.*)<\/html_instructions>/ig); 20 | if (steps == null) { 21 | console.log('No data available for ' + origin + ' to ' + dest); 22 | } else { 23 | steps.forEach(function (ins) { 24 | ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>'); 25 | ins = ins.replace(/\
/g, ''); 27 | console.log(ins); 28 | }); 29 | console.log(''); 30 | console.log(page.content.match(/.*<\/copyrights>/ig).join('').replace(/<.*?>/g, '')); 31 | } 32 | } 33 | phantom.exit(); 34 | }); 35 | } 36 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/echoToFile.coffee: -------------------------------------------------------------------------------- 1 | # echoToFile.coffee - Write in a given file all the parameters passed on the CLI 2 | fs = require 'fs' 3 | system = require 'system' 4 | 5 | if system.args.length < 3 6 | console.log "Usage: echoToFile.coffee DESTINATION_FILE " 7 | phantom.exit 1 8 | else 9 | content = "" 10 | f = null 11 | i = 2 12 | while i < system.args.length 13 | content += system.args[i] + (if i == system.args.length - 1 then "" else " ") 14 | ++i 15 | try 16 | fs.write system.args[1], content, "w" 17 | catch e 18 | console.log e 19 | phantom.exit() 20 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/echoToFile.js: -------------------------------------------------------------------------------- 1 | // echoToFile.js - Write in a given file all the parameters passed on the CLI 2 | var fs = require('fs'), 3 | system = require('system'); 4 | 5 | if (system.args.length < 3) { 6 | console.log("Usage: echoToFile.js DESTINATION_FILE "); 7 | phantom.exit(1); 8 | } else { 9 | var content = '', 10 | f = null, 11 | i; 12 | for ( i= 2; i < system.args.length; ++i ) { 13 | content += system.args[i] + (i === system.args.length-1 ? '' : ' '); 14 | } 15 | 16 | try { 17 | fs.write(system.args[1], content, 'w'); 18 | } catch(e) { 19 | console.log(e); 20 | } 21 | 22 | phantom.exit(); 23 | } 24 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/features.coffee: -------------------------------------------------------------------------------- 1 | feature = undefined 2 | supported = [] 3 | unsupported = [] 4 | phantom.injectJs "modernizr.js" 5 | console.log "Detected features (using Modernizr " + Modernizr._version + "):" 6 | for feature of Modernizr 7 | if Modernizr.hasOwnProperty(feature) 8 | if feature[0] isnt "_" and typeof Modernizr[feature] isnt "function" and feature isnt "input" and feature isnt "inputtypes" 9 | if Modernizr[feature] 10 | supported.push feature 11 | else 12 | unsupported.push feature 13 | console.log "" 14 | console.log "Supported:" 15 | supported.forEach (e) -> 16 | console.log " " + e 17 | 18 | console.log "" 19 | console.log "Not supported:" 20 | unsupported.forEach (e) -> 21 | console.log " " + e 22 | 23 | phantom.exit() -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/features.js: -------------------------------------------------------------------------------- 1 | var feature, supported = [], unsupported = []; 2 | 3 | phantom.injectJs('modernizr.js'); 4 | console.log('Detected features (using Modernizr ' + Modernizr._version + '):'); 5 | for (feature in Modernizr) { 6 | if (Modernizr.hasOwnProperty(feature)) { 7 | if (feature[0] !== '_' && typeof Modernizr[feature] !== 'function' && 8 | feature !== 'input' && feature !== 'inputtypes') { 9 | if (Modernizr[feature]) { 10 | supported.push(feature); 11 | } else { 12 | unsupported.push(feature); 13 | } 14 | } 15 | } 16 | } 17 | 18 | console.log(''); 19 | console.log('Supported:'); 20 | supported.forEach(function (e) { 21 | console.log(' ' + e); 22 | }); 23 | 24 | console.log(''); 25 | console.log('Not supported:'); 26 | unsupported.forEach(function (e) { 27 | console.log(' ' + e); 28 | }); 29 | phantom.exit(); 30 | 31 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/fibo.coffee: -------------------------------------------------------------------------------- 1 | fibs = [0, 1] 2 | f = -> 3 | console.log fibs[fibs.length - 1] 4 | fibs.push fibs[fibs.length - 1] + fibs[fibs.length - 2] 5 | if fibs.length > 10 6 | window.clearInterval ticker 7 | phantom.exit() 8 | ticker = window.setInterval(f, 300) 9 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/fibo.js: -------------------------------------------------------------------------------- 1 | var fibs = [0, 1]; 2 | var ticker = window.setInterval(function () { 3 | console.log(fibs[fibs.length - 1]); 4 | fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]); 5 | if (fibs.length > 10) { 6 | window.clearInterval(ticker); 7 | phantom.exit(); 8 | } 9 | }, 300); 10 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/follow.coffee: -------------------------------------------------------------------------------- 1 | # List following and followers from several accounts 2 | 3 | users = [ 4 | 'PhantomJS' 5 | 'ariyahidayat' 6 | 'detronizator' 7 | 'KDABQt' 8 | 'lfranchi' 9 | 'jonleighton' 10 | '_jamesmgreene' 11 | 'Vitalliumm' 12 | ] 13 | 14 | follow = (user, callback) -> 15 | page = require('webpage').create() 16 | page.open 'http://mobile.twitter.com/' + user, (status) -> 17 | if status is 'fail' 18 | console.log user + ': ?' 19 | else 20 | data = page.evaluate -> document.querySelector('div.profile td.stat.stat-last div.statnum').innerText; 21 | console.log user + ': ' + data 22 | page.close() 23 | callback.apply() 24 | 25 | process = () -> 26 | if (users.length > 0) 27 | user = users[0] 28 | users.splice(0, 1) 29 | follow(user, process) 30 | else 31 | phantom.exit() 32 | 33 | process() 34 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/follow.js: -------------------------------------------------------------------------------- 1 | // List following and followers from several accounts 2 | 3 | var users = ['PhantomJS', 4 | 'ariyahidayat', 5 | 'detronizator', 6 | 'KDABQt', 7 | 'lfranchi', 8 | 'jonleighton', 9 | '_jamesmgreene', 10 | 'Vitalliumm']; 11 | 12 | function follow(user, callback) { 13 | var page = require('webpage').create(); 14 | page.open('http://mobile.twitter.com/' + user, function (status) { 15 | if (status === 'fail') { 16 | console.log(user + ': ?'); 17 | } else { 18 | var data = page.evaluate(function () { 19 | return document.querySelector('div.profile td.stat.stat-last div.statnum').innerText; 20 | }); 21 | console.log(user + ': ' + data); 22 | } 23 | page.close(); 24 | callback.apply(); 25 | }); 26 | } 27 | 28 | function process() { 29 | if (users.length > 0) { 30 | var user = users[0]; 31 | users.splice(0, 1); 32 | follow(user, process); 33 | } else { 34 | phantom.exit(); 35 | } 36 | } 37 | 38 | process(); 39 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/hello.coffee: -------------------------------------------------------------------------------- 1 | console.log 'Hello, world!' 2 | phantom.exit() 3 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/hello.js: -------------------------------------------------------------------------------- 1 | console.log('Hello, world!'); 2 | phantom.exit(); 3 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/imagebin.coffee: -------------------------------------------------------------------------------- 1 | # Upload an image to imagebin.org 2 | 3 | page = require('webpage').create() 4 | system = require 'system' 5 | 6 | if system.args.length isnt 2 7 | console.log 'Usage: imagebin.coffee filename' 8 | phantom.exit 1 9 | else 10 | fname = system.args[1] 11 | page.open 'http://imagebin.org/index.php?page=add', -> 12 | page.uploadFile 'input[name=image]', fname 13 | page.evaluate -> 14 | document.querySelector('input[name=nickname]').value = 'phantom' 15 | document.querySelector('input[name=disclaimer_agree]').click() 16 | document.querySelector('form').submit() 17 | 18 | window.setTimeout -> 19 | phantom.exit() 20 | , 3000 21 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/imagebin.js: -------------------------------------------------------------------------------- 1 | // Upload an image to imagebin.org 2 | 3 | var page = require('webpage').create(), 4 | system = require('system'), 5 | fname; 6 | 7 | if (system.args.length !== 2) { 8 | console.log('Usage: imagebin.js filename'); 9 | phantom.exit(1); 10 | } else { 11 | fname = system.args[1]; 12 | page.open("http://imagebin.org/index.php?page=add", function () { 13 | page.uploadFile('input[name=image]', fname); 14 | page.evaluate(function () { 15 | document.querySelector('input[name=nickname]').value = 'phantom'; 16 | document.querySelector('input[name=disclaimer_agree]').click() 17 | document.querySelector('form').submit(); 18 | }); 19 | window.setTimeout(function () { 20 | phantom.exit(); 21 | }, 3000); 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/injectme.coffee: -------------------------------------------------------------------------------- 1 | # Use 'page.injectJs()' to load the script itself in the Page context 2 | 3 | if phantom? 4 | page = require('webpage').create() 5 | 6 | # Route "console.log()" calls from within the Page context to the main 7 | # Phantom context (i.e. current "this") 8 | page.onConsoleMessage = (msg) -> console.log(msg) 9 | 10 | page.onAlert = (msg) -> console.log(msg) 11 | 12 | console.log "* Script running in the Phantom context." 13 | console.log "* Script will 'inject' itself in a page..." 14 | page.open "about:blank", (status) -> 15 | if status is "success" 16 | if page.injectJs("injectme.coffee") 17 | console.log "... done injecting itself!" 18 | else 19 | console.log "... fail! Check the $PWD?!" 20 | phantom.exit() 21 | else 22 | alert "* Script running in the Page context." 23 | 24 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/injectme.js: -------------------------------------------------------------------------------- 1 | // Use 'page.injectJs()' to load the script itself in the Page context 2 | 3 | if ( typeof(phantom) !== "undefined" ) { 4 | var page = require('webpage').create(); 5 | 6 | // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 7 | page.onConsoleMessage = function(msg) { 8 | console.log(msg); 9 | }; 10 | 11 | page.onAlert = function(msg) { 12 | console.log(msg); 13 | }; 14 | 15 | console.log("* Script running in the Phantom context."); 16 | console.log("* Script will 'inject' itself in a page..."); 17 | page.open("about:blank", function(status) { 18 | if ( status === "success" ) { 19 | console.log(page.injectJs("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!"); 20 | } 21 | phantom.exit(); 22 | }); 23 | } else { 24 | alert("* Script running in the Page context."); 25 | } 26 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/ipgeocode.coffee: -------------------------------------------------------------------------------- 1 | # Give the estimated location based on the IP address. 2 | 3 | window.cb = (data) -> 4 | loc = data.city 5 | if data.region_name.length > 0 6 | loc = loc + ', ' + data.region_name 7 | console.log 'IP address: ' + data.ip 8 | console.log 'Estimated location: ' + loc 9 | phantom.exit() 10 | 11 | el = document.createElement 'script' 12 | el.src = 'http://freegeoip.net/json/?callback=window.cb' 13 | document.body.appendChild el 14 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/ipgeocode.js: -------------------------------------------------------------------------------- 1 | // Give the estimated location based on the IP address. 2 | 3 | cb = function (data) { 4 | var loc = data.city; 5 | if (data.region_name.length > 0) 6 | loc = loc + ', ' + data.region_name; 7 | console.log('IP address: ' + data.ip); 8 | console.log('Estimated location: ' + loc); 9 | phantom.exit(); 10 | }; 11 | 12 | var el = document.createElement('script'); 13 | el.src = 'http://freegeoip.net/json/?callback=cb'; 14 | document.body.appendChild(el); 15 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/loadspeed.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | system = require 'system' 3 | 4 | if system.args.length is 1 5 | console.log 'Usage: loadspeed.coffee ' 6 | phantom.exit 1 7 | else 8 | t = Date.now() 9 | address = system.args[1] 10 | page.open address, (status) -> 11 | if status isnt 'success' 12 | console.log('FAIL to load the address') 13 | else 14 | t = Date.now() - t 15 | console.log('Page title is ' + page.evaluate( (-> document.title) )) 16 | console.log('Loading time ' + t + ' msec') 17 | phantom.exit() 18 | 19 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/loadspeed.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'), 3 | t, address; 4 | 5 | if (system.args.length === 1) { 6 | console.log('Usage: loadspeed.js '); 7 | phantom.exit(1); 8 | } else { 9 | t = Date.now(); 10 | address = system.args[1]; 11 | page.open(address, function (status) { 12 | if (status !== 'success') { 13 | console.log('FAIL to load the address'); 14 | } else { 15 | t = Date.now() - t; 16 | console.log('Page title is ' + page.evaluate(function () { 17 | return document.title; 18 | })); 19 | console.log('Loading time ' + t + ' msec'); 20 | } 21 | phantom.exit(); 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/loadurlwithoutcss.coffee: -------------------------------------------------------------------------------- 1 | page = require("webpage").create() 2 | system = require("system") 3 | 4 | if system.args.length < 2 5 | console.log "Usage: loadurlwithoutcss.js URL" 6 | phantom.exit() 7 | 8 | address = system.args[1] 9 | 10 | page.onResourceRequested = (requestData, request) -> 11 | if (/http:\/\/.+?\.css/g).test(requestData["url"]) or requestData["Content-Type"] is "text/css" 12 | console.log "The url of the request is matching. Aborting: " + requestData["url"] 13 | request.abort() 14 | 15 | page.open address, (status) -> 16 | if status is "success" 17 | phantom.exit() 18 | else 19 | console.log "Unable to load the address!" 20 | phantom.exit() 21 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/loadurlwithoutcss.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'); 3 | 4 | if (system.args.length < 2) { 5 | console.log('Usage: loadurlwithoutcss.js URL'); 6 | phantom.exit(); 7 | } 8 | 9 | var address = system.args[1]; 10 | 11 | page.onResourceRequested = function(requestData, request) { 12 | if ((/http:\/\/.+?\.css/gi).test(requestData['url']) || requestData['Content-Type'] == 'text/css') { 13 | console.log('The url of the request is matching. Aborting: ' + requestData['url']); 14 | request.abort(); 15 | } 16 | }; 17 | 18 | page.open(address, function(status) { 19 | if (status === 'success') { 20 | phantom.exit(); 21 | } else { 22 | console.log('Unable to load the address!'); 23 | phantom.exit(); 24 | } 25 | }); -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/module.coffee: -------------------------------------------------------------------------------- 1 | universe = require './universe' 2 | universe.start() 3 | console.log 'The answer is' + universe.answer 4 | phantom.exit() 5 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/module.js: -------------------------------------------------------------------------------- 1 | var universe = require('./universe'); 2 | universe.start(); 3 | console.log('The answer is' + universe.answer); 4 | phantom.exit(); 5 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/movies.coffee: -------------------------------------------------------------------------------- 1 | # List movies from kids-in-mind.com 2 | 3 | window.cbfunc = (data) -> 4 | globaldata = data 5 | list = data.query.results.movie 6 | for item in list 7 | console.log item.title + ' [' + item.rating.MPAA.content + ']' 8 | phantom.exit() 9 | 10 | el = document.createElement 'script' 11 | el.src = 12 | "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20movies.kids-in-mind&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=window.cbfunc" 13 | document.body.appendChild el 14 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/movies.js: -------------------------------------------------------------------------------- 1 | // List movies from kids-in-mind.com 2 | 3 | var cbfunc = function (data) { 4 | globaldata= data; 5 | var list = data.query.results.movie; 6 | list.forEach(function (item) { 7 | console.log(item.title + ' [' + item.rating.MPAA.content + ']'); 8 | }); 9 | phantom.exit(); 10 | }; 11 | 12 | var el = document.createElement('script'); 13 | el.src = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20movies.kids-in-mind&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc'; 14 | document.body.appendChild(el); 15 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/netlog.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | system = require 'system' 3 | 4 | if system.args.length is 1 5 | console.log 'Usage: netlog.coffee ' 6 | phantom.exit 1 7 | else 8 | address = system.args[1] 9 | page.onResourceRequested = (req) -> 10 | console.log 'requested ' + JSON.stringify(req, undefined, 4) 11 | 12 | page.onResourceReceived = (res) -> 13 | console.log 'received ' + JSON.stringify(res, undefined, 4) 14 | 15 | page.open address, (status) -> 16 | if status isnt 'success' 17 | console.log 'FAIL to load the address' 18 | phantom.exit() 19 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/netlog.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'), 3 | address; 4 | 5 | if (system.args.length === 1) { 6 | console.log('Usage: netlog.js '); 7 | phantom.exit(1); 8 | } else { 9 | address = system.args[1]; 10 | 11 | page.onResourceRequested = function (req) { 12 | console.log('requested: ' + JSON.stringify(req, undefined, 4)); 13 | }; 14 | 15 | page.onResourceReceived = function (res) { 16 | console.log('received: ' + JSON.stringify(res, undefined, 4)); 17 | }; 18 | 19 | page.open(address, function (status) { 20 | if (status !== 'success') { 21 | console.log('FAIL to load the address'); 22 | } 23 | phantom.exit(); 24 | }); 25 | } 26 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/netsniff.coffee: -------------------------------------------------------------------------------- 1 | if not Date::toISOString 2 | Date::toISOString = -> 3 | pad = (n) -> 4 | if n < 10 then '0' + n else n 5 | ms = (n) -> 6 | if n < 10 then '00' + n else (if n < 100 then '0' + n else n) 7 | @getFullYear() + '-' + 8 | pad(@getMonth() + 1) + '-' + 9 | pad(@getDate()) + 'T' + 10 | pad(@getHours()) + ':' + 11 | pad(@getMinutes()) + ':' + 12 | pad(@getSeconds()) + '.' + 13 | ms(@getMilliseconds()) + 'Z' 14 | 15 | createHAR = (address, title, startTime, resources) -> 16 | entries = [] 17 | 18 | resources.forEach (resource) -> 19 | request = resource.request 20 | startReply = resource.startReply 21 | endReply = resource.endReply 22 | 23 | if not request or not startReply or not endReply 24 | return 25 | 26 | entries.push 27 | startedDateTime: request.time.toISOString() 28 | time: endReply.time - request.time 29 | request: 30 | method: request.method 31 | url: request.url 32 | httpVersion: 'HTTP/1.1' 33 | cookies: [] 34 | headers: request.headers 35 | queryString: [] 36 | headersSize: -1 37 | bodySize: -1 38 | 39 | response: 40 | status: endReply.status 41 | statusText: endReply.statusText 42 | httpVersion: 'HTTP/1.1' 43 | cookies: [] 44 | headers: endReply.headers 45 | redirectURL: '' 46 | headersSize: -1 47 | bodySize: startReply.bodySize 48 | content: 49 | size: startReply.bodySize 50 | mimeType: endReply.contentType 51 | 52 | cache: {} 53 | timings: 54 | blocked: 0 55 | dns: -1 56 | connect: -1 57 | send: 0 58 | wait: startReply.time - request.time 59 | receive: endReply.time - startReply.time 60 | ssl: -1 61 | pageref: address 62 | 63 | log: 64 | version: '1.2' 65 | creator: 66 | name: 'PhantomJS' 67 | version: phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch 68 | 69 | pages: [ 70 | startedDateTime: startTime.toISOString() 71 | id: address 72 | title: title 73 | pageTimings: 74 | onLoad: page.endTime - page.startTime 75 | ] 76 | entries: entries 77 | 78 | page = require('webpage').create() 79 | system = require 'system' 80 | 81 | if system.args.length is 1 82 | console.log 'Usage: netsniff.coffee ' 83 | phantom.exit 1 84 | else 85 | page.address = system.args[1] 86 | page.resources = [] 87 | 88 | page.onLoadStarted = -> 89 | page.startTime = new Date() 90 | 91 | page.onResourceRequested = (req) -> 92 | page.resources[req.id] = 93 | request: req 94 | startReply: null 95 | endReply: null 96 | 97 | page.onResourceReceived = (res) -> 98 | if res.stage is 'start' 99 | page.resources[res.id].startReply = res 100 | if res.stage is 'end' 101 | page.resources[res.id].endReply = res 102 | 103 | page.open page.address, (status) -> 104 | if status isnt 'success' 105 | console.log 'FAIL to load the address' 106 | phantom.exit(1) 107 | else 108 | page.endTime = new Date() 109 | page.title = page.evaluate -> 110 | document.title 111 | 112 | har = createHAR page.address, page.title, page.startTime, page.resources 113 | console.log JSON.stringify har, undefined, 4 114 | phantom.exit() 115 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/outputEncoding.coffee: -------------------------------------------------------------------------------- 1 | helloWorld = () -> console.log phantom.outputEncoding + ": こんにちは、世界!" 2 | 3 | console.log "Using default encoding..." 4 | helloWorld() 5 | 6 | console.log "\nUsing other encodings..." 7 | for enc in ["euc-jp", "sjis", "utf8", "System"] 8 | do (enc) -> 9 | phantom.outputEncoding = enc 10 | helloWorld() 11 | 12 | phantom.exit() 13 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/outputEncoding.js: -------------------------------------------------------------------------------- 1 | function helloWorld() { 2 | console.log(phantom.outputEncoding + ": こんにちは、世界!"); 3 | } 4 | 5 | console.log("Using default encoding..."); 6 | helloWorld(); 7 | 8 | console.log("\nUsing other encodings..."); 9 | 10 | var encodings = ["euc-jp", "sjis", "utf8", "System"]; 11 | for (var i = 0; i < encodings.length; i++) { 12 | phantom.outputEncoding = encodings[i]; 13 | helloWorld(); 14 | } 15 | 16 | phantom.exit() 17 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/page_events.coffee: -------------------------------------------------------------------------------- 1 | # The purpose of this is to show how and when events fire, considering 5 steps 2 | # happening as follows: 3 | # 4 | # 1. Load URL 5 | # 2. Load same URL, but adding an internal FRAGMENT to it 6 | # 3. Click on an internal Link, that points to another internal FRAGMENT 7 | # 4. Click on an external Link, that will send the page somewhere else 8 | # 5. Close page 9 | # 10 | # Take particular care when going through the output, to understand when 11 | # things happen (and in which order). Particularly, notice what DOESN'T 12 | # happen during step 3. 13 | # 14 | # If invoked with "-v" it will print out the Page Resources as they are 15 | # Requested and Received. 16 | # 17 | # NOTE.1: The "onConsoleMessage/onAlert/onPrompt/onConfirm" events are 18 | # registered but not used here. This is left for you to have fun with. 19 | # NOTE.2: This script is not here to teach you ANY JavaScript. It's aweful! 20 | # NOTE.3: Main audience for this are people new to PhantomJS. 21 | printArgs = -> 22 | i = undefined 23 | ilen = undefined 24 | i = 0 25 | ilen = arguments_.length 26 | 27 | while i < ilen 28 | console.log " arguments[" + i + "] = " + JSON.stringify(arguments_[i]) 29 | ++i 30 | console.log "" 31 | sys = require("system") 32 | page = require("webpage").create() 33 | logResources = false 34 | step1url = "http://en.wikipedia.org/wiki/DOM_events" 35 | step2url = "http://en.wikipedia.org/wiki/DOM_events#Event_flow" 36 | logResources = true if sys.args.length > 1 and sys.args[1] is "-v" 37 | 38 | #////////////////////////////////////////////////////////////////////////////// 39 | page.onInitialized = -> 40 | console.log "page.onInitialized" 41 | printArgs.apply this, arguments_ 42 | 43 | page.onLoadStarted = -> 44 | console.log "page.onLoadStarted" 45 | printArgs.apply this, arguments_ 46 | 47 | page.onLoadFinished = -> 48 | console.log "page.onLoadFinished" 49 | printArgs.apply this, arguments_ 50 | 51 | page.onUrlChanged = -> 52 | console.log "page.onUrlChanged" 53 | printArgs.apply this, arguments_ 54 | 55 | page.onNavigationRequested = -> 56 | console.log "page.onNavigationRequested" 57 | printArgs.apply this, arguments_ 58 | 59 | if logResources is true 60 | page.onResourceRequested = -> 61 | console.log "page.onResourceRequested" 62 | printArgs.apply this, arguments_ 63 | 64 | page.onResourceReceived = -> 65 | console.log "page.onResourceReceived" 66 | printArgs.apply this, arguments_ 67 | page.onClosing = -> 68 | console.log "page.onClosing" 69 | printArgs.apply this, arguments_ 70 | 71 | 72 | # window.console.log(msg); 73 | page.onConsoleMessage = -> 74 | console.log "page.onConsoleMessage" 75 | printArgs.apply this, arguments_ 76 | 77 | 78 | # window.alert(msg); 79 | page.onAlert = -> 80 | console.log "page.onAlert" 81 | printArgs.apply this, arguments_ 82 | 83 | 84 | # var confirmed = window.confirm(msg); 85 | page.onConfirm = -> 86 | console.log "page.onConfirm" 87 | printArgs.apply this, arguments_ 88 | 89 | 90 | # var user_value = window.prompt(msg, default_value); 91 | page.onPrompt = -> 92 | console.log "page.onPrompt" 93 | printArgs.apply this, arguments_ 94 | 95 | 96 | #////////////////////////////////////////////////////////////////////////////// 97 | setTimeout (-> 98 | console.log "" 99 | console.log "### STEP 1: Load '" + step1url + "'" 100 | page.open step1url 101 | ), 0 102 | setTimeout (-> 103 | console.log "" 104 | console.log "### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)" 105 | page.open step2url 106 | ), 5000 107 | setTimeout (-> 108 | console.log "" 109 | console.log "### STEP 3: Click on page internal link (aka FRAGMENT)" 110 | page.evaluate -> 111 | ev = document.createEvent("MouseEvents") 112 | ev.initEvent "click", true, true 113 | document.querySelector("a[href='#Event_object']").dispatchEvent ev 114 | 115 | ), 10000 116 | setTimeout (-> 117 | console.log "" 118 | console.log "### STEP 4: Click on page external link" 119 | page.evaluate -> 120 | ev = document.createEvent("MouseEvents") 121 | ev.initEvent "click", true, true 122 | document.querySelector("a[title='JavaScript']").dispatchEvent ev 123 | 124 | ), 15000 125 | setTimeout (-> 126 | console.log "" 127 | console.log "### STEP 5: Close page and shutdown (with a delay)" 128 | page.close() 129 | setTimeout (-> 130 | phantom.exit() 131 | ), 100 132 | ), 20000 -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/pagecallback.coffee: -------------------------------------------------------------------------------- 1 | p = require("webpage").create() 2 | 3 | p.onConsoleMessage = (msg) -> 4 | console.log msg 5 | 6 | # Calls to "callPhantom" within the page 'p' arrive here 7 | p.onCallback = (msg) -> 8 | console.log "Received by the 'phantom' main context: " + msg 9 | "Hello there, I'm coming to you from the 'phantom' context instead" 10 | 11 | p.evaluate -> 12 | # Return-value of the "onCallback" handler arrive here 13 | callbackResponse = window.callPhantom "Hello, I'm coming to you from the 'page' context" 14 | console.log "Received by the 'page' context: " + callbackResponse 15 | 16 | phantom.exit() 17 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/pagecallback.js: -------------------------------------------------------------------------------- 1 | var p = require("webpage").create(); 2 | 3 | p.onConsoleMessage = function(msg) { console.log(msg); }; 4 | 5 | // Calls to "callPhantom" within the page 'p' arrive here 6 | p.onCallback = function(msg) { 7 | console.log("Received by the 'phantom' main context: "+msg); 8 | return "Hello there, I'm coming to you from the 'phantom' context instead"; 9 | }; 10 | 11 | p.evaluate(function() { 12 | // Return-value of the "onCallback" handler arrive here 13 | var callbackResponse = window.callPhantom("Hello, I'm coming to you from the 'page' context"); 14 | console.log("Received by the 'page' context: "+callbackResponse); 15 | }); 16 | 17 | phantom.exit(); 18 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/phantomwebintro.coffee: -------------------------------------------------------------------------------- 1 | # Read the Phantom webpage '#intro' element text using jQuery and "includeJs" 2 | 3 | page = require('webpage').create() 4 | 5 | page.onConsoleMessage = (msg) -> console.log msg 6 | 7 | page.open "http://www.phantomjs.org", (status) -> 8 | if status is "success" 9 | page.includeJs "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", -> 10 | page.evaluate -> 11 | console.log "$(\"#intro\").text() -> " + $("#intro").text() 12 | phantom.exit() 13 | 14 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/phantomwebintro.js: -------------------------------------------------------------------------------- 1 | // Read the Phantom webpage '#intro' element text using jQuery and "includeJs" 2 | 3 | var page = require('webpage').create(); 4 | 5 | page.onConsoleMessage = function(msg) { 6 | console.log(msg); 7 | }; 8 | 9 | page.open("http://www.phantomjs.org", function(status) { 10 | if ( status === "success" ) { 11 | page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() { 12 | page.evaluate(function() { 13 | console.log("$(\"#intro\").text() -> " + $("#intro").text()); 14 | }); 15 | phantom.exit(); 16 | }); 17 | } 18 | }); 19 | 20 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/pizza.coffee: -------------------------------------------------------------------------------- 1 | # Find pizza in Mountain View using Yelp 2 | 3 | page = require('webpage').create() 4 | url = 'http://lite.yelp.com/search?find_desc=pizza&find_loc=94040&find_submit=Search' 5 | 6 | page.open url, 7 | (status) -> 8 | if status isnt 'success' 9 | console.log 'Unable to access network' 10 | else 11 | results = page.evaluate -> 12 | pizza = [] 13 | list = document.querySelectorAll 'address' 14 | for item in list 15 | pizza.push(item.innerText) 16 | return pizza 17 | console.log results.join('\n') 18 | phantom.exit() 19 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/pizza.js: -------------------------------------------------------------------------------- 1 | // Find pizza in Mountain View using Yelp 2 | 3 | var page = require('webpage').create(), 4 | url = 'http://lite.yelp.com/search?find_desc=pizza&find_loc=94040&find_submit=Search'; 5 | 6 | page.open(url, function (status) { 7 | if (status !== 'success') { 8 | console.log('Unable to access network'); 9 | } else { 10 | var results = page.evaluate(function() { 11 | var list = document.querySelectorAll('address'), pizza = [], i; 12 | for (i = 0; i < list.length; i++) { 13 | pizza.push(list[i].innerText); 14 | } 15 | return pizza; 16 | }); 17 | console.log(results.join('\n')); 18 | } 19 | phantom.exit(); 20 | }); 21 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/post.coffee: -------------------------------------------------------------------------------- 1 | # Example using HTTP POST operation 2 | 3 | page = require('webpage').create() 4 | server = 'http://posttestserver.com/post.php?dump' 5 | data = 'universe=expanding&answer=42' 6 | 7 | page.open server, 'post', data, (status) -> 8 | if status isnt 'success' 9 | console.log 'Unable to post!' 10 | else 11 | console.log page.content 12 | phantom.exit() 13 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/post.js: -------------------------------------------------------------------------------- 1 | // Example using HTTP POST operation 2 | 3 | var page = require('webpage').create(), 4 | server = 'http://posttestserver.com/post.php?dump', 5 | data = 'universe=expanding&answer=42'; 6 | 7 | page.open(server, 'post', data, function (status) { 8 | if (status !== 'success') { 9 | console.log('Unable to post!'); 10 | } else { 11 | console.log(page.content); 12 | } 13 | phantom.exit(); 14 | }); 15 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/postserver.coffee: -------------------------------------------------------------------------------- 1 | # Example using HTTP POST operation 2 | page = require("webpage").create() 3 | server = require("webserver").create() 4 | system = require("system") 5 | data = "universe=expanding&answer=42" 6 | if system.args.length isnt 2 7 | console.log "Usage: postserver.js " 8 | phantom.exit 1 9 | port = system.args[1] 10 | service = server.listen(port, (request, response) -> 11 | console.log "Request received at " + new Date() 12 | response.statusCode = 200 13 | response.headers = 14 | Cache: "no-cache" 15 | "Content-Type": "text/plain;charset=utf-8" 16 | 17 | response.write JSON.stringify(request, null, 4) 18 | response.close() 19 | ) 20 | page.open "http://localhost:" + port + "/", "post", data, (status) -> 21 | if status isnt "success" 22 | console.log "Unable to post!" 23 | else 24 | console.log page.plainText 25 | phantom.exit() 26 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/postserver.js: -------------------------------------------------------------------------------- 1 | // Example using HTTP POST operation 2 | 3 | var page = require('webpage').create(), 4 | server = require('webserver').create(), 5 | system = require('system'), 6 | data = 'universe=expanding&answer=42'; 7 | 8 | if (system.args.length !== 2) { 9 | console.log('Usage: postserver.js '); 10 | phantom.exit(1); 11 | } 12 | 13 | var port = system.args[1]; 14 | 15 | service = server.listen(port, function (request, response) { 16 | console.log('Request received at ' + new Date()); 17 | 18 | response.statusCode = 200; 19 | response.headers = { 20 | 'Cache': 'no-cache', 21 | 'Content-Type': 'text/plain;charset=utf-8' 22 | }; 23 | response.write(JSON.stringify(request, null, 4)); 24 | response.close(); 25 | }); 26 | 27 | page.open('http://localhost:' + port + '/', 'post', data, function (status) { 28 | if (status !== 'success') { 29 | console.log('Unable to post!'); 30 | } else { 31 | console.log(page.plainText); 32 | } 33 | phantom.exit(); 34 | }); 35 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/printenv.coffee: -------------------------------------------------------------------------------- 1 | system = require("system") 2 | env = system.env 3 | key = undefined 4 | for key of env 5 | console.log key + "=" + env[key] if env.hasOwnProperty(key) 6 | phantom.exit() -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/printenv.js: -------------------------------------------------------------------------------- 1 | var system = require('system'), 2 | env = system.env, 3 | key; 4 | 5 | for (key in env) { 6 | if (env.hasOwnProperty(key)) { 7 | console.log(key + '=' + env[key]); 8 | } 9 | } 10 | phantom.exit(); 11 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/printheaderfooter.coffee: -------------------------------------------------------------------------------- 1 | someCallback = (pageNum, numPages) -> 2 | "

someCallback: " + pageNum + " / " + numPages + "

" 3 | page = require("webpage").create() 4 | system = require("system") 5 | if system.args.length < 3 6 | console.log "Usage: printheaderfooter.js URL filename" 7 | phantom.exit 1 8 | else 9 | address = system.args[1] 10 | output = system.args[2] 11 | page.viewportSize = 12 | width: 600 13 | height: 600 14 | 15 | page.paperSize = 16 | format: "A4" 17 | margin: "1cm" 18 | 19 | # default header/footer for pages that don't have custom overwrites (see below) 20 | header: 21 | height: "1cm" 22 | contents: phantom.callback((pageNum, numPages) -> 23 | return "" if pageNum is 1 24 | "

Header " + pageNum + " / " + numPages + "

" 25 | ) 26 | 27 | footer: 28 | height: "1cm" 29 | contents: phantom.callback((pageNum, numPages) -> 30 | return "" if pageNum is numPages 31 | "

Footer " + pageNum + " / " + numPages + "

" 32 | ) 33 | 34 | page.open address, (status) -> 35 | if status isnt "success" 36 | console.log "Unable to load the address!" 37 | else 38 | 39 | # check whether the loaded page overwrites the header/footer setting, 40 | # i.e. whether a PhantomJSPriting object exists. Use that then instead 41 | # of our defaults above. 42 | # 43 | # example: 44 | # 45 | # 46 | # 58 | # 59 | #

asdfadsf

asdfadsfycvx

60 | # 61 | # 62 | if page.evaluate(-> 63 | typeof PhantomJSPrinting is "object" 64 | ) 65 | paperSize = page.paperSize 66 | paperSize.header.height = page.evaluate(-> 67 | PhantomJSPrinting.header.height 68 | ) 69 | paperSize.header.contents = phantom.callback((pageNum, numPages) -> 70 | page.evaluate ((pageNum, numPages) -> 71 | PhantomJSPrinting.header.contents pageNum, numPages 72 | ), pageNum, numPages 73 | ) 74 | paperSize.footer.height = page.evaluate(-> 75 | PhantomJSPrinting.footer.height 76 | ) 77 | paperSize.footer.contents = phantom.callback((pageNum, numPages) -> 78 | page.evaluate ((pageNum, numPages) -> 79 | PhantomJSPrinting.footer.contents pageNum, numPages 80 | ), pageNum, numPages 81 | ) 82 | page.paperSize = paperSize 83 | console.log page.paperSize.header.height 84 | console.log page.paperSize.footer.height 85 | window.setTimeout (-> 86 | page.render output 87 | phantom.exit() 88 | ), 200 89 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/printheaderfooter.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'); 3 | 4 | function someCallback(pageNum, numPages) { 5 | return "

someCallback: " + pageNum + " / " + numPages + "

"; 6 | } 7 | 8 | if (system.args.length < 3) { 9 | console.log('Usage: printheaderfooter.js URL filename'); 10 | phantom.exit(1); 11 | } else { 12 | var address = system.args[1]; 13 | var output = system.args[2]; 14 | page.viewportSize = { width: 600, height: 600 }; 15 | page.paperSize = { 16 | format: 'A4', 17 | margin: "1cm", 18 | /* default header/footer for pages that don't have custom overwrites (see below) */ 19 | header: { 20 | height: "1cm", 21 | contents: phantom.callback(function(pageNum, numPages) { 22 | if (pageNum == 1) { 23 | return ""; 24 | } 25 | return "

Header " + pageNum + " / " + numPages + "

"; 26 | }) 27 | }, 28 | footer: { 29 | height: "1cm", 30 | contents: phantom.callback(function(pageNum, numPages) { 31 | if (pageNum == numPages) { 32 | return ""; 33 | } 34 | return "

Footer " + pageNum + " / " + numPages + "

"; 35 | }) 36 | } 37 | }; 38 | page.open(address, function (status) { 39 | if (status !== 'success') { 40 | console.log('Unable to load the address!'); 41 | } else { 42 | /* check whether the loaded page overwrites the header/footer setting, 43 | i.e. whether a PhantomJSPriting object exists. Use that then instead 44 | of our defaults above. 45 | 46 | example: 47 | 48 | 49 | 61 | 62 |

asdfadsf

asdfadsfycvx

63 | 64 | */ 65 | if (page.evaluate(function(){return typeof PhantomJSPrinting == "object";})) { 66 | paperSize = page.paperSize; 67 | paperSize.header.height = page.evaluate(function() { 68 | return PhantomJSPrinting.header.height; 69 | }); 70 | paperSize.header.contents = phantom.callback(function(pageNum, numPages) { 71 | return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.header.contents(pageNum, numPages);}, pageNum, numPages); 72 | }); 73 | paperSize.footer.height = page.evaluate(function() { 74 | return PhantomJSPrinting.footer.height; 75 | }); 76 | paperSize.footer.contents = phantom.callback(function(pageNum, numPages) { 77 | return page.evaluate(function(pageNum, numPages){return PhantomJSPrinting.footer.contents(pageNum, numPages);}, pageNum, numPages); 78 | }); 79 | page.paperSize = paperSize; 80 | console.log(page.paperSize.header.height); 81 | console.log(page.paperSize.footer.height); 82 | } 83 | window.setTimeout(function () { 84 | page.render(output); 85 | phantom.exit(); 86 | }, 200); 87 | } 88 | }); 89 | } 90 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/printmargins.coffee: -------------------------------------------------------------------------------- 1 | page = require("webpage").create() 2 | system = require("system") 3 | if system.args.length < 7 4 | console.log "Usage: printmargins.js URL filename LEFT TOP RIGHT BOTTOM" 5 | console.log " margin examples: \"1cm\", \"10px\", \"7mm\", \"5in\"" 6 | phantom.exit 1 7 | else 8 | address = system.args[1] 9 | output = system.args[2] 10 | marginLeft = system.args[3] 11 | marginTop = system.args[4] 12 | marginRight = system.args[5] 13 | marginBottom = system.args[6] 14 | page.viewportSize = 15 | width: 600 16 | height: 600 17 | 18 | page.paperSize = 19 | format: "A4" 20 | margin: 21 | left: marginLeft 22 | top: marginTop 23 | right: marginRight 24 | bottom: marginBottom 25 | 26 | page.open address, (status) -> 27 | if status isnt "success" 28 | console.log "Unable to load the address!" 29 | else 30 | window.setTimeout (-> 31 | page.render output 32 | phantom.exit() 33 | ), 200 34 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/printmargins.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'); 3 | 4 | if (system.args.length < 7) { 5 | console.log('Usage: printmargins.js URL filename LEFT TOP RIGHT BOTTOM'); 6 | console.log(' margin examples: "1cm", "10px", "7mm", "5in"'); 7 | phantom.exit(1); 8 | } else { 9 | var address = system.args[1]; 10 | var output = system.args[2]; 11 | var marginLeft = system.args[3]; 12 | var marginTop = system.args[4]; 13 | var marginRight = system.args[5]; 14 | var marginBottom = system.args[6]; 15 | page.viewportSize = { width: 600, height: 600 }; 16 | page.paperSize = { 17 | format: 'A4', 18 | margin: { 19 | left: marginLeft, 20 | top: marginTop, 21 | right: marginRight, 22 | bottom: marginBottom 23 | } 24 | }; 25 | page.open(address, function (status) { 26 | if (status !== 'success') { 27 | console.log('Unable to load the address!'); 28 | } else { 29 | window.setTimeout(function () { 30 | page.render(output); 31 | phantom.exit(); 32 | }, 200); 33 | } 34 | }); 35 | } 36 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/rasterize.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | system = require 'system' 3 | 4 | if system.args.length < 3 or system.args.length > 4 5 | console.log 'Usage: rasterize.coffee URL filename [paperwidth*paperheight|paperformat]' 6 | console.log ' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"' 7 | phantom.exit 1 8 | else 9 | address = system.args[1] 10 | output = system.args[2] 11 | page.viewportSize = { width: 600, height: 600 } 12 | if system.args.length is 4 and system.args[2].substr(-4) is ".pdf" 13 | size = system.args[3].split '*' 14 | if size.length is 2 15 | page.paperSize = { width: size[0], height: size[1], border: '0px' } 16 | else 17 | page.paperSize = { format: system.args[3], orientation: 'portrait', border: '1cm' } 18 | page.open address, (status) -> 19 | if status isnt 'success' 20 | console.log 'Unable to load the address!' 21 | phantom.exit() 22 | else 23 | window.setTimeout (-> page.render output; phantom.exit()), 200 24 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/rasterize.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'), 3 | address, output, size; 4 | 5 | if (system.args.length < 3 || system.args.length > 5) { 6 | console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]'); 7 | console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); 8 | phantom.exit(1); 9 | } else { 10 | address = system.args[1]; 11 | output = system.args[2]; 12 | page.viewportSize = { width: 600, height: 600 }; 13 | if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") { 14 | size = system.args[3].split('*'); 15 | page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } 16 | : { format: system.args[3], orientation: 'portrait', margin: '1cm' }; 17 | } 18 | if (system.args.length > 4) { 19 | page.zoomFactor = system.args[4]; 20 | } 21 | page.open(address, function (status) { 22 | if (status !== 'success') { 23 | console.log('Unable to load the address!'); 24 | phantom.exit(); 25 | } else { 26 | window.setTimeout(function () { 27 | page.render(output); 28 | phantom.exit(); 29 | }, 200); 30 | } 31 | }); 32 | } 33 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/render_multi_url.coffee: -------------------------------------------------------------------------------- 1 | # Render Multiple URLs to file 2 | 3 | system = require("system") 4 | 5 | # Render given urls 6 | # @param array of URLs to render 7 | # @param callbackPerUrl Function called after finishing each URL, including the last URL 8 | # @param callbackFinal Function called after finishing everything 9 | RenderUrlsToFile = (urls, callbackPerUrl, callbackFinal) -> 10 | urlIndex = 0 # only for easy file naming 11 | webpage = require("webpage") 12 | page = null 13 | getFilename = -> 14 | "rendermulti-" + urlIndex + ".png" 15 | 16 | next = (status, url, file) -> 17 | page.close() 18 | callbackPerUrl status, url, file 19 | retrieve() 20 | 21 | retrieve = -> 22 | if urls.length > 0 23 | url = urls.shift() 24 | urlIndex++ 25 | page = webpage.create() 26 | page.viewportSize = 27 | width: 800 28 | height: 600 29 | 30 | page.settings.userAgent = "Phantom.js bot" 31 | page.open "http://" + url, (status) -> 32 | file = getFilename() 33 | if status is "success" 34 | window.setTimeout (-> 35 | page.render file 36 | next status, url, file 37 | ), 200 38 | else 39 | next status, url, file 40 | 41 | else 42 | callbackFinal() 43 | 44 | retrieve() 45 | arrayOfUrls = null 46 | if system.args.length > 1 47 | arrayOfUrls = Array::slice.call(system.args, 1) 48 | else 49 | # Default (no args passed) 50 | console.log "Usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]" 51 | arrayOfUrls = ["www.google.com", "www.bbc.co.uk", "www.phantomjs.org"] 52 | 53 | RenderUrlsToFile arrayOfUrls, ((status, url, file) -> 54 | if status isnt "success" 55 | console.log "Unable to render '" + url + "'" 56 | else 57 | console.log "Rendered '" + url + "' at '" + file + "'" 58 | ), -> 59 | phantom.exit() 60 | 61 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/render_multi_url.js: -------------------------------------------------------------------------------- 1 | // Render Multiple URLs to file 2 | 3 | var RenderUrlsToFile, arrayOfUrls, system; 4 | 5 | system = require("system"); 6 | 7 | /* 8 | Render given urls 9 | @param array of URLs to render 10 | @param callbackPerUrl Function called after finishing each URL, including the last URL 11 | @param callbackFinal Function called after finishing everything 12 | */ 13 | RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) { 14 | var getFilename, next, page, retrieve, urlIndex, webpage; 15 | urlIndex = 0; 16 | webpage = require("webpage"); 17 | page = null; 18 | getFilename = function() { 19 | return "rendermulti-" + urlIndex + ".png"; 20 | }; 21 | next = function(status, url, file) { 22 | page.close(); 23 | callbackPerUrl(status, url, file); 24 | return retrieve(); 25 | }; 26 | retrieve = function() { 27 | var url; 28 | if (urls.length > 0) { 29 | url = urls.shift(); 30 | urlIndex++; 31 | page = webpage.create(); 32 | page.viewportSize = { 33 | width: 800, 34 | height: 600 35 | }; 36 | page.settings.userAgent = "Phantom.js bot"; 37 | return page.open("http://" + url, function(status) { 38 | var file; 39 | file = getFilename(); 40 | if (status === "success") { 41 | return window.setTimeout((function() { 42 | page.render(file); 43 | return next(status, url, file); 44 | }), 200); 45 | } else { 46 | return next(status, url, file); 47 | } 48 | }); 49 | } else { 50 | return callbackFinal(); 51 | } 52 | }; 53 | return retrieve(); 54 | }; 55 | 56 | arrayOfUrls = null; 57 | 58 | if (system.args.length > 1) { 59 | arrayOfUrls = Array.prototype.slice.call(system.args, 1); 60 | } else { 61 | console.log("Usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]"); 62 | arrayOfUrls = ["www.google.com", "www.bbc.co.uk", "www.phantomjs.org"]; 63 | } 64 | 65 | RenderUrlsToFile(arrayOfUrls, (function(status, url, file) { 66 | if (status !== "success") { 67 | return console.log("Unable to render '" + url + "'"); 68 | } else { 69 | return console.log("Rendered '" + url + "' at '" + file + "'"); 70 | } 71 | }), function() { 72 | return phantom.exit(); 73 | }); 74 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/run-jasmine.coffee: -------------------------------------------------------------------------------- 1 | system = require 'system' 2 | 3 | ## 4 | # Wait until the test condition is true or a timeout occurs. Useful for waiting 5 | # on a server response or for a ui change (fadeIn, etc.) to occur. 6 | # 7 | # @param testFx javascript condition that evaluates to a boolean, 8 | # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 9 | # as a callback function. 10 | # @param onReady what to do when testFx condition is fulfilled, 11 | # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 12 | # as a callback function. 13 | # @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 14 | ## 15 | waitFor = (testFx, onReady, timeOutMillis=3000) -> 16 | start = new Date().getTime() 17 | condition = false 18 | f = -> 19 | if (new Date().getTime() - start < timeOutMillis) and not condition 20 | # If not time-out yet and condition not yet fulfilled 21 | condition = (if typeof testFx is 'string' then eval testFx else testFx()) #< defensive code 22 | else 23 | if not condition 24 | # If condition still not fulfilled (timeout but condition is 'false') 25 | console.log "'waitFor()' timeout" 26 | phantom.exit 1 27 | else 28 | # Condition fulfilled (timeout and/or condition is 'true') 29 | console.log "'waitFor()' finished in #{new Date().getTime() - start}ms." 30 | if typeof onReady is 'string' then eval onReady else onReady() #< Do what it's supposed to do once the condition is fulfilled 31 | clearInterval interval #< Stop this interval 32 | interval = setInterval f, 100 #< repeat check every 100ms 33 | 34 | if system.args.length isnt 2 35 | console.log 'Usage: run-jasmine.coffee URL' 36 | phantom.exit 1 37 | 38 | page = require('webpage').create() 39 | 40 | # Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 41 | page.onConsoleMessage = (msg) -> 42 | console.log msg 43 | 44 | page.open system.args[1], (status) -> 45 | if status isnt 'success' 46 | console.log 'Unable to access network' 47 | phantom.exit() 48 | else 49 | waitFor -> 50 | page.evaluate -> 51 | if document.body.querySelector '.finished-at' 52 | return true 53 | return false 54 | , -> 55 | page.evaluate -> 56 | console.log document.body.querySelector('.description').innerText 57 | list = document.body.querySelectorAll('.failed > .description, .failed > .messages > .resultMessage') 58 | for el in list 59 | console.log el.innerText 60 | 61 | phantom.exit() 62 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/run-jasmine.js: -------------------------------------------------------------------------------- 1 | var system = require('system'); 2 | 3 | /** 4 | * Wait until the test condition is true or a timeout occurs. Useful for waiting 5 | * on a server response or for a ui change (fadeIn, etc.) to occur. 6 | * 7 | * @param testFx javascript condition that evaluates to a boolean, 8 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 9 | * as a callback function. 10 | * @param onReady what to do when testFx condition is fulfilled, 11 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 12 | * as a callback function. 13 | * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 14 | */ 15 | function waitFor(testFx, onReady, timeOutMillis) { 16 | var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timeout is 3s 17 | start = new Date().getTime(), 18 | condition = false, 19 | interval = setInterval(function() { 20 | if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { 21 | // If not time-out yet and condition not yet fulfilled 22 | condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code 23 | } else { 24 | if(!condition) { 25 | // If condition still not fulfilled (timeout but condition is 'false') 26 | console.log("'waitFor()' timeout"); 27 | phantom.exit(1); 28 | } else { 29 | // Condition fulfilled (timeout and/or condition is 'true') 30 | console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); 31 | typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled 32 | clearInterval(interval); //< Stop this interval 33 | } 34 | } 35 | }, 100); //< repeat check every 100ms 36 | }; 37 | 38 | 39 | if (system.args.length !== 2) { 40 | console.log('Usage: run-jasmine.js URL'); 41 | phantom.exit(1); 42 | } 43 | 44 | var page = require('webpage').create(); 45 | 46 | // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 47 | page.onConsoleMessage = function(msg) { 48 | console.log(msg); 49 | }; 50 | 51 | page.open(system.args[1], function(status){ 52 | if (status !== "success") { 53 | console.log("Unable to access network"); 54 | phantom.exit(); 55 | } else { 56 | waitFor(function(){ 57 | return page.evaluate(function(){ 58 | return document.body.querySelector('.symbolSummary .pending') === null 59 | }); 60 | }, function(){ 61 | var exitCode = page.evaluate(function(){ 62 | console.log(''); 63 | console.log(document.body.querySelector('.description').innerText); 64 | var list = document.body.querySelectorAll('.results > #details > .specDetail.failed'); 65 | if (list && list.length > 0) { 66 | console.log(''); 67 | console.log(list.length + ' test(s) FAILED:'); 68 | for (i = 0; i < list.length; ++i) { 69 | var el = list[i], 70 | desc = el.querySelector('.description'), 71 | msg = el.querySelector('.resultMessage.fail'); 72 | console.log(''); 73 | console.log(desc.innerText); 74 | console.log(msg.innerText); 75 | console.log(''); 76 | } 77 | return 1; 78 | } else { 79 | console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText); 80 | return 0; 81 | } 82 | }); 83 | phantom.exit(exitCode); 84 | }); 85 | } 86 | }); 87 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/run-qunit.coffee: -------------------------------------------------------------------------------- 1 | system = require 'system' 2 | 3 | ## 4 | # Wait until the test condition is true or a timeout occurs. Useful for waiting 5 | # on a server response or for a ui change (fadeIn, etc.) to occur. 6 | # 7 | # @param testFx javascript condition that evaluates to a boolean, 8 | # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 9 | # as a callback function. 10 | # @param onReady what to do when testFx condition is fulfilled, 11 | # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 12 | # as a callback function. 13 | # @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 14 | ## 15 | waitFor = (testFx, onReady, timeOutMillis=3000) -> 16 | start = new Date().getTime() 17 | condition = false 18 | f = -> 19 | if (new Date().getTime() - start < timeOutMillis) and not condition 20 | # If not time-out yet and condition not yet fulfilled 21 | condition = (if typeof testFx is 'string' then eval testFx else testFx()) #< defensive code 22 | else 23 | if not condition 24 | # If condition still not fulfilled (timeout but condition is 'false') 25 | console.log "'waitFor()' timeout" 26 | phantom.exit 1 27 | else 28 | # Condition fulfilled (timeout and/or condition is 'true') 29 | console.log "'waitFor()' finished in #{new Date().getTime() - start}ms." 30 | if typeof onReady is 'string' then eval onReady else onReady() #< Do what it's supposed to do once the condition is fulfilled 31 | clearInterval interval #< Stop this interval 32 | interval = setInterval f, 100 #< repeat check every 100ms 33 | 34 | if system.args.length isnt 2 35 | console.log 'Usage: run-qunit.coffee URL' 36 | phantom.exit 1 37 | 38 | page = require('webpage').create() 39 | 40 | # Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 41 | page.onConsoleMessage = (msg) -> 42 | console.log msg 43 | 44 | page.open system.args[1], (status) -> 45 | if status isnt 'success' 46 | console.log 'Unable to access network' 47 | phantom.exit 1 48 | else 49 | waitFor -> 50 | page.evaluate -> 51 | el = document.getElementById 'qunit-testresult' 52 | if el and el.innerText.match 'completed' 53 | return true 54 | return false 55 | , -> 56 | failedNum = page.evaluate -> 57 | el = document.getElementById 'qunit-testresult' 58 | console.log el.innerText 59 | try 60 | return el.getElementsByClassName('failed')[0].innerHTML 61 | catch e 62 | return 10000 63 | 64 | phantom.exit if parseInt(failedNum, 10) > 0 then 1 else 0 65 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/run-qunit.js: -------------------------------------------------------------------------------- 1 | var system = require('system'); 2 | 3 | /** 4 | * Wait until the test condition is true or a timeout occurs. Useful for waiting 5 | * on a server response or for a ui change (fadeIn, etc.) to occur. 6 | * 7 | * @param testFx javascript condition that evaluates to a boolean, 8 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 9 | * as a callback function. 10 | * @param onReady what to do when testFx condition is fulfilled, 11 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 12 | * as a callback function. 13 | * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 14 | */ 15 | function waitFor(testFx, onReady, timeOutMillis) { 16 | var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3001, //< Default Max Timout is 3s 17 | start = new Date().getTime(), 18 | condition = false, 19 | interval = setInterval(function() { 20 | if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { 21 | // If not time-out yet and condition not yet fulfilled 22 | condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code 23 | } else { 24 | if(!condition) { 25 | // If condition still not fulfilled (timeout but condition is 'false') 26 | console.log("'waitFor()' timeout"); 27 | phantom.exit(1); 28 | } else { 29 | // Condition fulfilled (timeout and/or condition is 'true') 30 | console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); 31 | typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled 32 | clearInterval(interval); //< Stop this interval 33 | } 34 | } 35 | }, 100); //< repeat check every 250ms 36 | }; 37 | 38 | 39 | if (system.args.length !== 2) { 40 | console.log('Usage: run-qunit.js URL'); 41 | phantom.exit(1); 42 | } 43 | 44 | var page = require('webpage').create(); 45 | 46 | // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 47 | page.onConsoleMessage = function(msg) { 48 | console.log(msg); 49 | }; 50 | 51 | page.open(system.args[1], function(status){ 52 | if (status !== "success") { 53 | console.log("Unable to access network"); 54 | phantom.exit(1); 55 | } else { 56 | waitFor(function(){ 57 | return page.evaluate(function(){ 58 | var el = document.getElementById('qunit-testresult'); 59 | if (el && el.innerText.match('completed')) { 60 | return true; 61 | } 62 | return false; 63 | }); 64 | }, function(){ 65 | var failedNum = page.evaluate(function(){ 66 | var el = document.getElementById('qunit-testresult'); 67 | console.log(el.innerText); 68 | try { 69 | return el.getElementsByClassName('failed')[0].innerHTML; 70 | } catch (e) { } 71 | return 10000; 72 | }); 73 | phantom.exit((parseInt(failedNum, 10) > 0) ? 1 : 0); 74 | }); 75 | } 76 | }); 77 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/scandir.coffee: -------------------------------------------------------------------------------- 1 | # List all the files in a Tree of Directories 2 | system = require 'system' 3 | 4 | if system.args.length != 2 5 | console.log "Usage: phantomjs scandir.coffee DIRECTORY_TO_SCAN" 6 | phantom.exit 1 7 | scanDirectory = (path) -> 8 | fs = require 'fs' 9 | if fs.exists(path) and fs.isFile(path) 10 | console.log path 11 | else if fs.isDirectory(path) 12 | fs.list(path).forEach (e) -> 13 | scanDirectory path + "/" + e if e != "." and e != ".." 14 | 15 | scanDirectory system.args[1] 16 | phantom.exit() 17 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/scandir.js: -------------------------------------------------------------------------------- 1 | // List all the files in a Tree of Directories 2 | var system = require('system'); 3 | 4 | if (system.args.length !== 2) { 5 | console.log("Usage: phantomjs scandir.js DIRECTORY_TO_SCAN"); 6 | phantom.exit(1); 7 | } 8 | 9 | var scanDirectory = function (path) { 10 | var fs = require('fs'); 11 | if (fs.exists(path) && fs.isFile(path)) { 12 | console.log(path); 13 | } else if (fs.isDirectory(path)) { 14 | fs.list(path).forEach(function (e) { 15 | if ( e !== "." && e !== ".." ) { //< Avoid loops 16 | scanDirectory(path + '/' + e); 17 | } 18 | }); 19 | } 20 | }; 21 | scanDirectory(system.args[1]); 22 | phantom.exit(); 23 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/seasonfood.coffee: -------------------------------------------------------------------------------- 1 | # Show BBC seasonal food list. 2 | 3 | window.cbfunc = (data) -> 4 | list = data.query.results.results.result 5 | names = ['January', 'February', 'March', 6 | 'April', 'May', 'June', 7 | 'July', 'August', 'September', 8 | 'October', 'November', 'December'] 9 | for item in list 10 | console.log [item.name.replace(/\s/ig, ' '), ':', 11 | names[item.atItsBestUntil], 'to', 12 | names[item.atItsBestFrom]].join(' ') 13 | phantom.exit() 14 | 15 | el = document.createElement 'script' 16 | el.src = 'http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20bbc.goodfood.seasonal%3B&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=window.cbfunc' 17 | document.body.appendChild el 18 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/seasonfood.js: -------------------------------------------------------------------------------- 1 | // Show BBC seasonal food list. 2 | 3 | var cbfunc = function (data) { 4 | var list = data.query.results.results.result, 5 | names = ['January', 'February', 'March', 6 | 'April', 'May', 'June', 7 | 'July', 'August', 'September', 8 | 'October', 'November', 'December']; 9 | list.forEach(function (item) { 10 | console.log([item.name.replace(/\s/ig, ' '), ':', 11 | names[item.atItsBestUntil], 'to', 12 | names[item.atItsBestFrom]].join(' ')); 13 | }); 14 | phantom.exit(); 15 | }; 16 | 17 | var el = document.createElement('script'); 18 | el.src = 'http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20bbc.goodfood.seasonal%3B&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc'; 19 | document.body.appendChild(el); 20 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/server.coffee: -------------------------------------------------------------------------------- 1 | page = require("webpage").create() 2 | server = require("webserver").create() 3 | system = require("system") 4 | host = undefined 5 | port = undefined 6 | if system.args.length isnt 2 7 | console.log "Usage: server.js " 8 | phantom.exit 1 9 | else 10 | port = system.args[1] 11 | listening = server.listen(port, (request, response) -> 12 | console.log "GOT HTTP REQUEST" 13 | console.log JSON.stringify(request, null, 4) 14 | 15 | # we set the headers here 16 | response.statusCode = 200 17 | response.headers = 18 | Cache: "no-cache" 19 | "Content-Type": "text/html" 20 | 21 | 22 | # this is also possible: 23 | response.setHeader "foo", "bar" 24 | 25 | # now we write the body 26 | # note: the headers above will now be sent implictly 27 | response.write "YES!" 28 | 29 | # note: writeBody can be called multiple times 30 | response.write "

pretty cool :)" 31 | response.close() 32 | ) 33 | unless listening 34 | console.log "could not create web server listening on port " + port 35 | phantom.exit() 36 | url = "http://localhost:" + port + "/foo/bar.php?asdf=true" 37 | console.log "SENDING REQUEST TO:" 38 | console.log url 39 | page.open url, (status) -> 40 | if status isnt "success" 41 | console.log "FAIL to load the address" 42 | else 43 | console.log "GOT REPLY FROM SERVER:" 44 | console.log page.content 45 | phantom.exit() 46 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/server.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(); 2 | var server = require('webserver').create(); 3 | var system = require('system'); 4 | var host, port; 5 | 6 | if (system.args.length !== 2) { 7 | console.log('Usage: server.js '); 8 | phantom.exit(1); 9 | } else { 10 | port = system.args[1]; 11 | var listening = server.listen(port, function (request, response) { 12 | console.log("GOT HTTP REQUEST"); 13 | console.log(JSON.stringify(request, null, 4)); 14 | 15 | // we set the headers here 16 | response.statusCode = 200; 17 | response.headers = {"Cache": "no-cache", "Content-Type": "text/html"}; 18 | // this is also possible: 19 | response.setHeader("foo", "bar"); 20 | // now we write the body 21 | // note: the headers above will now be sent implictly 22 | response.write("YES!"); 23 | // note: writeBody can be called multiple times 24 | response.write("

pretty cool :)"); 25 | response.close(); 26 | }); 27 | if (!listening) { 28 | console.log("could not create web server listening on port " + port); 29 | phantom.exit(); 30 | } 31 | var url = "http://localhost:" + port + "/foo/bar.php?asdf=true"; 32 | console.log("SENDING REQUEST TO:"); 33 | console.log(url); 34 | page.open(url, function (status) { 35 | if (status !== 'success') { 36 | console.log('FAIL to load the address'); 37 | } else { 38 | console.log("GOT REPLY FROM SERVER:"); 39 | console.log(page.content); 40 | } 41 | phantom.exit(); 42 | }); 43 | } 44 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/serverkeepalive.coffee: -------------------------------------------------------------------------------- 1 | port = undefined 2 | server = undefined 3 | service = undefined 4 | system = require("system") 5 | if system.args.length isnt 2 6 | console.log "Usage: serverkeepalive.js " 7 | phantom.exit 1 8 | else 9 | port = system.args[1] 10 | server = require("webserver").create() 11 | service = server.listen(port, 12 | keepAlive: true 13 | , (request, response) -> 14 | console.log "Request at " + new Date() 15 | console.log JSON.stringify(request, null, 4) 16 | body = JSON.stringify(request, null, 4) 17 | response.statusCode = 200 18 | response.headers = 19 | Cache: "no-cache" 20 | "Content-Type": "text/plain" 21 | Connection: "Keep-Alive" 22 | "Keep-Alive": "timeout=5, max=100" 23 | "Content-Length": body.length 24 | 25 | response.write body 26 | response.close() 27 | ) 28 | if service 29 | console.log "Web server running on port " + port 30 | else 31 | console.log "Error: Could not create web server listening on port " + port 32 | phantom.exit() -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/serverkeepalive.js: -------------------------------------------------------------------------------- 1 | var port, server, service, 2 | system = require('system'); 3 | 4 | if (system.args.length !== 2) { 5 | console.log('Usage: serverkeepalive.js '); 6 | phantom.exit(1); 7 | } else { 8 | port = system.args[1]; 9 | server = require('webserver').create(); 10 | 11 | service = server.listen(port, { keepAlive: true }, function (request, response) { 12 | console.log('Request at ' + new Date()); 13 | console.log(JSON.stringify(request, null, 4)); 14 | 15 | var body = JSON.stringify(request, null, 4); 16 | response.statusCode = 200; 17 | response.headers = { 18 | 'Cache': 'no-cache', 19 | 'Content-Type': 'text/plain', 20 | 'Connection': 'Keep-Alive', 21 | 'Keep-Alive': 'timeout=5, max=100', 22 | 'Content-Length': body.length 23 | }; 24 | response.write(body); 25 | response.close(); 26 | }); 27 | 28 | if (service) { 29 | console.log('Web server running on port ' + port); 30 | } else { 31 | console.log('Error: Could not create web server listening on port ' + port); 32 | phantom.exit(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/simpleserver.coffee: -------------------------------------------------------------------------------- 1 | system = require 'system' 2 | 3 | if system.args.length is 1 4 | console.log "Usage: simpleserver.coffee " 5 | phantom.exit 1 6 | else 7 | port = system.args[1] 8 | server = require("webserver").create() 9 | 10 | service = server.listen(port, (request, response) -> 11 | 12 | console.log "Request at " + new Date() 13 | console.log JSON.stringify(request, null, 4) 14 | 15 | response.statusCode = 200 16 | response.headers = 17 | Cache: "no-cache" 18 | "Content-Type": "text/html" 19 | 20 | response.write "" 21 | response.write "" 22 | response.write "Hello, world!" 23 | response.write "" 24 | response.write "" 25 | response.write "

This is from PhantomJS web server.

" 26 | response.write "

Request data:

" 27 | response.write "
"
28 |     response.write JSON.stringify(request, null, 4)
29 |     response.write "
" 30 | response.write "" 31 | response.write "" 32 | response.close() 33 | ) 34 | if service 35 | console.log "Web server running on port " + port 36 | else 37 | console.log "Error: Could not create web server listening on port " + port 38 | phantom.exit() 39 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/simpleserver.js: -------------------------------------------------------------------------------- 1 | var port, server, service, 2 | system = require('system'); 3 | 4 | if (system.args.length !== 2) { 5 | console.log('Usage: simpleserver.js '); 6 | phantom.exit(1); 7 | } else { 8 | port = system.args[1]; 9 | server = require('webserver').create(); 10 | 11 | service = server.listen(port, function (request, response) { 12 | 13 | console.log('Request at ' + new Date()); 14 | console.log(JSON.stringify(request, null, 4)); 15 | 16 | response.statusCode = 200; 17 | response.headers = { 18 | 'Cache': 'no-cache', 19 | 'Content-Type': 'text/html' 20 | }; 21 | response.write(''); 22 | response.write(''); 23 | response.write('Hello, world!'); 24 | response.write(''); 25 | response.write(''); 26 | response.write('

This is from PhantomJS web server.

'); 27 | response.write('

Request data:

'); 28 | response.write('
');
29 |         response.write(JSON.stringify(request, null, 4));
30 |         response.write('
'); 31 | response.write(''); 32 | response.write(''); 33 | response.close(); 34 | }); 35 | 36 | if (service) { 37 | console.log('Web server running on port ' + port); 38 | } else { 39 | console.log('Error: Could not create web server listening on port ' + port); 40 | phantom.exit(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/sleepsort.coffee: -------------------------------------------------------------------------------- 1 | ### 2 | Sort integers from the command line in a very ridiculous way: leveraging timeouts :P 3 | ### 4 | 5 | system = require 'system' 6 | 7 | if system.args.length < 2 8 | console.log "Usage: phantomjs sleepsort.coffee PUT YOUR INTEGERS HERE SEPARATED BY SPACES" 9 | phantom.exit 1 10 | else 11 | sortedCount = 0 12 | args = Array.prototype.slice.call(system.args, 1) 13 | for int in args 14 | setTimeout (do (int) -> 15 | -> 16 | console.log int 17 | ++sortedCount 18 | phantom.exit() if sortedCount is args.length), 19 | int 20 | 21 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/sleepsort.js: -------------------------------------------------------------------------------- 1 | // sleepsort.js - Sort integers from the commandline in a very ridiculous way: leveraging timeouts :P 2 | var system = require('system'); 3 | 4 | function sleepSort(array, callback) { 5 | var sortedCount = 0, 6 | i, len; 7 | for ( i = 0, len = array.length; i < len; ++i ) { 8 | setTimeout((function(j){ 9 | return function() { 10 | console.log(array[j]); 11 | ++sortedCount; 12 | (len === sortedCount) && callback(); 13 | }; 14 | }(i)), array[i]); 15 | } 16 | } 17 | 18 | if ( system.args < 2 ) { 19 | console.log("Usage: phantomjs sleepsort.js PUT YOUR INTEGERS HERE SEPARATED BY SPACES"); 20 | phantom.exit(1); 21 | } else { 22 | sleepSort(Array.prototype.slice.call(system.args, 1), function() { 23 | phantom.exit(); 24 | }); 25 | } -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/stdin-stdout-stderr.coffee: -------------------------------------------------------------------------------- 1 | system = require 'system' 2 | 3 | system.stdout.write 'Hello, system.stdout.write!' 4 | system.stdout.writeLine '\nHello, system.stdout.writeLine!' 5 | 6 | system.stderr.write 'Hello, system.stderr.write!' 7 | system.stderr.writeLine '\nHello, system.stderr.writeLine!' 8 | 9 | system.stdout.writeLine 'system.stdin.readLine(): ' 10 | line = system.stdin.readLine() 11 | system.stdout.writeLine JSON.stringify line 12 | 13 | # This is essentially a `readAll` 14 | system.stdout.writeLine 'system.stdin.read(5): (ctrl+D to end)' 15 | input = system.stdin.read 5 16 | system.stdout.writeLine JSON.stringify input 17 | 18 | phantom.exit 0 19 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/stdin-stdout-stderr.js: -------------------------------------------------------------------------------- 1 | var system = require('system'); 2 | 3 | system.stdout.write('Hello, system.stdout.write!'); 4 | system.stdout.writeLine('\nHello, system.stdout.writeLine!'); 5 | 6 | system.stderr.write('Hello, system.stderr.write!'); 7 | system.stderr.writeLine('\nHello, system.stderr.writeLine!'); 8 | 9 | system.stdout.writeLine('system.stdin.readLine(): '); 10 | var line = system.stdin.readLine(); 11 | system.stdout.writeLine(JSON.stringify(line)); 12 | 13 | // This is essentially a `readAll` 14 | system.stdout.writeLine('system.stdin.read(5): (ctrl+D to end)'); 15 | var input = system.stdin.read(5); 16 | system.stdout.writeLine(JSON.stringify(input)); 17 | 18 | phantom.exit(0); 19 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/technews.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | 3 | page.viewportSize = { width: 320, height: 480 } 4 | 5 | page.open 'http://news.google.com/news/i/section?&topic=t', 6 | (status) -> 7 | if status isnt 'success' 8 | console.log 'Unable to access the network!' 9 | else 10 | page.evaluate -> 11 | body = document.body 12 | body.style.backgroundColor = '#fff' 13 | body.querySelector('div#title-block').style.display = 'none' 14 | body.querySelector('form#edition-picker-form') 15 | .parentElement.parentElement.style.display = 'none' 16 | page.render 'technews.png' 17 | phantom.exit() 18 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/technews.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(); 2 | page.viewportSize = { width: 320, height: 480 }; 3 | page.open('http://news.google.com/news/i/section?&topic=t', function (status) { 4 | if (status !== 'success') { 5 | console.log('Unable to access the network!'); 6 | } else { 7 | page.evaluate(function () { 8 | var body = document.body; 9 | body.style.backgroundColor = '#fff'; 10 | body.querySelector('div#title-block').style.display = 'none'; 11 | body.querySelector('form#edition-picker-form').parentElement.parentElement.style.display = 'none'; 12 | }); 13 | page.render('technews.png'); 14 | } 15 | phantom.exit(); 16 | }); 17 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/tweets.coffee: -------------------------------------------------------------------------------- 1 | # Get twitter status for given account (or for the default one, "PhantomJS") 2 | 3 | page = require('webpage').create() 4 | system = require 'system' 5 | twitterId = 'PhantomJS' #< default value 6 | 7 | # Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 8 | page.onConsoleMessage = (msg) -> 9 | console.log msg 10 | 11 | # Print usage message, if no twitter ID is passed 12 | if system.args.length < 2 13 | console.log 'Usage: tweets.coffee [twitter ID]' 14 | else 15 | twitterId = system.args[1] 16 | 17 | # Heading 18 | console.log "*** Latest tweets from @#{twitterId} ***\n" 19 | 20 | # Open Twitter Mobile and, onPageLoad, do... 21 | page.open encodeURI("http://mobile.twitter.com/#{twitterId}"), (status) -> 22 | # Check for page load success 23 | if status isnt 'success' 24 | console.log 'Unable to access network' 25 | else 26 | # Execute some DOM inspection within the page context 27 | page.evaluate -> 28 | list = document.querySelectorAll 'div.tweet-text' 29 | for i, j in list 30 | console.log "#{j + 1}: #{i.innerText}" 31 | phantom.exit() 32 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/tweets.js: -------------------------------------------------------------------------------- 1 | // Get twitter status for given account (or for the default one, "PhantomJS") 2 | 3 | var page = require('webpage').create(), 4 | system = require('system'), 5 | twitterId = "PhantomJS"; //< default value 6 | 7 | // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this") 8 | page.onConsoleMessage = function(msg) { 9 | console.log(msg); 10 | }; 11 | 12 | // Print usage message, if no twitter ID is passed 13 | if (system.args.length < 2) { 14 | console.log("Usage: tweets.js [twitter ID]"); 15 | } else { 16 | twitterId = system.args[1]; 17 | } 18 | 19 | // Heading 20 | console.log("*** Latest tweets from @" + twitterId + " ***\n"); 21 | 22 | // Open Twitter Mobile and, onPageLoad, do... 23 | page.open(encodeURI("http://mobile.twitter.com/" + twitterId), function (status) { 24 | // Check for page load success 25 | if (status !== "success") { 26 | console.log("Unable to access network"); 27 | } else { 28 | // Execute some DOM inspection within the page context 29 | page.evaluate(function() { 30 | var list = document.querySelectorAll('div.tweet-text'); 31 | for (var i = 0; i < list.length; ++i) { 32 | console.log((i + 1) + ": " + list[i].innerText); 33 | } 34 | }); 35 | } 36 | phantom.exit(); 37 | }); 38 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/universe.js: -------------------------------------------------------------------------------- 1 | // This is to be used by "module.js" (and "module.coffee") example(s). 2 | // There should NOT be a "universe.coffee" as only 1 of the 2 would 3 | // ever be loaded unless the file extension was specified. 4 | 5 | exports.answer = 42; 6 | 7 | exports.start = function () { 8 | console.log('Starting the universe....'); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/unrandomize.coffee: -------------------------------------------------------------------------------- 1 | # Modify global object at the page initialization. 2 | # In this example, effectively Math.random() always returns 0.42. 3 | 4 | page = require('webpage').create() 5 | page.onInitialized = -> 6 | page.evaluate -> 7 | Math.random = -> 8 | 42 / 100 9 | 10 | page.open "http://ariya.github.com/js/random/", (status) -> 11 | if status != "success" 12 | console.log "Network error." 13 | else 14 | console.log page.evaluate(-> 15 | document.getElementById("numbers").textContent 16 | ) 17 | phantom.exit() 18 | 19 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/unrandomize.js: -------------------------------------------------------------------------------- 1 | // Modify global object at the page initialization. 2 | // In this example, effectively Math.random() always returns 0.42. 3 | 4 | var page = require('webpage').create(); 5 | 6 | page.onInitialized = function () { 7 | page.evaluate(function () { 8 | Math.random = function() { 9 | return 42 / 100; 10 | }; 11 | }); 12 | }; 13 | 14 | page.open('http://ariya.github.com/js/random/', function (status) { 15 | var result; 16 | if (status !== 'success') { 17 | console.log('Network error.'); 18 | } else { 19 | console.log(page.evaluate(function () { 20 | return document.getElementById('numbers').textContent; 21 | })); 22 | } 23 | phantom.exit(); 24 | }); 25 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/useragent.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | 3 | console.log 'The default user agent is ' + page.settings.userAgent 4 | 5 | page.settings.userAgent = 'SpecialAgent' 6 | page.open 'http://www.httpuseragent.org', (status) -> 7 | if status isnt 'success' 8 | console.log 'Unable to access network' 9 | else 10 | console.log page.evaluate -> document.getElementById('myagent').innerText 11 | phantom.exit() 12 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/useragent.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(); 2 | console.log('The default user agent is ' + page.settings.userAgent); 3 | page.settings.userAgent = 'SpecialAgent'; 4 | page.open('http://www.httpuseragent.org', function (status) { 5 | if (status !== 'success') { 6 | console.log('Unable to access network'); 7 | } else { 8 | var ua = page.evaluate(function () { 9 | return document.getElementById('myagent').innerText; 10 | }); 11 | console.log(ua); 12 | } 13 | phantom.exit(); 14 | }); 15 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/version.coffee: -------------------------------------------------------------------------------- 1 | console.log 'using PhantomJS version ' + 2 | phantom.version.major + '.' + 3 | phantom.version.minor + '.' + 4 | phantom.version.patch 5 | phantom.exit() 6 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/version.js: -------------------------------------------------------------------------------- 1 | console.log('using PhantomJS version ' + 2 | phantom.version.major + '.' + 3 | phantom.version.minor + '.' + 4 | phantom.version.patch); 5 | phantom.exit(); 6 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/waitfor.coffee: -------------------------------------------------------------------------------- 1 | ## 2 | # Wait until the test condition is true or a timeout occurs. Useful for waiting 3 | # on a server response or for a ui change (fadeIn, etc.) to occur. 4 | # 5 | # @param testFx javascript condition that evaluates to a boolean, 6 | # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 7 | # as a callback function. 8 | # @param onReady what to do when testFx condition is fulfilled, 9 | # it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 10 | # as a callback function. 11 | # @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 12 | ## 13 | waitFor = (testFx, onReady, timeOutMillis=3000) -> 14 | start = new Date().getTime() 15 | condition = false 16 | f = -> 17 | if (new Date().getTime() - start < timeOutMillis) and not condition 18 | # If not time-out yet and condition not yet fulfilled 19 | condition = (if typeof testFx is 'string' then eval testFx else testFx()) #< defensive code 20 | else 21 | if not condition 22 | # If condition still not fulfilled (timeout but condition is 'false') 23 | console.log "'waitFor()' timeout" 24 | phantom.exit 1 25 | else 26 | # Condition fulfilled (timeout and/or condition is 'true') 27 | console.log "'waitFor()' finished in #{new Date().getTime() - start}ms." 28 | if typeof onReady is 'string' then eval onReady else onReady() #< Do what it's supposed to do once the condition is fulfilled 29 | clearInterval interval #< Stop this interval 30 | interval = setInterval f, 250 #< repeat check every 250ms 31 | 32 | 33 | page = require('webpage').create() 34 | 35 | # Open Twitter on 'sencha' profile and, onPageLoad, do... 36 | page.open 'http://twitter.com/#!/sencha', (status) -> 37 | # Check for page load success 38 | if status isnt 'success' 39 | console.log 'Unable to access network' 40 | else 41 | # Wait for 'signin-dropdown' to be visible 42 | waitFor -> 43 | # Check in the page if a specific element is now visible 44 | page.evaluate -> 45 | $('#signin-dropdown').is ':visible' 46 | , -> 47 | console.log 'The sign-in dialog should be visible now.' 48 | phantom.exit() 49 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/waitfor.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Wait until the test condition is true or a timeout occurs. Useful for waiting 3 | * on a server response or for a ui change (fadeIn, etc.) to occur. 4 | * 5 | * @param testFx javascript condition that evaluates to a boolean, 6 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 7 | * as a callback function. 8 | * @param onReady what to do when testFx condition is fulfilled, 9 | * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or 10 | * as a callback function. 11 | * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. 12 | */ 13 | function waitFor(testFx, onReady, timeOutMillis) { 14 | var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s 15 | start = new Date().getTime(), 16 | condition = false, 17 | interval = setInterval(function() { 18 | if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { 19 | // If not time-out yet and condition not yet fulfilled 20 | condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code 21 | } else { 22 | if(!condition) { 23 | // If condition still not fulfilled (timeout but condition is 'false') 24 | console.log("'waitFor()' timeout"); 25 | phantom.exit(1); 26 | } else { 27 | // Condition fulfilled (timeout and/or condition is 'true') 28 | console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); 29 | typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled 30 | clearInterval(interval); //< Stop this interval 31 | } 32 | } 33 | }, 250); //< repeat check every 250ms 34 | }; 35 | 36 | 37 | var page = require('webpage').create(); 38 | 39 | // Open Twitter on 'sencha' profile and, onPageLoad, do... 40 | page.open("http://twitter.com/#!/sencha", function (status) { 41 | // Check for page load success 42 | if (status !== "success") { 43 | console.log("Unable to access network"); 44 | } else { 45 | // Wait for 'signin-dropdown' to be visible 46 | waitFor(function() { 47 | // Check in the page if a specific element is now visible 48 | return page.evaluate(function() { 49 | return $("#signin-dropdown").is(":visible"); 50 | }); 51 | }, function() { 52 | console.log("The sign-in dialog should be visible now."); 53 | phantom.exit(); 54 | }); 55 | } 56 | }); 57 | 58 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/walk_through_frames.coffee: -------------------------------------------------------------------------------- 1 | pageTitle = (page) -> 2 | page.evaluate -> 3 | window.document.title 4 | setPageTitle = (page, newTitle) -> 5 | page.evaluate ((newTitle) -> 6 | window.document.title = newTitle 7 | ), newTitle 8 | p = require("webpage").create() 9 | p.open "../test/webpage-spec-frames/index.html", (status) -> 10 | console.log "pageTitle(): " + pageTitle(p) 11 | console.log "currentFrameName(): " + p.currentFrameName() 12 | console.log "childFramesCount(): " + p.childFramesCount() 13 | console.log "childFramesName(): " + p.childFramesName() 14 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 15 | setPageTitle p, pageTitle(p) + "-visited" 16 | console.log "" 17 | console.log "p.switchToChildFrame(\"frame1\"): " + p.switchToChildFrame("frame1") 18 | console.log "pageTitle(): " + pageTitle(p) 19 | console.log "currentFrameName(): " + p.currentFrameName() 20 | console.log "childFramesCount(): " + p.childFramesCount() 21 | console.log "childFramesName(): " + p.childFramesName() 22 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 23 | setPageTitle p, pageTitle(p) + "-visited" 24 | console.log "" 25 | console.log "p.switchToChildFrame(\"frame1-2\"): " + p.switchToChildFrame("frame1-2") 26 | console.log "pageTitle(): " + pageTitle(p) 27 | console.log "currentFrameName(): " + p.currentFrameName() 28 | console.log "childFramesCount(): " + p.childFramesCount() 29 | console.log "childFramesName(): " + p.childFramesName() 30 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 31 | setPageTitle p, pageTitle(p) + "-visited" 32 | console.log "" 33 | console.log "p.switchToParentFrame(): " + p.switchToParentFrame() 34 | console.log "pageTitle(): " + pageTitle(p) 35 | console.log "currentFrameName(): " + p.currentFrameName() 36 | console.log "childFramesCount(): " + p.childFramesCount() 37 | console.log "childFramesName(): " + p.childFramesName() 38 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 39 | setPageTitle p, pageTitle(p) + "-visited" 40 | console.log "" 41 | console.log "p.switchToChildFrame(0): " + p.switchToChildFrame(0) 42 | console.log "pageTitle(): " + pageTitle(p) 43 | console.log "currentFrameName(): " + p.currentFrameName() 44 | console.log "childFramesCount(): " + p.childFramesCount() 45 | console.log "childFramesName(): " + p.childFramesName() 46 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 47 | setPageTitle p, pageTitle(p) + "-visited" 48 | console.log "" 49 | console.log "p.switchToMainFrame()" 50 | p.switchToMainFrame() 51 | console.log "pageTitle(): " + pageTitle(p) 52 | console.log "currentFrameName(): " + p.currentFrameName() 53 | console.log "childFramesCount(): " + p.childFramesCount() 54 | console.log "childFramesName(): " + p.childFramesName() 55 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 56 | setPageTitle p, pageTitle(p) + "-visited" 57 | console.log "" 58 | console.log "p.switchToChildFrame(\"frame2\"): " + p.switchToChildFrame("frame2") 59 | console.log "pageTitle(): " + pageTitle(p) 60 | console.log "currentFrameName(): " + p.currentFrameName() 61 | console.log "childFramesCount(): " + p.childFramesCount() 62 | console.log "childFramesName(): " + p.childFramesName() 63 | console.log "setPageTitle(CURRENT TITLE+'-visited')" 64 | setPageTitle p, pageTitle(p) + "-visited" 65 | console.log "" 66 | phantom.exit() 67 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/walk_through_frames.js: -------------------------------------------------------------------------------- 1 | var p = require("webpage").create(); 2 | 3 | function pageTitle(page) { 4 | return page.evaluate(function(){ 5 | return window.document.title; 6 | }); 7 | } 8 | 9 | function setPageTitle(page, newTitle) { 10 | page.evaluate(function(newTitle){ 11 | window.document.title = newTitle; 12 | }, newTitle); 13 | } 14 | 15 | p.open("../test/webpage-spec-frames/index.html", function(status) { 16 | console.log("pageTitle(): " + pageTitle(p)); 17 | console.log("currentFrameName(): "+p.currentFrameName()); 18 | console.log("childFramesCount(): "+p.childFramesCount()); 19 | console.log("childFramesName(): "+p.childFramesName()); 20 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 21 | console.log(""); 22 | 23 | console.log("p.switchToChildFrame(\"frame1\"): "+p.switchToChildFrame("frame1")); 24 | console.log("pageTitle(): " + pageTitle(p)); 25 | console.log("currentFrameName(): "+p.currentFrameName()); 26 | console.log("childFramesCount(): "+p.childFramesCount()); 27 | console.log("childFramesName(): "+p.childFramesName()); 28 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 29 | console.log(""); 30 | 31 | console.log("p.switchToChildFrame(\"frame1-2\"): "+p.switchToChildFrame("frame1-2")); 32 | console.log("pageTitle(): " + pageTitle(p)); 33 | console.log("currentFrameName(): "+p.currentFrameName()); 34 | console.log("childFramesCount(): "+p.childFramesCount()); 35 | console.log("childFramesName(): "+p.childFramesName()); 36 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 37 | console.log(""); 38 | 39 | console.log("p.switchToParentFrame(): "+p.switchToParentFrame()); 40 | console.log("pageTitle(): " + pageTitle(p)); 41 | console.log("currentFrameName(): "+p.currentFrameName()); 42 | console.log("childFramesCount(): "+p.childFramesCount()); 43 | console.log("childFramesName(): "+p.childFramesName()); 44 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 45 | console.log(""); 46 | 47 | console.log("p.switchToChildFrame(0): "+p.switchToChildFrame(0)); 48 | console.log("pageTitle(): " + pageTitle(p)); 49 | console.log("currentFrameName(): "+p.currentFrameName()); 50 | console.log("childFramesCount(): "+p.childFramesCount()); 51 | console.log("childFramesName(): "+p.childFramesName()); 52 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 53 | console.log(""); 54 | 55 | console.log("p.switchToMainFrame()"); p.switchToMainFrame(); 56 | console.log("pageTitle(): " + pageTitle(p)); 57 | console.log("currentFrameName(): "+p.currentFrameName()); 58 | console.log("childFramesCount(): "+p.childFramesCount()); 59 | console.log("childFramesName(): "+p.childFramesName()); 60 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 61 | console.log(""); 62 | 63 | console.log("p.switchToChildFrame(\"frame2\"): "+p.switchToChildFrame("frame2")); 64 | console.log("pageTitle(): " + pageTitle(p)); 65 | console.log("currentFrameName(): "+p.currentFrameName()); 66 | console.log("childFramesCount(): "+p.childFramesCount()); 67 | console.log("childFramesName(): "+p.childFramesName()); 68 | console.log("setPageTitle(CURRENT TITLE+'-visited')"); setPageTitle(p, pageTitle(p) + "-visited"); 69 | console.log(""); 70 | 71 | phantom.exit(); 72 | }); 73 | 74 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/weather.coffee: -------------------------------------------------------------------------------- 1 | page = require('webpage').create() 2 | system = require 'system' 3 | 4 | city = 'Mountain View, California'; # default 5 | if system.args.length > 1 6 | city = Array.prototype.slice.call(system.args, 1).join(' ') 7 | url = encodeURI 'http://api.openweathermap.org/data/2.1/find/name?q=' + city 8 | 9 | console.log 'Checking weather condition for', city, '...' 10 | 11 | page.open url, (status) -> 12 | if status isnt 'success' 13 | console.log 'Error: Unable to access network!' 14 | else 15 | result = page.evaluate -> 16 | return document.body.innerText 17 | try 18 | data = JSON.parse result 19 | data = data.list[0] 20 | console.log '' 21 | console.log 'City:', data.name 22 | console.log 'Condition:', data.weather.map (entry) -> 23 | return entry.main 24 | console.log 'Temperature:', Math.round(data.main.temp - 273.15), 'C' 25 | console.log 'Humidity:', Math.round(data.main.humidity), '%' 26 | catch e 27 | console.log 'Error:', e.toString() 28 | 29 | phantom.exit() 30 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/examples/weather.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(), 2 | system = require('system'), 3 | city, 4 | url; 5 | 6 | city = 'Mountain View, California'; // default 7 | if (system.args.length > 1) { 8 | city = Array.prototype.slice.call(system.args, 1).join(' '); 9 | } 10 | url = encodeURI('http://api.openweathermap.org/data/2.1/find/name?q=' + city); 11 | 12 | console.log('Checking weather condition for', city, '...'); 13 | 14 | page.open(url, function(status) { 15 | var result, data; 16 | if (status !== 'success') { 17 | console.log('Error: Unable to access network!'); 18 | } else { 19 | result = page.evaluate(function () { 20 | return document.body.innerText; 21 | }); 22 | try { 23 | data = JSON.parse(result); 24 | data = data.list[0]; 25 | console.log(''); 26 | console.log('City:', data.name); 27 | console.log('Condition:', data.weather.map(function(entry) { 28 | return entry.main; 29 | }).join(', ')); 30 | console.log('Temperature:', Math.round(data.main.temp - 273.15), 'C'); 31 | console.log('Humidity:', Math.round(data.main.humidity), '%'); 32 | } catch (e) { 33 | console.log('Error:', e.toString()); 34 | } 35 | } 36 | phantom.exit(); 37 | }); 38 | -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/phantomjs.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liangyangtao/ubk_weixinbysogou/93fff8b83d702f222a7a93a0e1fba24cb88174b8/phantomjs-1.9.7-windows/phantomjs.exe -------------------------------------------------------------------------------- /phantomjs-1.9.7-windows/third-party.txt: -------------------------------------------------------------------------------- 1 | This document contains the list of Third Party Software included with 2 | PhantomJS, along with the license information. 3 | 4 | Third Party Software may impose additional restrictions and it is the 5 | user's responsibility to ensure that they have met the licensing 6 | requirements of PhantomJS and the relevant license of the Third Party 7 | Software they are using. 8 | 9 | Qt - http://qt-project.org/ 10 | License: GNU Lesser General Public License (LGPL) version 2.1. 11 | Reference: http://qt-project.org/doc/qt-4.8/lgpl.html. 12 | 13 | WebKit - http://www.webkit.org/ 14 | License: GNU Lesser General Public License (LGPL) version 2.1 and BSD. 15 | Reference: http://www.webkit.org/coding/lgpl-license.html and 16 | http://www.webkit.org/coding/bsd-license.html. 17 | 18 | Mongoose - https://github.com/cesanta/mongoose 19 | License: MIT 20 | Reference: https://github.com/cesanta/mongoose/commit/abbf27338ef554cce0281ac157aa71a9c1b82a55 21 | 22 | Breakpad - http://code.google.com/p/google-breakpad/ 23 | License: BSD. 24 | Reference: http://code.google.com/p/google-breakpad/source/browse/trunk/COPYING. 25 | 26 | OpenSSL - http://www.openssl.org/ 27 | License: OpenSSL License, SSLeay License. 28 | Reference: http://www.openssl.org/source/license.html. 29 | 30 | Linenoise - https://github.com/tadmarshall/linenoise 31 | License: BSD. 32 | Reference: https://github.com/tadmarshall/linenoise/blob/master/linenoise.h. 33 | 34 | QCommandLine - http://xf.iksaif.net/dev/qcommandline.html 35 | License: GNU Lesser General Public License (LGPL) version 2.1. 36 | Reference: http://dev.iksaif.net/projects/qcommandline/repository/revisions/master/entry/COPYING 37 | 38 | CoffeeScript - http://coffeescript.org/ 39 | License: MIT. 40 | Reference: https://github.com/jashkenas/coffee-script/blob/master/README. 41 | 42 | GIFLIB - http://giflib.sourceforge.net/ 43 | License: MIT 44 | Reference: http://giflib.cvs.sourceforge.net/viewvc/giflib/giflib/COPYING 45 | 46 | wkhtmlpdf - http://code.google.com/p/wkhtmltopdf/ 47 | License: GNU Lesser General Public License (LGPL) 48 | Reference: http://code.google.com/p/wkhtmltopdf/ 49 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/Constants.java: -------------------------------------------------------------------------------- 1 | package com.unbank; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class Constants { 7 | 8 | public static Integer SERVERPORT; 9 | public static String SERVERIP; 10 | public static boolean USE_SSL; 11 | 12 | public static int HEARTBEATRATE; 13 | public static String HEARTBEATREQUEST = "HEARTBEATREQUEST"; 14 | public static String HEARTBEATRESPONSE = "HEARTBEATRESPONSE"; 15 | public static int HEART_TIMEOUT; 16 | public static int CLENT_TIMEOUT; 17 | 18 | public static Boolean ISTANCHUANG; 19 | 20 | public void init() { 21 | 22 | } 23 | 24 | public static Integer getSERVERPORT() { 25 | return SERVERPORT; 26 | } 27 | 28 | public static void setSERVERPORT(Integer sERVERPORT) { 29 | SERVERPORT = sERVERPORT; 30 | } 31 | 32 | public static String getSERVERIP() { 33 | return SERVERIP; 34 | } 35 | 36 | public static void setSERVERIP(String sERVERIP) { 37 | SERVERIP = sERVERIP; 38 | } 39 | 40 | public static boolean isUSE_SSL() { 41 | return USE_SSL; 42 | } 43 | 44 | public static void setUSE_SSL(boolean uSE_SSL) { 45 | USE_SSL = uSE_SSL; 46 | } 47 | 48 | public static int getHEARTBEATRATE() { 49 | return HEARTBEATRATE; 50 | } 51 | 52 | public static void setHEARTBEATRATE(int hEARTBEATRATE) { 53 | HEARTBEATRATE = hEARTBEATRATE; 54 | } 55 | 56 | public static String getHEARTBEATREQUEST() { 57 | return HEARTBEATREQUEST; 58 | } 59 | 60 | public static void setHEARTBEATREQUEST(String hEARTBEATREQUEST) { 61 | HEARTBEATREQUEST = hEARTBEATREQUEST; 62 | } 63 | 64 | public static String getHEARTBEATRESPONSE() { 65 | return HEARTBEATRESPONSE; 66 | } 67 | 68 | public static void setHEARTBEATRESPONSE(String hEARTBEATRESPONSE) { 69 | HEARTBEATRESPONSE = hEARTBEATRESPONSE; 70 | } 71 | 72 | public static int getHEART_TIMEOUT() { 73 | return HEART_TIMEOUT; 74 | } 75 | 76 | public static void setHEART_TIMEOUT(int hEART_TIMEOUT) { 77 | HEART_TIMEOUT = hEART_TIMEOUT; 78 | } 79 | 80 | public static int getCLENT_TIMEOUT() { 81 | return CLENT_TIMEOUT; 82 | } 83 | 84 | public static void setCLENT_TIMEOUT(int cLENT_TIMEOUT) { 85 | CLENT_TIMEOUT = cLENT_TIMEOUT; 86 | } 87 | 88 | public static Boolean getISTANCHUANG() { 89 | return ISTANCHUANG; 90 | } 91 | 92 | public static void setISTANCHUANG(Boolean iSTANCHUANG) { 93 | ISTANCHUANG = iSTANCHUANG; 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/Spider.java: -------------------------------------------------------------------------------- 1 | package com.unbank; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.apache.log4j.PropertyConfigurator; 6 | import org.springframework.context.support.ClassPathXmlApplicationContext; 7 | 8 | public class Spider { 9 | private static Log logger = LogFactory.getLog(Spider.class); 10 | static { 11 | // 启动日志 12 | try { 13 | PropertyConfigurator.configure(Spider.class.getClassLoader() 14 | .getResource("").toURI().getPath() 15 | + "log4j.properties"); 16 | logger.info("---日志系统启动成功---"); 17 | } catch (Exception e) { 18 | logger.error("日志系统启动失败:", e); 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | new ClassPathXmlApplicationContext( 24 | new String[] { "applicationContext.xml" }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/entity/News.java: -------------------------------------------------------------------------------- 1 | package com.unbank.entity; 2 | 3 | public class News { 4 | 5 | public int id; 6 | public String title; 7 | public String content; 8 | public Long time; 9 | public String url; 10 | public Long crawlTime; 11 | 12 | public int getId() { 13 | return id; 14 | } 15 | 16 | public void setId(int id) { 17 | this.id = id; 18 | } 19 | 20 | public String getTitle() { 21 | return title; 22 | } 23 | 24 | public void setTitle(String title) { 25 | this.title = title; 26 | } 27 | 28 | public String getContent() { 29 | return content; 30 | } 31 | 32 | public void setContent(String content) { 33 | this.content = content; 34 | } 35 | 36 | public Long getTime() { 37 | return time; 38 | } 39 | 40 | public void setTime(Long time) { 41 | this.time = time; 42 | } 43 | 44 | public String getUrl() { 45 | return url; 46 | } 47 | 48 | public void setUrl(String url) { 49 | this.url = url; 50 | } 51 | 52 | public Long getCrawlTime() { 53 | return crawlTime; 54 | } 55 | 56 | public void setCrawlTime(Long crawlTime) { 57 | this.crawlTime = crawlTime; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/entity/Weixin.java: -------------------------------------------------------------------------------- 1 | package com.unbank.entity; 2 | 3 | public class Weixin extends News { 4 | 5 | public String weixin_account; 6 | 7 | public String openid; 8 | 9 | public String getWeixin_account() { 10 | return weixin_account; 11 | } 12 | 13 | public void setWeixin_account(String weixin_account) { 14 | this.weixin_account = weixin_account; 15 | } 16 | 17 | public String getOpenid() { 18 | return openid; 19 | } 20 | 21 | public void setOpenid(String openid) { 22 | this.openid = openid; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/fetch/PhantomjsFetcher.java: -------------------------------------------------------------------------------- 1 | package com.unbank.fetch; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.apache.commons.logging.Log; 6 | import org.apache.commons.logging.LogFactory; 7 | import org.openqa.selenium.JavascriptExecutor; 8 | import org.openqa.selenium.WebDriver; 9 | import org.openqa.selenium.phantomjs.PhantomJSDriver; 10 | import org.openqa.selenium.phantomjs.PhantomJSDriverService; 11 | import org.openqa.selenium.remote.CapabilityType; 12 | import org.openqa.selenium.remote.DesiredCapabilities; 13 | import org.openqa.selenium.support.ui.ExpectedCondition; 14 | import org.openqa.selenium.support.ui.Wait; 15 | import org.openqa.selenium.support.ui.WebDriverWait; 16 | 17 | public class PhantomjsFetcher { 18 | 19 | private static Log logger = LogFactory.getLog(PhantomjsFetcher.class); 20 | 21 | private static WebDriver driver; 22 | 23 | public static synchronized WebDriver getInstenceDriver() { 24 | if (driver == null) { 25 | DesiredCapabilities caps = new DesiredCapabilities(); 26 | caps.setJavascriptEnabled(true); 27 | caps.setCapability( 28 | PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, 29 | "phantomjs-1.9.7-windows/phantomjs.exe"); 30 | driver = new PhantomJSDriver(caps); 31 | long timeout = 5000; 32 | TimeUnit timeUnit = TimeUnit.MILLISECONDS; 33 | driver.manage().timeouts().pageLoadTimeout(timeout, timeUnit); 34 | } 35 | return driver; 36 | } 37 | 38 | public PhantomjsFetcher() { 39 | getInstenceDriver(); 40 | } 41 | 42 | public static String get(String url) { 43 | getInstenceDriver(); 44 | String html = null; 45 | try { 46 | driver.get(url); 47 | waitForPageLoaded(driver); 48 | html = driver.getPageSource(); 49 | 50 | } catch (Exception e) { 51 | 52 | if (e instanceof org.openqa.selenium.TimeoutException) { 53 | System.out.println(((JavascriptExecutor) driver) 54 | .executeScript("return document.readyState")); 55 | logger.info(url + " " + "读取超时"); 56 | } else { 57 | e.printStackTrace(); 58 | } 59 | } finally { 60 | html = driver.getPageSource(); 61 | } 62 | return html; 63 | } 64 | 65 | public void scroll() { 66 | ((JavascriptExecutor) driver) 67 | .executeScript("window.scrollTo(0,document.body.scrollHeight)"); 68 | } 69 | 70 | public static void waitForPageLoaded(WebDriver driver) { 71 | ExpectedCondition expectation = new ExpectedCondition() { 72 | public Boolean apply(WebDriver driver) { 73 | // System.out.println(((JavascriptExecutor) driver) 74 | // .executeScript("return document.readyState")); 75 | return ((JavascriptExecutor) driver).executeScript( 76 | "return document.readyState").equals("complete"); 77 | } 78 | }; 79 | Wait wait = new WebDriverWait(driver, 5000); 80 | try { 81 | wait.until(expectation); 82 | } catch (Throwable error) { 83 | logger.info(error); 84 | } 85 | } 86 | 87 | public String get(String url, String string) { 88 | 89 | return null; 90 | } 91 | 92 | public void setProxy(String proxyIp, String proxyPort) { 93 | closeDriver(); 94 | String PROXY = proxyIp + ":" + proxyPort; 95 | org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); 96 | proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY); 97 | DesiredCapabilities caps = new DesiredCapabilities(); 98 | caps.setJavascriptEnabled(true); 99 | caps.setCapability(CapabilityType.PROXY, proxy); 100 | caps.setCapability( 101 | PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, 102 | "phantomjs-1.9.7-windows/phantomjs.exe"); 103 | driver = new PhantomJSDriver(caps); 104 | long timeout = 5000; 105 | TimeUnit timeUnit = TimeUnit.MILLISECONDS; 106 | driver.manage().timeouts().pageLoadTimeout(timeout, timeUnit); 107 | } 108 | 109 | public static void closeDriver() { 110 | if (driver != null) { 111 | driver.quit(); 112 | } 113 | 114 | }; 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/fetch/SougouWeiXinFetcher.java: -------------------------------------------------------------------------------- 1 | package com.unbank.fetch; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.apache.http.client.methods.HttpGet; 6 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 7 | 8 | public class SougouWeiXinFetcher extends Fetcher { 9 | 10 | private static Log logger = LogFactory.getLog(SougouWeiXinFetcher.class); 11 | 12 | public static SougouWeiXinFetcher fetcher = SougouWeiXinFetcher 13 | .getInstance(); 14 | 15 | public synchronized static SougouWeiXinFetcher getInstance() { 16 | if (fetcher == null) { 17 | fetcher = new SougouWeiXinFetcher(); 18 | PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); 19 | HttpClientBuilder httpClientBuilder = new HttpClientBuilder(false, 20 | poolingHttpClientConnectionManager, cookieStore); 21 | httpClient = httpClientBuilder.getHttpClient(); 22 | } 23 | return fetcher; 24 | } 25 | 26 | public void fillHeaderWithCookie(String url, HttpGet httpGet, String cookie) { 27 | httpGet.setHeader( 28 | "User-Agent", 29 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"); 30 | httpGet.setHeader("Accept", 31 | "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 32 | httpGet.setHeader("Accept-Language", 33 | "zh-CN,zh;q=0.8,en-us;q=0.8,en;q=0.6"); 34 | httpGet.setHeader("Accept-Encoding", "gzip, deflate,sdch"); 35 | httpGet.setHeader("Host", "weixin.sogou.com"); 36 | httpGet.setHeader("Connection", "keep-alive"); 37 | httpGet.setHeader("Referer", "http://weixin.sogou.com/"); 38 | httpGet.setHeader("Cache-Control", "max-age=0"); 39 | httpGet.setHeader("Cookie", cookie); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/filter/url/SimpleBloomFilter.java: -------------------------------------------------------------------------------- 1 | package com.unbank.filter.url; 2 | 3 | import java.util.BitSet; 4 | import java.util.List; 5 | 6 | import com.unbank.redis.JedisUtil; 7 | 8 | public class SimpleBloomFilter { 9 | private static final int DEFAULT_SIZE = 2 << 24; 10 | private static final int[] seeds = new int[] { 5, 7, 11, 13, 31, 37, 61 }; 11 | private BitSet bits = new BitSet(DEFAULT_SIZE); 12 | private SimpleHash[] func = new SimpleHash[seeds.length]; 13 | static SimpleBloomFilter filter; 14 | public static JedisUtil jedisUtil; 15 | public SimpleBloomFilter() { 16 | for (int i = 0; i < seeds.length; i++) { 17 | func[i] = new SimpleHash(DEFAULT_SIZE, seeds[i]); 18 | } 19 | } 20 | 21 | public void add(String value) { 22 | for (SimpleHash f : func) { 23 | bits.set(f.hash(value), true); 24 | } 25 | } 26 | 27 | public boolean contains(String value) { 28 | if (value == null) { 29 | return false; 30 | } 31 | boolean ret = true; 32 | for (SimpleHash f : func) { 33 | ret = ret && bits.get(f.hash(value)); 34 | } 35 | return ret; 36 | } 37 | 38 | // 内部类,simpleHash 39 | public static class SimpleHash { 40 | private int cap; 41 | private int seed; 42 | 43 | public SimpleHash(int cap, int seed) { 44 | this.cap = cap; 45 | this.seed = seed; 46 | } 47 | 48 | public int hash(String value) { 49 | int result = 0; 50 | int len = value.length(); 51 | for (int i = 0; i < len; i++) { 52 | result = seed * result + value.charAt(i); 53 | } 54 | return (cap - 1) & result; 55 | } 56 | } 57 | 58 | public synchronized static SimpleBloomFilter getInstance() { 59 | if (filter == null) { 60 | filter = new SimpleBloomFilter(); 61 | List urls =jedisUtil.getList("finishurls"); 62 | for (String string : urls) { 63 | filter.add(string); 64 | } 65 | } 66 | return filter; 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/clent/TLSClentHandler.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.clent; 2 | 3 | import java.net.InetSocketAddress; 4 | 5 | import org.apache.commons.logging.Log; 6 | import org.apache.commons.logging.LogFactory; 7 | import org.apache.mina.core.service.IoHandlerAdapter; 8 | import org.apache.mina.core.session.IoSession; 9 | 10 | import com.unbank.Constants; 11 | import com.unbank.view.RightCornerPopMessage; 12 | 13 | public class TLSClentHandler extends IoHandlerAdapter { 14 | 15 | static Log logger = LogFactory.getLog(TLSClentHandler.class); 16 | 17 | @Override 18 | public void sessionCreated(IoSession session) throws Exception { 19 | logger.info("与服务器建立连接"); 20 | } 21 | 22 | @Override 23 | public void sessionClosed(IoSession session) throws Exception { 24 | InetSocketAddress remoteAddress = (InetSocketAddress) session 25 | .getRemoteAddress(); 26 | logger.info(remoteAddress + "和服务器断开了链接 Session 失效"); 27 | } 28 | 29 | @Override 30 | public void exceptionCaught(IoSession session, Throwable cause) 31 | throws Exception { 32 | InetSocketAddress remoteAddress = (InetSocketAddress) session 33 | .getRemoteAddress(); 34 | logger.info(remoteAddress + "有异常"); 35 | if (Constants.ISTANCHUANG) { 36 | new RightCornerPopMessage(remoteAddress + "有异常"); 37 | } 38 | // super.exceptionCaught(session, cause); 39 | } 40 | 41 | @Override 42 | public void messageReceived(IoSession session, Object message) 43 | throws Exception { 44 | logger.info("客户端接受的消息:" + message); 45 | if (Constants.ISTANCHUANG) { 46 | String temp = message.toString(); 47 | new RightCornerPopMessage(temp); 48 | } 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/clent/keeplive/ClentKeepAliveMessageFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.clent.keeplive; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.apache.mina.core.session.IoSession; 6 | import org.apache.mina.filter.keepalive.KeepAliveMessageFactory; 7 | 8 | import com.unbank.Constants; 9 | 10 | public class ClentKeepAliveMessageFactoryImpl implements 11 | KeepAliveMessageFactory { 12 | 13 | private static Log logger = LogFactory 14 | .getLog(ClentKeepAliveMessageFactoryImpl.class); 15 | 16 | public Object getRequest(IoSession arg0) { 17 | return Constants.HEARTBEATREQUEST; 18 | } 19 | 20 | public Object getResponse(IoSession arg0, Object arg1) { 21 | return Constants.HEARTBEATRESPONSE; 22 | } 23 | 24 | public boolean isRequest(IoSession arg0, Object arg1) { 25 | // logger.info(arg1); 26 | if (arg1 instanceof String) { 27 | String temp = (String) arg1; 28 | return temp.equals(Constants.HEARTBEATREQUEST); 29 | } 30 | return false; 31 | } 32 | 33 | public boolean isResponse(IoSession arg0, Object message) { 34 | // logger.info(message); 35 | if (message instanceof String) { 36 | String temp = (String) message; 37 | 38 | return temp.equals(Constants.HEARTBEATRESPONSE); 39 | } 40 | return false; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/clent/keeplive/KeepAliveRequestTimeoutHandlerImpl.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.clent.keeplive; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.apache.mina.core.session.IoSession; 6 | import org.apache.mina.filter.keepalive.KeepAliveFilter; 7 | import org.apache.mina.filter.keepalive.KeepAliveRequestTimeoutHandler; 8 | 9 | public class KeepAliveRequestTimeoutHandlerImpl implements 10 | KeepAliveRequestTimeoutHandler { 11 | private static Log logger = LogFactory 12 | .getLog(KeepAliveRequestTimeoutHandlerImpl.class); 13 | 14 | public void keepAliveRequestTimedOut(KeepAliveFilter arg0, IoSession arg1) 15 | throws Exception { 16 | 17 | logger.info(arg1.getRemoteAddress() + "心跳监测超时"); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/clent/listener/IoListener.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.clent.listener; 2 | 3 | import org.apache.mina.core.service.IoService; 4 | import org.apache.mina.core.service.IoServiceListener; 5 | import org.apache.mina.core.session.IdleStatus; 6 | import org.apache.mina.core.session.IoSession; 7 | 8 | public class IoListener implements IoServiceListener { 9 | 10 | public void serviceActivated(IoService service) throws Exception { 11 | // TODO Auto-generated method stub 12 | 13 | } 14 | 15 | public void serviceIdle(IoService service, IdleStatus idleStatus) 16 | throws Exception { 17 | // TODO Auto-generated method stub 18 | 19 | } 20 | 21 | public void serviceDeactivated(IoService service) throws Exception { 22 | // TODO Auto-generated method stub 23 | 24 | } 25 | 26 | public void sessionCreated(IoSession session) throws Exception { 27 | // TODO Auto-generated method stub 28 | 29 | } 30 | 31 | public void sessionClosed(IoSession session) throws Exception { 32 | // TODO Auto-generated method stub 33 | 34 | } 35 | 36 | public void sessionDestroyed(IoSession session) throws Exception { 37 | 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/server/TLSServer.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.server; 2 | 3 | import java.net.InetSocketAddress; 4 | import java.nio.charset.Charset; 5 | import java.util.concurrent.Executors; 6 | 7 | import org.apache.commons.logging.Log; 8 | import org.apache.commons.logging.LogFactory; 9 | import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; 10 | import org.apache.mina.core.session.IdleStatus; 11 | import org.apache.mina.filter.codec.ProtocolCodecFilter; 12 | import org.apache.mina.filter.codec.textline.TextLineCodecFactory; 13 | import org.apache.mina.filter.executor.ExecutorFilter; 14 | import org.apache.mina.filter.keepalive.KeepAliveFilter; 15 | import org.apache.mina.filter.keepalive.KeepAliveMessageFactory; 16 | import org.apache.mina.filter.keepalive.KeepAliveRequestTimeoutHandler; 17 | import org.apache.mina.filter.ssl.SslFilter; 18 | import org.apache.mina.transport.socket.SocketAcceptor; 19 | import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 20 | 21 | import com.unbank.Constants; 22 | import com.unbank.mina.server.keeplive.KeepAliveMessageFactoryImpl; 23 | import com.unbank.mina.server.keeplive.KeepAliveRequestTimeoutHandlerImpl; 24 | import com.unbank.mina.ssl.BogusSslContextFactory; 25 | 26 | public class TLSServer { 27 | 28 | private static Log logger = LogFactory.getLog(TLSServer.class); 29 | 30 | public TLSServer() { 31 | createAcceptor(); 32 | } 33 | 34 | private static void createAcceptor() { 35 | try { 36 | // 创建服务器端连接器 37 | SocketAcceptor acceptor = new NioSocketAcceptor(); 38 | acceptor.getSessionConfig().setReadBufferSize(2048); 39 | acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); 40 | // acceptor.setReuseAddress(true); 41 | // 获取默认过滤器 42 | DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); 43 | // 设置加密过滤器 44 | if (Constants.USE_SSL) { 45 | addSSLSupport(chain); 46 | } 47 | // 设置编码过滤器和按行读取数据模式 48 | // chain.addLast("codec", new ProtocolCodecFilter( 49 | // new TextLineCodecFactory(Charset.forName("UTF-8")))); 50 | chain.addLast("threadPool", 51 | new ExecutorFilter(Executors.newCachedThreadPool())); 52 | // 设定 对象传输工厂 53 | // ObjectSerializationCodecFactory factory = new 54 | // ObjectSerializationCodecFactory(); 55 | // 设定传输最大值 56 | TextLineCodecFactory factory = new TextLineCodecFactory( 57 | Charset.forName("UTF-8")); 58 | factory.setDecoderMaxLineLength(Integer.MAX_VALUE);// 设定后服务器可以接收大数据 59 | factory.setEncoderMaxLineLength(Integer.MAX_VALUE); 60 | 61 | // 设置编码过滤器和按行读取数据模式 62 | chain.addLast("codeobj", new ProtocolCodecFilter(factory)); 63 | 64 | KeepAliveMessageFactory keepAliveMessageFactory = new KeepAliveMessageFactoryImpl(); 65 | KeepAliveRequestTimeoutHandler keepAliveRequestTimeoutHandler = new KeepAliveRequestTimeoutHandlerImpl(); 66 | KeepAliveFilter keepAliveFilter = new KeepAliveFilter( 67 | keepAliveMessageFactory, IdleStatus.BOTH_IDLE, 68 | keepAliveRequestTimeoutHandler); 69 | // ** 是否回发 *//* 70 | keepAliveFilter.setForwardEvent(true); 71 | keepAliveFilter.setRequestTimeout(Constants.HEART_TIMEOUT); 72 | // ** 发送频率 *//* 73 | keepAliveFilter.setRequestInterval(Constants.HEARTBEATRATE); 74 | // acceptor.getFilterChain().addLast("heartbeat", keepAliveFilter); 75 | 76 | // 设置事件处理器 77 | acceptor.setHandler(new TLSServerHandler()); 78 | // 处理器的代码如下 79 | // 服务绑定到此端口号 80 | acceptor.bind(new InetSocketAddress(Constants.SERVERPORT)); 81 | } catch (Exception e) { 82 | e.printStackTrace(); 83 | } 84 | } 85 | 86 | private static void addSSLSupport(DefaultIoFilterChainBuilder chain) 87 | throws Exception { 88 | SslFilter sslFilter = new SslFilter( 89 | BogusSslContextFactory.getInstance(true)); 90 | chain.addLast("sslFilter", sslFilter); 91 | } 92 | } -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/server/TLSServerHandler.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.server; 2 | 3 | import java.net.InetSocketAddress; 4 | import java.util.Collections; 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | import org.apache.commons.logging.Log; 9 | import org.apache.commons.logging.LogFactory; 10 | import org.apache.mina.core.service.IoHandlerAdapter; 11 | import org.apache.mina.core.session.IoSession; 12 | 13 | import com.unbank.Constants; 14 | 15 | public class TLSServerHandler extends IoHandlerAdapter { 16 | 17 | static Log logger = LogFactory.getLog(TLSServerHandler.class); 18 | private static final Set sessions = Collections 19 | .synchronizedSet(new HashSet()); 20 | 21 | public static Set checkSession = Collections 22 | .synchronizedSet(new HashSet()); 23 | 24 | @Override 25 | public void exceptionCaught(IoSession session, Throwable cause) 26 | throws Exception { 27 | InetSocketAddress remoteAddress = (InetSocketAddress) session 28 | .getRemoteAddress(); 29 | logger.info(remoteAddress + "客户端到链接异常", cause); 30 | broadcast(remoteAddress + "客户端到链接异常" + cause.getMessage()); 31 | // super.exceptionCaught(session, cause); 32 | } 33 | 34 | @Override 35 | public void sessionClosed(IoSession session) throws Exception { 36 | sessions.remove(session); 37 | } 38 | 39 | @Override 40 | public void sessionCreated(IoSession session) throws Exception { 41 | super.sessionCreated(session); 42 | InetSocketAddress remoteAddress = (InetSocketAddress) session 43 | .getRemoteAddress(); 44 | sessions.add(session); 45 | logger.info(remoteAddress + "与服务器建立连接"); 46 | 47 | } 48 | 49 | // 当客户端发送的消息到达时: 50 | public void messageReceived(IoSession session, Object message) { 51 | InetSocketAddress remoteAddress = (InetSocketAddress) session 52 | .getRemoteAddress(); 53 | logger.info("接收到客户端" + remoteAddress + "消息" + message); 54 | if (message instanceof Integer) { 55 | 56 | } else if (message instanceof String) { 57 | String temp = (String) message; 58 | if (temp.equals(Constants.HEARTBEATREQUEST) 59 | || temp.equals(Constants.HEARTBEATRESPONSE)) { 60 | logger.info(remoteAddress + "心跳监测"); 61 | } else { 62 | broadcast(remoteAddress + "异常消息" + temp); 63 | } 64 | } 65 | } 66 | 67 | public void messageSent(IoSession session, Object message) { 68 | 69 | }; 70 | 71 | public void broadcast(String message) { 72 | synchronized (sessions) { 73 | for (IoSession session : sessions) { 74 | if (session.isConnected()) { 75 | session.write(message); 76 | } 77 | } 78 | } 79 | } 80 | } -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/server/keeplive/KeepAliveMessageFactoryImpl.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.server.keeplive; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.apache.mina.core.session.IoSession; 6 | import org.apache.mina.filter.keepalive.KeepAliveMessageFactory; 7 | 8 | import com.unbank.Constants; 9 | 10 | public class KeepAliveMessageFactoryImpl implements KeepAliveMessageFactory { 11 | 12 | private static Log logger = LogFactory 13 | .getLog(KeepAliveMessageFactoryImpl.class); 14 | 15 | public Object getRequest(IoSession arg0) { 16 | // logger.info(arg0.getRemoteAddress() + Constants.HEARTBEATREQUEST); 17 | return Constants.HEARTBEATREQUEST; 18 | } 19 | 20 | public Object getResponse(IoSession arg0, Object arg1) { 21 | // logger.info(arg0.getRemoteAddress() + Constants.HEARTBEATRESPONSE); 22 | return Constants.HEARTBEATRESPONSE; 23 | } 24 | 25 | public boolean isRequest(IoSession arg0, Object arg1) { 26 | // logger.info(arg1); 27 | if (arg1 instanceof String) { 28 | String temp = (String) arg1; 29 | return temp.equals(Constants.HEARTBEATREQUEST); 30 | } 31 | 32 | return false; 33 | } 34 | 35 | public boolean isResponse(IoSession arg0, Object message) { 36 | // logger.info(message); 37 | if (message instanceof String) { 38 | String temp = (String) message; 39 | return temp.equals(Constants.HEARTBEATRESPONSE); 40 | } 41 | return false; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/server/keeplive/KeepAliveRequestTimeoutHandlerImpl.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mina.server.keeplive; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.apache.mina.core.session.IoSession; 6 | import org.apache.mina.filter.keepalive.KeepAliveFilter; 7 | import org.apache.mina.filter.keepalive.KeepAliveRequestTimeoutHandler; 8 | 9 | public class KeepAliveRequestTimeoutHandlerImpl implements 10 | KeepAliveRequestTimeoutHandler { 11 | private static Log logger = LogFactory 12 | .getLog(KeepAliveRequestTimeoutHandlerImpl.class); 13 | 14 | public void keepAliveRequestTimedOut(KeepAliveFilter arg0, IoSession arg1) 15 | throws Exception { 16 | 17 | logger.info(arg1.getRemoteAddress() + "心跳监测超时"); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/ssl/BogusTrustManagerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | */ 20 | package com.unbank.mina.ssl; 21 | 22 | import java.security.InvalidAlgorithmParameterException; 23 | import java.security.KeyStore; 24 | import java.security.KeyStoreException; 25 | import java.security.cert.CertificateException; 26 | import java.security.cert.X509Certificate; 27 | 28 | import javax.net.ssl.ManagerFactoryParameters; 29 | import javax.net.ssl.TrustManager; 30 | import javax.net.ssl.TrustManagerFactorySpi; 31 | import javax.net.ssl.X509TrustManager; 32 | 33 | /** 34 | * Bogus trust manager factory. Creates BogusX509TrustManager 35 | * 36 | * @author Apache MINA Project 37 | */ 38 | class BogusTrustManagerFactory extends TrustManagerFactorySpi { 39 | 40 | static final X509TrustManager X509 = new X509TrustManager() { 41 | public void checkClientTrusted(X509Certificate[] x509Certificates, 42 | String s) throws CertificateException { 43 | } 44 | 45 | public void checkServerTrusted(X509Certificate[] x509Certificates, 46 | String s) throws CertificateException { 47 | } 48 | 49 | public X509Certificate[] getAcceptedIssuers() { 50 | return new X509Certificate[0]; 51 | } 52 | }; 53 | 54 | static final TrustManager[] X509_MANAGERS = new TrustManager[] { X509 }; 55 | 56 | public BogusTrustManagerFactory() { 57 | } 58 | 59 | @Override 60 | protected TrustManager[] engineGetTrustManagers() { 61 | return X509_MANAGERS; 62 | } 63 | 64 | @Override 65 | protected void engineInit(KeyStore keystore) throws KeyStoreException { 66 | // noop 67 | } 68 | 69 | @Override 70 | protected void engineInit(ManagerFactoryParameters managerFactoryParameters) 71 | throws InvalidAlgorithmParameterException { 72 | // noop 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/ssl/SslServerSocketFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | */ 20 | package com.unbank.mina.ssl; 21 | 22 | import java.io.IOException; 23 | import java.net.InetAddress; 24 | import java.net.ServerSocket; 25 | import java.security.GeneralSecurityException; 26 | 27 | import javax.net.ServerSocketFactory; 28 | 29 | /** 30 | * Simple Server Socket factory to create sockets with or without SSL enabled. 31 | * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes) 32 | * 33 | * @author Apache MINA Project 34 | */ 35 | public class SslServerSocketFactory extends javax.net.ServerSocketFactory { 36 | private static boolean sslEnabled = false; 37 | 38 | private static javax.net.ServerSocketFactory sslFactory = null; 39 | 40 | private static ServerSocketFactory factory = null; 41 | 42 | public SslServerSocketFactory() { 43 | super(); 44 | } 45 | 46 | @Override 47 | public ServerSocket createServerSocket(int port) throws IOException { 48 | return new ServerSocket(port); 49 | } 50 | 51 | @Override 52 | public ServerSocket createServerSocket(int port, int backlog) 53 | throws IOException { 54 | return new ServerSocket(port, backlog); 55 | } 56 | 57 | @Override 58 | public ServerSocket createServerSocket(int port, int backlog, 59 | InetAddress ifAddress) throws IOException { 60 | return new ServerSocket(port, backlog, ifAddress); 61 | } 62 | 63 | public static javax.net.ServerSocketFactory getServerSocketFactory() 64 | throws IOException { 65 | if (isSslEnabled()) { 66 | if (sslFactory == null) { 67 | try { 68 | sslFactory = BogusSslContextFactory.getInstance(true) 69 | .getServerSocketFactory(); 70 | } catch (GeneralSecurityException e) { 71 | IOException ioe = new IOException( 72 | "could not create SSL socket"); 73 | ioe.initCause(e); 74 | throw ioe; 75 | } 76 | } 77 | return sslFactory; 78 | } else { 79 | if (factory == null) { 80 | factory = new SslServerSocketFactory(); 81 | } 82 | return factory; 83 | } 84 | 85 | } 86 | 87 | public static boolean isSslEnabled() { 88 | return sslEnabled; 89 | } 90 | 91 | public static void setSslEnabled(boolean newSslEnabled) { 92 | sslEnabled = newSslEnabled; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mina/ssl/SslSocketFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | * 19 | */ 20 | package com.unbank.mina.ssl; 21 | 22 | import java.io.IOException; 23 | import java.net.InetAddress; 24 | import java.net.Socket; 25 | import java.net.UnknownHostException; 26 | import java.security.GeneralSecurityException; 27 | 28 | import javax.net.SocketFactory; 29 | 30 | /** 31 | * Simple Socket factory to create sockets with or without SSL enabled. 32 | * If SSL enabled a "bougus" SSL Context is used (suitable for test purposes) 33 | * 34 | * @author Apache MINA Project 35 | */ 36 | public class SslSocketFactory extends SocketFactory { 37 | private static boolean sslEnabled = false; 38 | 39 | private static javax.net.ssl.SSLSocketFactory sslFactory = null; 40 | 41 | private static javax.net.SocketFactory factory = null; 42 | 43 | public SslSocketFactory() { 44 | super(); 45 | } 46 | 47 | @Override 48 | public Socket createSocket(String arg1, int arg2) throws IOException, 49 | UnknownHostException { 50 | if (isSslEnabled()) { 51 | return getSSLFactory().createSocket(arg1, arg2); 52 | } else { 53 | return new Socket(arg1, arg2); 54 | } 55 | } 56 | 57 | @Override 58 | public Socket createSocket(String arg1, int arg2, InetAddress arg3, int arg4) 59 | throws IOException, UnknownHostException { 60 | if (isSslEnabled()) { 61 | return getSSLFactory().createSocket(arg1, arg2, arg3, arg4); 62 | } else { 63 | return new Socket(arg1, arg2, arg3, arg4); 64 | } 65 | } 66 | 67 | @Override 68 | public Socket createSocket(InetAddress arg1, int arg2) throws IOException { 69 | if (isSslEnabled()) { 70 | return getSSLFactory().createSocket(arg1, arg2); 71 | } else { 72 | return new Socket(arg1, arg2); 73 | } 74 | } 75 | 76 | @Override 77 | public Socket createSocket(InetAddress arg1, int arg2, InetAddress arg3, 78 | int arg4) throws IOException { 79 | if (isSslEnabled()) { 80 | return getSSLFactory().createSocket(arg1, arg2, arg3, arg4); 81 | } else { 82 | return new Socket(arg1, arg2, arg3, arg4); 83 | } 84 | } 85 | 86 | public static javax.net.SocketFactory getSocketFactory() { 87 | if (factory == null) { 88 | factory = new SslSocketFactory(); 89 | } 90 | return factory; 91 | } 92 | 93 | private javax.net.ssl.SSLSocketFactory getSSLFactory() { 94 | if (sslFactory == null) { 95 | try { 96 | sslFactory = BogusSslContextFactory.getInstance(false) 97 | .getSocketFactory(); 98 | } catch (GeneralSecurityException e) { 99 | throw new RuntimeException("could not create SSL socket", e); 100 | } 101 | } 102 | return sslFactory; 103 | } 104 | 105 | public static boolean isSslEnabled() { 106 | return sslEnabled; 107 | } 108 | 109 | public static void setSslEnabled(boolean newSslEnabled) { 110 | sslEnabled = newSslEnabled; 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mybatis/entity/SQLAdapter.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mybatis.entity; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | public class SQLAdapter { 7 | 8 | private Integer prikey; 9 | private String sql; 10 | private Map obj; 11 | private List list; 12 | 13 | public SQLAdapter(Map objects) { 14 | this.obj = objects; 15 | } 16 | 17 | public SQLAdapter(String sql) { 18 | this.sql = sql; 19 | } 20 | 21 | public SQLAdapter(String sql, Map objects) { 22 | this.sql = sql; 23 | this.obj = objects; 24 | } 25 | 26 | public SQLAdapter(List list) { 27 | this.list = list; 28 | } 29 | 30 | public SQLAdapter() { 31 | } 32 | 33 | public String getSql() { 34 | return sql; 35 | } 36 | 37 | public void setSql(String sql) { 38 | this.sql = sql; 39 | } 40 | 41 | public Map getObj() { 42 | return obj; 43 | } 44 | 45 | public void setObj(Map obj) { 46 | this.obj = obj; 47 | } 48 | 49 | public List getList() { 50 | return list; 51 | } 52 | 53 | public void setList(List list) { 54 | this.list = list; 55 | } 56 | 57 | public Integer getPrikey() { 58 | return prikey; 59 | } 60 | 61 | public void setPrikey(Integer prikey) { 62 | this.prikey = prikey; 63 | } 64 | 65 | 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mybatis/factory/DynamicConnectionFactory.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mybatis.factory; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.apache.ibatis.io.Resources; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | 10 | public class DynamicConnectionFactory { 11 | public static Map sessionFactorys = new HashMap(); 12 | 13 | public static SqlSessionFactory getInstanceSessionFactory(String environment) { 14 | if (sessionFactorys.get(environment) == null) { 15 | String resource = "mybatis.xml"; 16 | try { 17 | SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder() 18 | .build(Resources.getResourceAsReader(resource), 19 | environment); 20 | 21 | sessionFactorys.put(environment, sessionFactory); 22 | } catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | return sessionFactorys.get(environment); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mybatis/mapper/SQLAdapterMapper.java: -------------------------------------------------------------------------------- 1 | package com.unbank.mybatis.mapper; 2 | 3 | import com.unbank.mybatis.entity.SQLAdapter; 4 | 5 | public interface SQLAdapterMapper { 6 | 7 | void executeSQL(SQLAdapter sqlAdapter); 8 | 9 | void insertNoPriKey(SQLAdapter sqlAdapter); 10 | 11 | int insertReturnPriKey(SQLAdapter sqlAdapter); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/mybatis/mapper/SQLAdapterMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ${sql} ( 7 | ${key} 9 | ) values ( 10 | #{item} 12 | ) 13 | 14 | 15 | 16 | 18 | SELECT LAST_INSERT_ID() 19 | 20 | ${sql} ( 21 | ${key} 23 | ) values ( 24 | #{item} 26 | ) 27 | 28 | 29 | 32 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/quartz/SimpleBloomFilter.java: -------------------------------------------------------------------------------- 1 | package com.unbank.quartz; 2 | 3 | import java.util.BitSet; 4 | 5 | import javax.annotation.PostConstruct; 6 | 7 | import org.springframework.stereotype.Component; 8 | 9 | @Component 10 | public class SimpleBloomFilter { 11 | private static final int DEFAULT_SIZE = 2 << 24; 12 | private static final int[] seeds = new int[] { 5, 7, 11, 13, 31, 37, 61 }; 13 | private BitSet bits = new BitSet(DEFAULT_SIZE); 14 | private SimpleHash[] func = new SimpleHash[seeds.length]; 15 | static SimpleBloomFilter filter; 16 | 17 | public SimpleBloomFilter() { 18 | for (int i = 0; i < seeds.length; i++) { 19 | func[i] = new SimpleHash(DEFAULT_SIZE, seeds[i]); 20 | } 21 | } 22 | 23 | public void add(String value) { 24 | for (SimpleHash f : func) { 25 | bits.set(f.hash(value), true); 26 | } 27 | } 28 | 29 | public boolean contains(String value) { 30 | if (value == null) { 31 | return false; 32 | } 33 | boolean ret = true; 34 | for (SimpleHash f : func) { 35 | ret = ret && bits.get(f.hash(value)); 36 | } 37 | return ret; 38 | } 39 | 40 | // 内部类,simpleHash 41 | public static class SimpleHash { 42 | private int cap; 43 | private int seed; 44 | 45 | public SimpleHash(int cap, int seed) { 46 | this.cap = cap; 47 | this.seed = seed; 48 | } 49 | 50 | public int hash(String value) { 51 | int result = 0; 52 | int len = value.length(); 53 | for (int i = 0; i < len; i++) { 54 | result = seed * result + value.charAt(i); 55 | } 56 | return (cap - 1) & result; 57 | } 58 | } 59 | 60 | public static void main(String[] args) { 61 | String value = "stone2083@yahoo.cn"; 62 | SimpleBloomFilter filter = new SimpleBloomFilter(); 63 | System.out.println(filter.contains(value)); 64 | filter.add(value); 65 | System.out.println(filter.contains(value)); 66 | } 67 | 68 | @PostConstruct 69 | public static SimpleBloomFilter getInstance() { 70 | if (filter == null) { 71 | filter = new SimpleBloomFilter(); 72 | } 73 | return filter; 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /src/main/java/com/unbank/quartz/StartWeiXinGongZhonghaoSpider.java: -------------------------------------------------------------------------------- 1 | package com.unbank.quartz; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class StartWeiXinGongZhonghaoSpider { 10 | private static Log logger = LogFactory 11 | .getLog(StartWeiXinGongZhonghaoSpider.class); 12 | 13 | @Autowired 14 | SougouSpiderByChrome sougouSpiderByChrome; 15 | 16 | public void executeInternal() { 17 | try { 18 | sougouSpiderByChrome.spider(); 19 | } catch (Exception e) { 20 | logger.info("启动采集微信公众号定时任务出错", e); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/quartz/WeixinImageFeatch.java: -------------------------------------------------------------------------------- 1 | package com.unbank.quartz; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.io.BufferedInputStream; 5 | import java.io.File; 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | import java.util.Iterator; 11 | 12 | import javax.imageio.ImageIO; 13 | import javax.imageio.ImageReadParam; 14 | import javax.imageio.ImageReader; 15 | import javax.imageio.stream.ImageInputStream; 16 | 17 | import org.apache.commons.logging.Log; 18 | import org.apache.commons.logging.LogFactory; 19 | 20 | import com.unbank.fetch.Fetcher; 21 | import com.unbank.tools.MD5; 22 | 23 | public class WeixinImageFeatch { 24 | private static Log logger = LogFactory.getLog(WeixinImageFeatch.class); 25 | 26 | public String fetchImage(String imgSrc) { 27 | InputStream is = null; 28 | String imageName = null; 29 | BufferedInputStream bis = null; 30 | ImageInputStream iis = null; 31 | String imageFormatName = ""; 32 | try { 33 | is = Fetcher.getInstance().getImage(imgSrc); 34 | bis = new BufferedInputStream(is); 35 | Date today = new Date(); 36 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); 37 | String todayStr = simpleDateFormat.format(today); 38 | // 存储图片的路径 39 | String imagePath = "//10.0.2.35/images/" + todayStr + "/"; 40 | 41 | File imageDir = new File(imagePath); 42 | if (!imageDir.exists()) { 43 | imageDir.mkdirs(); 44 | } 45 | iis = ImageIO.createImageInputStream(is); 46 | 47 | Iterator iter = ImageIO.getImageReaders(iis); 48 | if (iter.hasNext()) { 49 | ImageReader reader = iter.next(); 50 | reader.setInput(iis); 51 | imageFormatName = reader.getFormatName().toLowerCase(); 52 | imageName = imagePath 53 | + MD5.GetMD5Code(imgSrc + new Date().getTime()) + "." 54 | + imageFormatName; 55 | ImageReadParam irp = reader.getDefaultReadParam(); 56 | BufferedImage bi = reader.read(0, irp); 57 | ImageIO.write(bi, imageFormatName, new File(imageName)); 58 | } 59 | } catch (IOException e) { 60 | logger.info("保存图片失败", e); 61 | return null; 62 | } finally { 63 | if (is != null) { 64 | try { 65 | is.close(); 66 | } catch (IOException e) { 67 | logger.info("关闭输入流失败", e); 68 | } 69 | } 70 | 71 | if (bis != null) { 72 | try { 73 | bis.close(); 74 | } catch (IOException e) { 75 | logger.info("关闭数据流失败", e); 76 | } 77 | } 78 | 79 | if (iis != null) { 80 | try { 81 | iis.close(); 82 | } catch (IOException e) { 83 | // TODO Auto-generated catch block 84 | e.printStackTrace(); 85 | } 86 | } 87 | } 88 | if (imageName != null) { 89 | imageName = "http://10.0.2.35:8080/unbankImage/" 90 | + imageName.substring(12); 91 | } 92 | return imageName; 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/queue/BaseQueue.java: -------------------------------------------------------------------------------- 1 | package com.unbank.queue; 2 | 3 | import java.util.concurrent.LinkedBlockingQueue; 4 | 5 | import org.apache.commons.logging.Log; 6 | import org.apache.commons.logging.LogFactory; 7 | 8 | public class BaseQueue { 9 | public static Log logger = LogFactory.getLog(BaseQueue.class); 10 | 11 | public void put(LinkedBlockingQueue queue, Object object) { 12 | try { 13 | queue.put(object); 14 | } catch (InterruptedException e) { 15 | logger.info("", e); 16 | } 17 | } 18 | 19 | public Object take(LinkedBlockingQueue queue) { 20 | Object object = null; 21 | try { 22 | object = queue.take(); 23 | } catch (InterruptedException e) { 24 | logger.info("", e); 25 | } 26 | return object; 27 | } 28 | 29 | public void sleeping(int time) { 30 | try { 31 | Thread.sleep(time); 32 | } catch (InterruptedException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/unbank/redis/RedisClient.java: -------------------------------------------------------------------------------- 1 | package com.unbank.redis; 2 | import java.util.ArrayList; 3 | import java.util.List; 4 | 5 | import redis.clients.jedis.Jedis; 6 | import redis.clients.jedis.JedisPool; 7 | import redis.clients.jedis.JedisPoolConfig; 8 | import redis.clients.jedis.JedisShardInfo; 9 | import redis.clients.jedis.ShardedJedis; 10 | import redis.clients.jedis.ShardedJedisPool; 11 | 12 | 13 | 14 | public class RedisClient { 15 | 16 | private Jedis jedis;//非切片额客户端连接 17 | private JedisPool jedisPool;//非切片连接池 18 | private ShardedJedis shardedJedis;//切片额客户端连接 19 | private ShardedJedisPool shardedJedisPool;//切片连接池 20 | 21 | public RedisClient() 22 | { 23 | initialPool(); 24 | initialShardedPool(); 25 | shardedJedis = shardedJedisPool.getResource(); 26 | jedis = jedisPool.getResource(); 27 | 28 | 29 | } 30 | 31 | /** 32 | * 初始化非切片池 33 | */ 34 | private void initialPool() 35 | { 36 | // 池基本配置 37 | JedisPoolConfig config = new JedisPoolConfig(); 38 | // config.setMaxActive(20); 39 | config.setMaxTotal(20); 40 | config.setMaxIdle(5); 41 | // config.setMaxIdle(maxIdle); 42 | // config.setMaxWait(1000l); 43 | config.setMaxWaitMillis(5000l); 44 | config.setTestOnBorrow(false); 45 | jedisPool = new JedisPool(config,"127.0.0.1",6379); 46 | } 47 | 48 | /** 49 | * 初始化切片池 50 | */ 51 | private void initialShardedPool() 52 | { 53 | // 池基本配置 54 | JedisPoolConfig config = new JedisPoolConfig(); 55 | // config.setMaxActive(20); 56 | config.setMaxTotal(20); 57 | config.setMaxIdle(5); 58 | // config.setMaxWait(1000l); 59 | config.setMaxWaitMillis(1000l); 60 | config.setTestOnBorrow(false); 61 | // slave链接 62 | List shards = new ArrayList(); 63 | shards.add(new JedisShardInfo("127.0.0.1", 6379, "master")); 64 | 65 | // 构造池 66 | shardedJedisPool = new ShardedJedisPool(config, shards); 67 | } 68 | 69 | public void show() { 70 | // KeyOperate(); 71 | // StringOperate(); 72 | // ListOperate(); 73 | // SetOperate(); 74 | // SortedSetOperate(); 75 | // HashOperate(); 76 | // jedisPool.returnBrokenResource(jedis); 77 | jedisPool.destroy(); 78 | // jedisPool.returnResource(jedis); 79 | // shardedJedisPool.returnResource(shardedJedis); 80 | // shardedJedisPool. 81 | } 82 | 83 | 84 | } -------------------------------------------------------------------------------- /src/main/java/com/unbank/spider/weixinbigV/WeiXinGongZhongHaoPagePaser.java: -------------------------------------------------------------------------------- 1 | package com.unbank.spider.weixinbigV; 2 | 3 | import java.io.StringReader; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.Iterator; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | import net.sf.json.JSONArray; 11 | import net.sf.json.JSONObject; 12 | 13 | import org.apache.commons.lang3.StringUtils; 14 | import org.apache.commons.logging.Log; 15 | import org.apache.commons.logging.LogFactory; 16 | import org.dom4j.Document; 17 | import org.dom4j.DocumentException; 18 | import org.dom4j.Element; 19 | import org.dom4j.io.SAXReader; 20 | import org.jsoup.Jsoup; 21 | 22 | import com.unbank.redis.JedisUtil; 23 | import com.unbank.tools.MD5; 24 | 25 | public class WeiXinGongZhongHaoPagePaser { 26 | private static Log logger = LogFactory 27 | .getLog(WeiXinGongZhongHaoPagePaser.class); 28 | 29 | public JedisUtil jedisUtil; 30 | 31 | public WeiXinGongZhongHaoPagePaser(JedisUtil jedisUtil) { 32 | this.jedisUtil = jedisUtil; 33 | } 34 | 35 | public List> paserXmlPage(String html) { 36 | String temp = html.replaceAll("\\n{0,10}", ""); 37 | temp = StringUtils.substringBetween(temp, "sogou.weixin.gzhcb(", 38 | ") 80 | 81 | 82 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 94 | 95 | 96 | 97 | 98 | 99 | 01 10 16 * * ? 100 | 101 | 102 | 103 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,A1,A2,A3 2 | log4j.appender.A1=org.apache.log4j.ConsoleAppender 3 | log4j.appender.A1.Threshold=INFO 4 | log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender 5 | log4j.appender.A2.Threshold=INFO 6 | log4j.appender.A2.File=/logs/dailyLog/ENDailyRollingLog.log 7 | log4j.appender.A2.DatePattern='.'yyyy-MM-dd'.log' 8 | log4j.appender.A3=org.apache.log4j.RollingFileAppender 9 | log4j.appender.A3.Threshold=INFO 10 | log4j.appender.A3.File=/logs/ENRollingLog.log 11 | log4j.appender.A3.MaxFileSize=5MB 12 | log4j.appender.A3.MaxBackupIndex=5 13 | log4j.appender.A1.layout=org.apache.log4j.PatternLayout 14 | log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} <# %l> %-5p [%r]:%n%m%n 15 | log4j.appender.A2.layout=org.apache.log4j.PatternLayout 16 | log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} <# %l> %-5p [%r]:%n%m%n 17 | log4j.appender.A3.layout=org.apache.log4j.PatternLayout 18 | log4j.appender.A3.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} <# %l> %-5p [%r]:%n%m%n -------------------------------------------------------------------------------- /src/main/resources/mybatis.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/resources/weixingongzhonghao.txt: -------------------------------------------------------------------------------- 1 | // 中国经济网 2 | http://weixin.sogou.com/gzh?openid=oIWsFt0B7LsVbUCMpgksNY8tqIno 3 | // 农村金融时报 4 | http://weixin.sogou.com/gzh?openid=oIWsFt27OIgshxth6A653Ed6AacU 5 | // 银行家园 6 | http://weixin.sogou.com/gzh?openid=oIWsFt69qg4swOidJFRxEeC_789Q 7 | // 京北互联网金融 8 | http://weixin.sogou.com/gzh?openid=oIWsFt7ebncWck6TypHYTpjHi2sM 9 | // 胡蓉经理提供 10 | http://weixin.sogou.com/gzh?openid=oIWsFt3QUWxIbSe7VNA4ETn3ec00 11 | http://weixin.sogou.com/gzh?openid=oIWsFt3hc-CuR4SbVaFqrc_HnKXQ 12 | http://weixin.sogou.com/gzh?openid=oIWsFtwzh_jENpiFCIHGoN6Ji33s 13 | http://weixin.sogou.com/gzh?openid=oIWsFt81zdrUUzjH-SGC3IkRx6iw 14 | http://weixin.sogou.com/gzh?openid=oIWsFtyGryJyPREzXGffzyos-WdQ 15 | http://weixin.sogou.com/gzh?openid=oIWsFt1fi3gRAapqAZ_YlsGx3KMY 16 | http://weixin.sogou.com/gzh?openid=oIWsFtxL_noQnwA8o2TRHz9xradA 17 | http://weixin.sogou.com/gzh?openid=oIWsFtyeIlUSB9IaP5n5uGVloPNA 18 | http://weixin.sogou.com/gzh?openid=oIWsFt7Er833fvFpNZIx4m5_duR0 19 | http://weixin.sogou.com/gzh?openid=oIWsFt1AjNb_lcbg1Mdki0ag2rrY 20 | http://weixin.sogou.com/gzh?openid=oIWsFtzbLA-qYIWgwcAL67-Wh2VY 21 | http://weixin.sogou.com/gzh?openid=oIWsFtz7DKsBH9S8yE-eYMd_v2GI 22 | http://weixin.sogou.com/gzh?openid=oIWsFt7t_wPvArBEpWLOE3mjebxY 23 | http://weixin.sogou.com/gzh?openid=oIWsFt-XXnDE7oHMiueJfKbEhzrI 24 | http://weixin.sogou.com/gzh?openid=oIWsFtyb4-7dzlV36Twu3OH3itr8 25 | http://weixin.sogou.com/gzh?openid=oIWsFt-Z6umlCtfP53D59lLx8WUI 26 | http://weixin.sogou.com/gzh?openid=oIWsFt4gCswQGrZ-rE83Am4Iy2Xk 27 | http://weixin.sogou.com/gzh?openid=oIWsFt_cUwbglodLkLT749ZABOt4 28 | http://weixin.sogou.com/gzh?openid=oIWsFt4JERI6aVU7J1rs8Dt3KXGI 29 | http://weixin.sogou.com/gzh?openid=oIWsFt3KXdUWDSKNEyUpVWcT3rhg 30 | http://weixin.sogou.com/gzh?openid=oIWsFt89mqsRNr-HSk72nHeLEUls 31 | http://weixin.sogou.com/gzh?openid=oIWsFt05IU8ZsTxavSFZV9xWME2k 32 | http://weixin.sogou.com/gzh?openid=oIWsFtwp3kf5gF5wevv47pPr8RIE 33 | // 不常更新的 34 | http://weixin.sogou.com/gzh?openid=oIWsFt86uzsXPB3JaW9O-x8J1l9k 35 | http://weixin.sogou.com/gzh?openid=oIWsFt_l_ISH42qi_3K5WBw9PEQo 36 | http://weixin.sogou.com/gzh?openid=oIWsFt78EkVGzrFbUrS8fH_SV2B4 37 | http://weixin.sogou.com/gzh?openid=oIWsFt90GC7XFO5S7UJ6lYxMgWu0 38 | http://weixin.sogou.com/gzh?openid=oIWsFt38e1hdn7WhelIduG2JkM1s 39 | // 40 | // 第二批 41 | //天津新闻头条 42 | http://weixin.sogou.com/gzh?openid=oIWsFtwxY6x3E3y42qlId08_B4yc 43 | //天津港电子商务网 44 | http://weixin.sogou.com/gzh?openid=oIWsFtwnwycXbzUT4Q3oqdgOaHeI 45 | //天津能源化工电子商务交易中心 46 | http://weixin.sogou.com/gzh?openid=oIWsFt8bygNLsPEZfgnLOSnhLIks 47 | //天津市场营销协会 48 | http://weixin.sogou.com/gzh?openid=oIWsFt23NDrBqQwSf29-f-MffXV0 49 | //天津城市规划协会 50 | http://weixin.sogou.com/gzh?openid=oIWsFt4cHnqHWxhEydbXLLp8lsX8 51 | //天津新闻 52 | http://weixin.sogou.com/gzh?openid=oIWsFt75sRA5R3CujtqLUAB422pU 53 | //天津招商网 54 | http://weixin.sogou.com/gzh?openid=oIWsFt246QCGF8dbvroyT-hYF38g 55 | //关注天津 56 | http://weixin.sogou.com/gzh?openid=oIWsFt5__Z2AKn9s_uISsjvR86ks 57 | //天津科技 58 | http://weixin.sogou.com/gzh?openid=oIWsFt8QuUQZbeYNVNBeSzL5UfgQ 59 | //天津市金属材料行业协会 60 | http://weixin.sogou.com/gzh?openid=oIWsFt-sFm-uqYLDSZfymSviJeD8 61 | //天津市滨海新区中小企业协会 62 | http://weixin.sogou.com/gzh?openid=oIWsFt-SNjbvijU9y9-4N_dezmEQ 63 | //天津市商务服务行业协会 64 | http://weixin.sogou.com/gzh?openid=oIWsFt10tFzHPCbyhH-_n0htrr0A 65 | //天津保税空港企业协会 66 | http://weixin.sogou.com/gzh?openid=oIWsFtxWpgr6H7gVlQKwuQ7ziR5c 67 | //天津市百货商业协会 68 | http://weixin.sogou.com/gzh?openid=oIWsFt3mCjWEGVFbj2JCz_JLF1hA 69 | //天津市高新技术企业协会 70 | http://weixin.sogou.com/gzh?openid=oIWsFtz5zL_hOTOj8K27VchWalIA 71 | //京津冀一体化 72 | http://weixin.sogou.com/gzh?openid=oIWsFt2Gbmd-J3U5Qs18uJ1wZlLg 73 | //微京津冀 74 | http://weixin.sogou.com/gzh?openid=oIWsFtzZU1XgiYItuwzpmMrA0I0E 75 | //生态京津冀 76 | http://weixin.sogou.com/gzh?openid=oIWsFtwSlPYlg8a1Q1Pdce84Q0R4 77 | //京津冀生活通 78 | http://weixin.sogou.com/gzh?openid=oIWsFtzZhxxCUBHMaoxmtWxXRLi8 79 | //京津冀商务信息中心 80 | http://weixin.sogou.com/gzh?openid=oIWsFtwsl7iXbFD42BcJ4Hx6Uyvg 81 | //京津冀一体化 82 | http://weixin.sogou.com/gzh?openid=oIWsFtwzk98pPtXH5-plfi5HoRpI 83 | //京津冀招商网 84 | http://weixin.sogou.com/gzh?openid=oIWsFt-zdOaiLpqwEL_t-AJ4c3ds 85 | //京津冀一体化权威资讯 86 | http://weixin.sogou.com/gzh?openid=oIWsFt_7Ogp47kLvv5WWh_J6_1Eg 87 | //聚焦京津冀 88 | http://weixin.sogou.com/gzh?openid=oIWsFtxPVBfUJdyflRun_A4d2Qbs 89 | //京津冀发展智库 90 | http://weixin.sogou.com/gzh?openid=oIWsFt5uS6G1FiQloUfSid_4Hkj4 91 | //京津冀 92 | http://weixin.sogou.com/gzh?openid=oIWsFt8tqHZ5k3FGtdcFqhu9iATE 93 | //京津冀招商 94 | http://weixin.sogou.com/gzh?openid=oIWsFt9CeiwXBvMD0TNvK0u6MkIA 95 | //京津冀投资 96 | http://weixin.sogou.com/gzh?openid=oIWsFtzTSfN0urxhXjD8I2PVJ4xA 97 | //京津冀企业联盟 98 | http://weixin.sogou.com/gzh?openid=oIWsFt7QlBGGZI8QjZnNJn-G6G90 99 | //掌上京津冀 100 | http://weixin.sogou.com/gzh?openid=oIWsFt4Gelu634i0xxqA84Q50-eo -------------------------------------------------------------------------------- /src/test/java/com/unbank/test/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.unbank.test; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/unbank/test/CipherMessage.java: -------------------------------------------------------------------------------- 1 | package com.unbank.test; 2 | 3 | import java.security.Key; 4 | 5 | import javax.crypto.Cipher; 6 | import javax.crypto.KeyGenerator; 7 | 8 | public class CipherMessage { 9 | private String algorithm; // 算法,如DES 10 | private Key key; // 根据算法对应的密钥 11 | private String plainText; // 明文 12 | 13 | KeyGenerator keyGenerator; 14 | Cipher cipher; 15 | 16 | // 函数进行初始化 17 | CipherMessage(String alg, String msg) { 18 | algorithm = alg; 19 | plainText = msg; 20 | } 21 | 22 | // 加密函数,将原文加密成密文 23 | public byte[] CipherMsg() { 24 | byte[] cipherText = null; 25 | 26 | try { 27 | // 生成Cipher对象 28 | cipher = Cipher.getInstance(algorithm); 29 | // 用密钥加密明文(plainText),生成密文(cipherText) 30 | cipher.init(Cipher.ENCRYPT_MODE, key); // 操作模式为加密(Cipher.ENCRYPT_MODE),key为密钥 31 | cipherText = cipher.doFinal(plainText.getBytes()); // 得到加密后的字节数组 32 | // String str = new String(cipherText); 33 | } catch (Exception e) { 34 | e.printStackTrace(); 35 | } 36 | return cipherText; 37 | } 38 | 39 | // 解密函数,将密文解密回原文 40 | public String EncipherMsg(byte[] cipherText, Key k) { 41 | byte[] sourceText = null; 42 | 43 | try { 44 | cipher.init(Cipher.DECRYPT_MODE, k); // 操作模式为解密,key为密钥 45 | sourceText = cipher.doFinal(cipherText); 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | } 49 | return new String(sourceText); 50 | 51 | } 52 | 53 | // 生成密钥 54 | public Key initKey() { 55 | try { 56 | // 初始化密钥key 57 | keyGenerator = KeyGenerator.getInstance(algorithm); 58 | keyGenerator.init(56); // 选择DES算法,密钥长度必须为56位 59 | key = keyGenerator.generateKey(); // 生成密钥 60 | } catch (Exception ex) { 61 | ex.printStackTrace(); 62 | } 63 | return key; 64 | } 65 | 66 | // 获取Key类型的密钥 67 | public Key getKey() { 68 | return key; 69 | } 70 | 71 | // 获取Key类型的密钥 72 | public Key getKey(byte[] k) { 73 | try { 74 | key = cipher.unwrap(k, algorithm, Cipher.DECRYPT_MODE); 75 | } catch (Exception ex) { 76 | ex.printStackTrace(); 77 | } 78 | return key; 79 | } 80 | 81 | // 获取密钥包装成byte[]类型的 82 | public byte[] getBinaryKey(Key k) { 83 | byte[] bk = null; 84 | try { 85 | bk = cipher.wrap(k); 86 | } catch (Exception ex) { 87 | ex.printStackTrace(); 88 | } 89 | return bk; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/com/unbank/test/WeixinSpiderDemo.java: -------------------------------------------------------------------------------- 1 | package com.unbank.test; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.InputStream; 5 | import java.util.Date; 6 | import java.util.List; 7 | import java.util.regex.Matcher; 8 | import java.util.regex.Pattern; 9 | 10 | import org.apache.commons.codec.binary.Base64; 11 | import org.apache.commons.logging.Log; 12 | import org.apache.commons.logging.LogFactory; 13 | import org.apache.http.Header; 14 | import org.apache.http.client.config.RequestConfig; 15 | import org.apache.http.client.methods.CloseableHttpResponse; 16 | import org.apache.http.client.methods.HttpGet; 17 | import org.apache.http.client.protocol.HttpClientContext; 18 | import org.apache.http.cookie.Cookie; 19 | import org.apache.http.impl.client.BasicCookieStore; 20 | import org.apache.http.impl.client.CloseableHttpClient; 21 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 22 | 23 | import com.unbank.fetch.HttpClientBuilder; 24 | 25 | public class WeixinSpiderDemo { 26 | 27 | private static RequestConfig requestConfig = RequestConfig.custom() 28 | .setSocketTimeout(30000).setConnectTimeout(30000) 29 | .setCircularRedirectsAllowed(true).setMaxRedirects(50).build(); 30 | private static Log logger = LogFactory.getLog(WeixinSpiderDemo.class); 31 | private static BasicCookieStore cookieStore = new BasicCookieStore(); 32 | 33 | public static void main(String[] args) { 34 | for (int i=0;i<100;i++) { 35 | System.out.println(Math.pow(2, 1)); 36 | } 37 | 38 | 39 | } 40 | public static byte[] decode(final byte[] bytes) { 41 | return Base64.decodeBase64(bytes); 42 | } 43 | 44 | /** 45 | * 二进制数据编码为BASE64字符串 46 | * 47 | * @param bytes 48 | * @return 49 | * @throws Exception 50 | */ 51 | public static String encode(final byte[] bytes) { 52 | return new String(Base64.encodeBase64(bytes)); 53 | } 54 | 55 | 56 | 57 | 58 | 59 | 60 | } 61 | --------------------------------------------------------------------------------