hexo-seo-submit
Version:
Manual or automatic submit your site to search engines(google, bing, baidu). There are Github actions and coding jenkins available for CI
8 lines • 4.66 kB
JavaScript
import {program}from'commander';import L from'node:fs';import O from'node:path';import p from'chalk';import {fileURLToPath}from'node:url';import T from'node:fs/promises';import'wildcard-match';import G from'axios';import {HttpsProxyAgent}from'https-proxy-agent';import {GoogleAuth}from'google-auth-library';var h="hexo-seo-submit";var l={baidu:"http://data.zz.baidu.com/urls",bing:"https://ssl.bing.com/webmaster/api.svc/pox/SubmitUrlBatch",google:"https://indexing.googleapis.com/batch"},d=`
`;var y=async o=>await T.readFile(O.join(process.cwd(),o),"utf8");var k=2e4,w=G.create({timeout:k,paramsSerializer:{}}),_=w.request,u=_;var S=(o,r)=>{let{site:e,...t}=r;return u({url:`${l.baidu}?site=${e}`,data:o,method:"post",params:t,headers:{"Content-type":"text/plain"}})};var E=(o,r)=>{let{apiKey:e,siteUrl:t}=r;return u({url:l.bing,method:"post",params:{apiKey:e},data:{urlList:o,siteUrl:t},headers:{"Content-Type":"application/json; charset=utf-8"}})};var B=async o=>await(await new GoogleAuth({credentials:{client_email:o.client_email,private_key:o.private_key?.replace(/(\\|\\\\)n/g,`
`)},scopes:["https://www.googleapis.com/auth/indexing"]}).getClient()).getAccessToken(),b=async o=>{let{urlList:r,secretsConfig:e,proxy:t}=o,n={};if(t){let m=new HttpsProxyAgent(t);n.proxy=false,n.httpsAgent=m,process.env.HTTPS_PROXY=t,process.env.HTTP_PROXY=t;}let i=await B(e),c=`===============${Math.round(Math.random()*2e5)}==`,a=r.map(m=>({body:JSON.stringify({url:m,type:"URL_UPDATED"})})),s=`
`,f=a.map(m=>`\r
--${c}${s}Content-Type: application/http ${s}Content-Transfer-Encoding: binary ${s}\r
POST /v3/urlNotifications:publish ${s}Content-Type: application/json ${s}accept: application/json ${s}Content-Length:${m.body.length}${s}\r
${m.body}`).join("");return u({method:"post",url:l.google,headers:{Authorization:`Bearer ${i.token}`,"Content-Type":`multipart/mixed; boundary=${c}`},data:f,...n})};var C=o=>new Promise((r,e)=>{if(!o){e(new Error("no config here"));return}let{file:t,accountKeysJSon:n,accountKeysJSonFile:i}=o;if(!t){e(new Error("Where are files?"));return}if(!n&&!i){e(new Error("Where are accountKeys?"));return}r("done");});var D=async o=>{try{await C(o);}catch(f){return Promise.reject(f.message)}let{file:r,accountKeysJSon:e,accountKeysJSonFile:t,proxy:n}=o,i=e;t&&(i=await T.readFile(O.join(process.cwd(),t),"utf8"));let c=JSON.parse(i||""),a=await T.readFile(r,"utf8");if(!a)return Promise.reject(new Error("no url to submit"));let s=a.split(d);return b({urlList:s,secretsConfig:c,proxy:n})},P=D;var K=fileURLToPath(new URL("../../",import.meta.url)),H=L.readFileSync(O.join(K,"package.json"),"utf8"),I=JSON.parse(H||"");program.name(h).description("push url to search engine").version(I.version);program.command("baidu").argument("[]").requiredOption("-t, --token <token>","push token.").requiredOption("-f, --file <file>","urls file path.").requiredOption("-s, --site <site>","blog site. Eg: https://ksh7.com").action(async(o,r)=>{try{let{token:e,file:t,site:n}=r,i=await y(t),c=await S(i,{token:e,site:n});console.log(p.bgGreen(`success ${c?.data?.success}; ${c?.data?.remain} remain`));}catch(e){console.error(p.red(e.response?.data?.message||e.message)),process.exit(1);}});program.command("bing").argument("[]").requiredOption("-k, --key <key>","push api key.").requiredOption("-f, --file <file>","url json file path.").action(async(o,r)=>{let{key:e,file:t}=r;try{let n=await y(t),i=JSON.parse(n||"");await E(i?.urlList,{siteUrl:i?.siteUrl,apiKey:e}),console.log(p.bgGreen("bing: push success"));}catch(n){console.error(p.red(`bing error: ${n?.response?.data||n.message}`)),process.exit(1);}});program.command("google").argument("[]").requiredOption("-f, --file <file>","url file path.").option("-p, --proxy <proxy>","https proxy url. Eg: http[s]://127.0.0.1:7890").option("-m, --client_email <client_email>","accountKeysJSon.client_email").option("-k, --private_key <private_key>","accountKeysJSon.private_key").option("-a, --accountKeysJSonFile <accountKeysJSonFile>","accountKeysJSonFile path").action(async(o,r)=>{let{accountKeysJSonFile:e,client_email:t,private_key:n,proxy:i,file:c}=r;if(!e&&!(t&&n)){console.error("google error: miss credentials"),process.exit(1);return}try{let a={};t&&n?a.accountKeysJSon=JSON.stringify({client_email:t,private_key:n}):a.accountKeysJSonFile=e;let s=await P({file:O.join(process.cwd(),c),...a,proxy:i});s?.data?.includes(200)?console.log(p.bgGreen("google: push success")):(console.error(p.red(`google failed ${s?.data}`)),process.exit(1));}catch(a){console.log(p.red(`google error: ${a?.response?.data.error.message||a.message}`)),process.exit(1);}});program.parse();