steem自动投票开发-1.获取用户的投票能量
首先steem和hive一样,点赞是需要能量的,没有能量就不能点赞了,对吧。好,那么第一步就要获取我有多少能量,可以通过这个链接查询:https://steemd.com/@polkallen
@符号后面的就是要查询的用户,这都是公开的,谁有钱谁没钱都能查询
上面图片显示的就是我的能力,投票看的就是voting power,所以现在要读取这个数值。读取这个值需要解析html,就下图圈起来的地方。
那么解析html肯定有开源的框架,我找了一个java适用的:
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.14.3</version> </dependency>
接下来就是要请求https://steemd.com/@polkallen 这个接口,随便什么语言各自实现就是了,这里要注意的是,大陆需要代理~
public static String getProxy(String url, Map<String,String> heads) { String result = null; Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory> create() .register("http", new MyConnectionSocketFactory()) .register("https", new MySSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver()); CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); HttpResponse httpResponse = null; InetSocketAddress socksaddr = new InetSocketAddress("127.0.0.1", 10808); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); HttpGet request = new HttpGet(url); if(heads!=null){ Set<String> keySet=heads.keySet(); for(String s:keySet){ request.addHeader(s,heads.get(s)); } } //System.out.println("Executing request " + request + " via SOCKS proxy " + socksaddr); try { httpResponse = httpclient.execute(request, context); HttpEntity httpEntity=httpResponse.getEntity(); if(httpEntity!=null){ result= EntityUtils.toString(httpEntity,"utf-8"); } } catch (IOException e) { e.printStackTrace(); } return result; }
上面方法返回的内容就是整个html的内容,接下来就是从html字符串里找到我们需要的内容。直接贴代码了,jsoup这个解析框架可以通过css样式找到html标签,很好用。
/** * 获取用户的能量 */ public HivePowerDto getUserPower(String userName){ HivePowerDto result = new HivePowerDto(); String response = null; if(this.isProx){ response = HttpProxyUtil.getProxy(this.userPowerUrl + "/@" + userName, this.initPageHeader()); }else{ response = HttpUtil.get(this.userPowerUrl + "/@" + userName, this.initPageHeader()); } //System.out.println("获取用户能量返回:" + response); if(StringUtils.isNotEmpty(response)){ Document doc = Jsoup.parse(response); //获取形如<div class="result_info ...">something</div>的第一个div元素,class^=result_info表示class以result_info开头,^是正则表达式的开始 Elements select = doc.select("div[class^=progress-bar progress-bar-striped]"); String text1 = select.first().attr("style"); String text2 = select.get(1).attr("style"); String text3 = select.last().attr("style"); if(StringUtils.isNotEmpty(text1)){ BigDecimal votingPower = new BigDecimal(text1.replaceAll("%;", "").replaceAll("width:", "").trim()); result.setVotingPower(votingPower); } if(StringUtils.isNotEmpty(text2)){ BigDecimal downvotePower = new BigDecimal(text2.replaceAll("%;", "").replaceAll("width:", "").trim()); result.setDownvotePower(downvotePower); } if(StringUtils.isNotEmpty(text3)){ BigDecimal resourceCredits = new BigDecimal(text3.replaceAll("%;", "").replaceAll("width:", "").trim()); result.setResourceCredits(resourceCredits); } } return result; }
最后看一下效果:
获取了投票能量,就可以根据当前的能量值来决定是否需要投票,比如我自己设定小于90就不再投票。