分類
程序

把媒體中地理位置標籤從 WGS-84 轉換成 GCJ-02

手機拍攝的照片地理位置默認是 WGS-84 系統,但是谷歌地圖使用的卻是 GCJ-02 系統,這就會出現偏移。爲了展示正確的位置,可以通過把照片的地理位置標籤轉換成 GCJ-02 來實現。需要用到 Phil Harvey開發的 ExifTool 和 Avatar sshuair 開發的 coord-convert。前者下載exiftool.exe 到本地即可,後者可以通過 pip 安裝。

# -*- coding: utf-8 -*-

#pip install coord-convert

#Usage:
#Drage a single photo or directory with photos to this script
# OR
#python wgs2gcj.py D:\my.JPG
#Results will be wrote to gps.log at the same dir with this script

from coord_convert.transform import wgs2gcj
import subprocess,json,sys,os,time,datetime
#You may set your exiftool file here
exiftool_path = "D:\\Program Files\\portable\\exiftool.exe"

work_dir = os.getcwd()
def log42(logFile,logText):
	ts = int(time.time())
	dt = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
	
	log_file = os.path.join(work_dir,logFile+".log")
	with open(log_file, "a") as myfile:
		myfile.write(dt+" "+logText+"\n")
		print(logText)
		
		
if (exiftool_path == ""):
	exiftool_path = os.path.join(work_dir,'exiftool.exe')
if not os.path.exists(exiftool_path):
	log42("gps","Error: exiftool.exe not found neither in script directory nor in the script setting \"exiftool_path\"")
	exit()
	
file_paths_input = sys.argv[1:]
file_paths = []

for file_path_input in file_paths_input:
	if os.path.isfile(file_path_input):
		file_paths.append(file_path_input)
	elif os.path.isdir(file_path_input):
		for root,ds,fs in os.walk(file_path_input):
			for ff in fs:
				full_path = os.path.join(root,ff)
				file_paths.append(full_path)

for f in file_paths:
	output = subprocess.check_output('"'+exiftool_path+'" -struct -j -a -n "-gps*" '+f, shell=True)
	data = json.loads(output)
	if 'GPSMapDatum' in data[0]:
		log42("gps",data[0]['SourceFile']+':'+str(data[0]['GPSMapDatum'])+' '+str(data[0]['GPSLatitude'])+' '+str(data[0]['GPSLongitude']))
		if(data[0]['GPSMapDatum']=='WGS-84'):
			gcj_lon, gcj_lat = wgs2gcj(data[0]['GPSLongitude'], data[0]['GPSLatitude'])
			log42("gps",str(gcj_lat)+' '+str(gcj_lon))
			if (gcj_lat==data[0]['GPSLatitude']):
				log42("gps",'not in China')
			else:
				output = subprocess.check_output('"'+exiftool_path+'" -overwrite_original -exif:gpsmapdatum=GCJ-02 -exif:gpslatitude='+str(gcj_lat)+' -exif:gpslongitude='+str(gcj_lon)+' '+f, shell=True)
				log42("gps",output.decode())
		else:			
			log42("gps","not a wgs84 location")
	else:
		log42("gps","no gps data found")
		
log42("gps","task finished")

另請參考:使用ExifTool處理文件元數據wgs-84-gcj-02-經緯度在線轉換

分類
程序

PHP 使用 Nominatim 的逆地理編碼

之前一直使用谷歌的逆地理編碼服務,多年來運行良好。但是谷歌畢竟是一家巨大的商業公司,所以我現在改用 Nominatim

Nominatim(來自拉丁語,“按名稱”)是一個通過名稱和地址搜索開放街圖數據,根據開放街圖點來生成合成地址(逆地理編碼)的工具。
<?php
header("Access-Control-Allow-Origin: *");
header('Content-type: text/json');
header("Cache-Control: no-cache, must-revalidate");

$lat=$_POST["lat"];
$lng=$_POST["lng"];
$lang=$_POST["lang"];

$url = "https://nominatim.openstreetmap.org/reverse?format=geojson&lat=".$lat."&lon=".$lng."&accept-language=".$lang;

$ch = curl_init(); 
//set your own agent name
$agent = 'location/0.2(ft.shaman.eu.org)';
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,20); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$content = curl_exec($ch); 
curl_close($ch);

$content = json_decode($content);
$resultArray = array();
foreach($content->features[0]->properties->address as $key => $value) {
    //remove country_code and postcode
    if ($key=='country_code' or $key=='postcode'){            
    }else{
        array_push($resultArray,$value);
    }
}
echo implode(",",$resultArray);
?>	
curl -d 'lat=22.57776&lng=113.94849&lang=zh-TW' https://your.api.domain/api.php
西丽华昌大厦,西丽南路,松坪村,南山区,西丽街道,广东省,中国
分類
程序

使用python同步本地時間

服務器超售到不行,cpu時間隔一段時間就滿好多。嘗試配置了標準了自動的時間同步,效果不理想。於是用python調用worldtimeapi.org的api配合crontab來修正系統時間。

import subprocess,requests,time

def main():
    try:
        response = requests.request("GET", "http://worldtimeapi.org/api/timezone/Asia/Hong_Kong")
        timeJson=response.json()
        timeAbs = abs(timeJson['unixtime']-time.time())
        if timeAbs > 15 :
            subprocess.call(['date','+%T','-s',timeJson['datetime'][11:19]])
            print("time synced")
        else:
            print(timeAbs)
            
    except:
        print("net error")
    
if __name__ == '__main__':
    main()
#添加crontab任務
crontab -e
#每兩小時檢查一次
0 */2 * * * /usr/local/bin/python3 /home/42/time.py >> /home/42/t.log 2>&1
分類
程序

python使用pyftpdlib實現ftp服務

需要使用ftp分享文件,記得很久之前折騰過ftp服務的搭建,賬號權限設置都挺複雜,這次使用python和pyftpdlib實現,幾分鐘就搞定啦。

#安裝pyftpdlib,當然還是推薦在虛擬環境裡裝
pip install pyftpdlib

然後新建一個python文件myFtp.py,內容如下:

import os

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

def main():
    # Instantiate a dummy authorizer for managing 'virtual' users
    authorizer = DummyAuthorizer()

    # 第一行新建一個具有讀寫權限的用戶(用戶名,密碼,ftp文件夾)
    # 第二行新建匿名讀取賬戶
    authorizer.add_user('user', '12345', '.', perm='elradfmwMT')
    authorizer.add_anonymous(os.getcwd())

    # Instantiate FTP handler class
    handler = FTPHandler
    handler.authorizer = authorizer

    # 設置一個個性化歡迎語 (客戶端來連接的時候會返回給它)
    handler.banner = "基於pyftpdlib的ftp服務已準備好"

    # Specify a masquerade address and the range of ports to use for
    # passive connections.  Decomment in case you're behind a NAT.
    #handler.masquerade_address = '151.25.42.11'
    #handler.passive_ports = range(60000, 65535)

    # 監聽任意ip目標的2121端口,ftp標準端口是21
    address = ('', 2121)
    server = FTPServer(address, handler)

    # set a limit for connections
    server.max_cons = 256
    server.max_cons_per_ip = 5

    # start ftp server
    server.serve_forever()

if __name__ == '__main__':
    main()
#最後運行就可以了
python myFtp.py

其實這次也是走了彎路的,那就是先嘗試了Twisted,stackoverflow的網友說可以一行命令實現ftp服務。結果pip裝不上,需要下載了安裝包用

pip install 
裝。裝好後匿名用戶可以跑,設置了密碼就顯示密碼錯誤,搜索一番也沒找到辦法,於是使用了pyftpdlib。雖然實現ftp服務失敗了,但Twisted確實是一個很厲害的項目,有時間的話可以學習一下。 Twisted also supports many common network protocols, including SMTP, POP3, IMAP, SSHv2, and DNS.

分類
程序

幹掉QQProtect.exe

本文使用python3搭配windows的定時任務來實現正常使用TIM而不運行QQProtect.exe的功能。之所以沒有用bat是因為bat會彈出黑色命令行窗口,而使用pythonw則不會彈窗,體驗較好。方法參考自知乎如何禁用QPCore service启动项?

#file:killQQP.pyw
#杀死QQProtect.exe:killQQP.pyw 1
#启动TIM时杀死QQProtect.exe:killQQP.pyw 2

import os,time,sys

def killProgress(targetProgress):
    if targetProgress in os.popen('tasklist /FI \"IMAGENAME eq '+targetProgress+'\"').read():
        os.system('TASKKILL /F /IM '+targetProgress)

progressToBeKill = "QQProtect.exe"
progressToBeLaunch = "TIM.exe"
targetProgressFile = 'E:\\\"Program Files (x86)\"\\Tencent\\TIM\Bin\\QQScLauncher.exe' 
  
if str(sys.argv[1]) == "1":
    killProgress(progressToBeKill)
elif str(sys.argv[1]) == "2":
    count = 0
    time.sleep(5)
    os.system(targetProgressFile)
    time.sleep(2)
    while count < 5:
        killProgress(progressToBeKill)
        time.sleep(10)
        count = count + 1
        
else:
    killProgress(progressToBeKill)

然後在開始菜單運行taskschd.msc,新建定時任務。第一個定時任務:常規選項卡勾選“使用最高權限運行”;觸發器選項卡選擇“登錄時”;操作選項卡,程序或腳本填pythonw的位置,比如我的是C:\Users\42\AppData\Local\Programs\Python\Python35\pythonw.exe,添加參數則填寫上面腳本所在位置加參數,如E:\code\killQQP.pyw 1,保存。第二個定時任務:常規選項卡勾選“使用最高權限運行”;觸發器選項卡選擇“發生事件時”,然後日誌選“應用程序”,源填入QPCore,時間ID填入0;操作選項卡,程序或腳本填pythonw的位置,我的還是C:\Users\42\AppData\Local\Programs\Python\Python35\pythonw.exe,添加參數填寫腳本所在位置加參數,如E:\code\killQQP.pyw 2,保存即可。QQ用戶需要修改targetProgressFile為QQ的啟動文件,可以通過在QQ啟動快捷方式上右鍵查看詳情取得。

附:啟動TIM時幹掉QQProtect.exe的定時任務的導出文件killQQP2.xml。

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2018-11-05T11:11:00.0447223</Date>
    <Author>42-PC\42</Author>
  </RegistrationInfo>
  <Triggers>
    <EventTrigger>
      <Enabled>true</Enabled>
      <Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Application"&gt;&lt;Select Path="Application"&gt;*[System[Provider[@Name='QPCore'] and EventID=0]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
    </EventTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>42-PC\42</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Users\42\AppData\Local\Programs\Python\Python35\pythonw.exe</Command>
      <Arguments>E:\code\killQQP.pyw 2</Arguments>
    </Exec>
  </Actions>
</Task>

本文更新於 2018/11/16。

分類
程序

python查詢aqicn的空氣質量數據

#!/data/data/com.termux/files/usr/bin/python
# -*- coding: utf8 -*-

#aqicn空氣質量 #https://aqicn.org/api/
#v2 同時指定多個監測站,已防止單個站點很久不更新數據
import json,requests,sys,time

#token從https://aqicn.org/data-platform/token/獲取,只需郵箱即可
token = 'YourToken'
#預設為當前IP位置,可自行指定基站,幫助 --help
stationList = ["@5851","@5855","@5860"]

def getAqi(station):    
    aqiUrl = "https://api.waqi.info/feed/"+station+"/?token="+token
    try:
        res = requests.get(aqiUrl)
        a = res.json()
        if a['status']=="ok":            
            if time.time() - a['data']['time']['v'] < 86400:
                return a
            else:
                return {"status":a['data']['city']['name']+"的數據已過期。"}
        else:
            res = requests.get(aqiUrl)
            a = res.json()
            return a
    except:
        return {"status":"net error"}

def searchStation(keyword):
    searchUrl = "https://api.waqi.info/search/?token="+token+"&keyword="+keyword
    try:
        res = requests.get(searchUrl)
        a = res.json()
        res = []
        if a['status']=="ok":
            for s in a['data']:
                resT={}
                resT['uid']=s['uid']
                resT['name']=s['station']['name']
                resT['aqi']=s['aqi']+" ("+s['time']['stime']+")"
                res.append(resT)
            return res
        else:
            return a
    except:
        return {"status":"net error"}
def processAqi(aqiJson):
    s=aqiJson
#     print(json.dumps(s, indent=4))
    res={}
    if s['status']=="ok":
        res['cityName']=s['data']['city']['name']
        res['time']=s['data']['time']['s']
        res['aqi']=s['data']['aqi']
        try:
            res['pm25']=s['data']['iaqi']['pm25']['v']
        except:
            res['pm25']="N/A"
        try:
            res['pm10']=s['data']['iaqi']['pm10']['v']
        except:
            res['pm10']="N/A"
        try:
            res['temp']=s['data']['iaqi']['t']['v']
        except:
            res['temp']="N/A"
        try:
            res['humidity']=s['data']['iaqi']['h']['v']
        except:
            res['humidity']="N/A"
        try:
            res['wind']=s['data']['iaqi']['w']['v']
        except:
            res['wind']="N/A"
    else:
        res=s
    return res

def main():
    global station
    if len(sys.argv)==1:
        for station in stationList:
            res = processAqi(getAqi(station))
            print(json.dumps(res, indent=2, ensure_ascii=False))
    elif len(sys.argv)==2:
        station = str(sys.argv[1])
        if station == "--help":
            res={"@5851":"根據觀測點編號查詢","Nanyang":"根據城市名稱查詢",
                 "here":"根據IP查詢","s shenzhen":"查詢深圳觀測點",}
        else:
            res = processAqi(getAqi(station))
        print(json.dumps(res, indent=2, ensure_ascii=False))
    elif len(sys.argv)==3:
        res = searchStation(str(sys.argv[2]))
        print(json.dumps(res, indent=2, ensure_ascii=False))
    
if __name__ == '__main__':
    main()

前天迎來如秋後的第一場中度霧霾。

本文更新於 2018/11/08。

分類
程序

python守護tomcat

下面腳本實現定時檢測tomcat是否還活着,如果死了就啟動它,如果活太久了(24小時)就殺掉再啟動。

#!/data/pythons/p35/bin/python
# -*- coding: utf8 -*-

#定時任務 /etc/crontab
#*/5 * * * * root (/data/pythons/p36/bin/python /data/pythons/scripts/serviceDeamon.py tomcat-8780)

import psutil,time,datetime,subprocess,sys

def log42(logFile,logText):
    ts = int(time.time())
    dt = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    with open("/tmp/"+dt[0:10]+logFile+".log", "a") as myfile:
        myfile.write(dt+" "+logText+"\n")
        
def startByCmdline(cmdlineKeyword): 
    command = 'export JAVA_HOME=/usr/local/java/jdk1.7.0_71;/bin/sh /data/web/'+cmdlineKeyword+'/bin/startup.sh'
    status,output = subprocess.getstatusoutput(command)
    log42('service'+cmdlineKeyword,"started"+ str(status))
    
def killByCmdline(cmdlineKeyword):    
    alive = False
    for proc in psutil.process_iter():
        try:
            pinfo = proc.as_dict(attrs=['pid', 'name','cmdline','create_time','ppid'])
        except psutil.NoSuchProcess:
            pass
        else:
            if any('/'+cmdlineKeyword in s for s in pinfo['cmdline']):
                alive = True
                aliveTime = time.time()-pinfo['create_time']
                if aliveTime > 60*60*24:        
                    log42('service'+cmdlineKeyword,"old enough "+str(aliveTime/60))
                    p = psutil.Process(pinfo['pid'])
                    p.kill()
                    time.sleep(3)
                    startByCmdline(cmdlineKeyword)
                else:
                    log42('service'+cmdlineKeyword,"too young "+str(aliveTime/60))
            else:
                pass
            
    if not alive :
        log42('service'+cmdlineKeyword,"died")
        startByCmdline(cmdlineKeyword)
                
if __name__ == '__main__':
    #'tomcat-8780'
    if str(sys.argv[1]).__contains__('tomcat') :
        killByCmdline(str(sys.argv[1]))
    else:
        log42('service',"wrong param")
        print('param like:tomcat-8780')