FlagCounter blocked? Own realization of a simple multi-national visitors counter

Due

    Some time ago found FlagCounter counter to the right of the blog suddenly gone, and saw a garden blog blocked FlagCounter news, a bit puzzled. So on FlagCounter I watched a website, found the recent visit from the new countries actually from Taiwan. After another round Baidu, see bloggers issued a statement saying because the state refused to use FlagCounter the position. So I quickly emptied the bulletin board, but unfortunately there were no alternatives to think simply write one yourself.
FlagCounter display Country: Taiwan

FlagCounter will show Taiwan as a country
Display contrast (left: FlagCounter Right: homemade)

Front display is HTML, image formats than FlagCounter much clearer.

Development environment and the online environment

  • SpringBoot 2.1.8
  • Redis 5.0.x
  • Domain name + SSL certificate (blog Park requires https)

Use to interface and open source data

  • www.taobao.com/help/getip.php used to obtain an IP address
  • http://ip-api.com/json/ used to resolve IP location (only HTTP, so the request needs to be transmitted through the rear end)
  • https://github.com/mukeshsolanki/country-picker-android flag Source

Development of ideas

  Taobao interface to obtain an IP address visitors at the front end, back-end pass through user access. Backend requests country code to parse the ip address ip-api, and recorded Redis database. Meanwhile the visitor's request to the back-end data and displayed on the page.

  In fact, looking for country codes data, we found there are two standards: one is the ISO-3166 standard, there is a GB / T 2659-2000. The difference is that the Hong Kong, Taiwan and other regions labeled HK, TW, which are directly labeled as CN. FlagCounter may just be the wave of the standard pit, all the regions of the country code marked as national treatment.

Key Code

package com.qf;  
  
import com.alibaba.fastjson.JSON;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.data.redis.core.StringRedisTemplate;  
import org.springframework.data.redis.core.ZSetOperations;  
import org.springframework.transaction.annotation.Transactional;  
import org.springframework.web.bind.annotation.CrossOrigin;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
importorg.springframework.web.client.RestTemplate;   
  
Import the java.util.HashMap;  
 Import java.util.Set;   
  
@CrossOrigin   
@RestController   
@Transactional   
public  class the Controller {   
  
    @Autowired   
    Private StringRedisTemplate redisTemplate;   
  
    Private RestTemplate RestTemplate = new new RestTemplate ();   
  
    / / interface hides   
    @ RequestMapping ( "xxxxxx" )  
     public String getVisitor () {  
         // remove ranking country and total (total) top 20, so that a total of 21 0-20  
        Set<ZSetOperations.TypedTuple<String>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeWithScores("visitor", 0, 20);  
        return JSON.toJSON(typedTupleSet).toString();  
    }  
  
    // 接口隐藏  
    @RequestMapping("xxxxxx")  
    public void addVisitor(@PathVariable String ip){  
        // 请求ip-api接口,获取ip所属地信息  
        IPaddr iPaddr = restTemplate.getForObject("http://ip-api.com/json/"+ip, IPaddr.class, new HashMap<>());  
        String code = iPaddr.getCountryCode();  
        convert GB / T 2659-2000 standard  //
        if (code.equals("HK") || code.equals("TW") || code.equals("MO")) {  
            code = "CN";  
        }  
        // 更新Redis中的记录  
        redisTemplate.opsForZSet().incrementScore("visitor", code, 1);  
        redisTemplate.opsForZSet().incrementScore("visitor", "total", 1);  
    }  
}  

 

Guess you like

Origin www.cnblogs.com/SaltyFishQF/p/11600499.html