公司要求每月發(fā)彩漫卫旱,發(fā)的條數(shù)還要達(dá)到一定數(shù)量。但你知道做為程序員坐昙,沒有那么多的可以發(fā)送對(duì)象,所以只能生成隨機(jī)號(hào)碼芋忿。
生成的號(hào)碼有4點(diǎn)要求:
- 不能發(fā)給公司人員
- 不能發(fā)給用戶
- 每個(gè)號(hào)碼只會(huì)收到一次
- 移動(dòng)號(hào)碼
公司人員是有通信錄的炸客,其中包含號(hào)碼信息襟士,但導(dǎo)出的是xls格式的通信錄文件。
自己不會(huì)網(wǎng)頁自動(dòng)化嚷量,導(dǎo)出xls文件只能手動(dòng)完成陋桂,而且只能一個(gè)部門一個(gè)部門的導(dǎo)出,部門又多蝶溶,真是體力活啊嗜历。
每個(gè)月都有新人入職或離職,為了保持最新抖所,每個(gè)月都重新導(dǎo)出一次梨州,真是體力活啊!
咪咕的用戶太多了,又不會(huì)向我們公開田轧,所以第2點(diǎn)是做不到的暴匠。只能努力做到第1點(diǎn)
為了不打擾陌生人,每次發(fā)過的號(hào)碼都記下來傻粘,下個(gè)月不再發(fā)送每窖,以后也不再發(fā)送,也就說只會(huì)收到一次弦悉。
移動(dòng)號(hào)碼段:1340-1348窒典,135-139,150-152稽莉,157-159瀑志,182-184,187-188
環(huán)境
create_no_migu_phone.sh
運(yùn)行會(huì)生成no_migu_phone.txt
污秆,sorted_migu.txt
no_migu_phone.txt
我們需要的最終通信錄20170422070548.xls
為部門通信錄文件夾為子公司
sorted_all.txt
由執(zhí)行excel2json.js
腳本后生成劈猪,為子公司通信錄sorted_migu.txt
為包含所有子公司的通信錄no_migu_phone-2月.txt
由no_migu_phone.txt
更名而來,已發(fā)送過的號(hào)碼
數(shù)據(jù)準(zhǔn)備和腳本運(yùn)行步驟:
- 先從公司網(wǎng)站導(dǎo)出各部門的通信錄文件良拼,比如
20170422070548.xls
-
node excel2json.js
生成sorted_all.txt
- 重命名
no_migu_phone.txt
為no_migu_phone-2月.txt
- 運(yùn)行
create_no_migu_phone.sh
代碼
create_no_migu_phone.sh
#!/usr/bin/env bash
##author: zhoujie<13456774460@139.com>
##這個(gè)腳本的作用是:生成咪咕彩漫的xxxxxxxx|xxxxxxxx格式的通信錄文件战得,并排除咪咕號(hào)碼
##調(diào)試開關(guān)
#set -eux
readonly randSortedFile=sorted_rand.txt
readonly miguSortedFile=sorted_migu.txt
##生成一定范圍內(nèi)的隨機(jī)數(shù)
##$1:最小的數(shù)
##$2:最大的數(shù)
function rand() {
local min=$1
local max=$(($2-min+1))
local num
num=$(head -n 11 < /dev/urandom | cksum | awk -F ' ' '{print $1}')
echo $((num%max+min))
}
##生成咪咕通信錄
function create_migu_phones () {
echo "生成咪咕通信錄:${miguSortedFile}......."
##找出所有子公司目錄下的sorted_all.txt文件
local company
company=$(find . -mindepth 1 -name sorted_all.txt)
##讀取所有sorted_all.txt文件內(nèi)容,并排序和剔除重復(fù)號(hào)碼将饺,最后寫入${miguSortedFile}文件
cat $company | sort -u > ${miguSortedFile}
}
##生成隨機(jī)號(hào)碼
function create_random_phones () {
echo "生成隨機(jī)號(hào)碼:${randSortedFile}......"
local randFile=rand.txt
true > ${randFile}
##生成randFile(rand.txt)文件
for (( i = 0; i < 3000; i = $((i + 1)) )); do
rnd=$(rand 13400000000 13999999999)
printf "%d|%d\n" "$rnd" "$rnd" >>${randFile}
done
#排序隨機(jī)號(hào)碼
sort -u ${randFile} > ${randSortedFile}
#刪除隨機(jī)號(hào)碼文件
rm ${randFile}
}
create_migu_phones
create_random_phones
#生成最終結(jié)果
##排除已在前面月份中發(fā)過的號(hào)碼
function comm_months_and_migu_phones () {
local noMiguFile=no_migu_phone.txt
echo "生成排除咪咕的通信錄:${noMiguFile}......."
local months_and_migu_phones=months_and_migu_phones.txt
##清空temp.txt
true >temp.txt
##把前面月份中的號(hào)碼寫入temp.txt
find . -maxdepth 1 -name "no_migu_phone-*" -exec cat {} >>temp.txt \;
##把咪咕中的號(hào)碼寫入temp.txt
cat ${miguSortedFile} >>temp.txt
##排序并排除重復(fù)的號(hào)碼
sort -u temp.txt >$months_and_migu_phones
rm temp.txt
##生成最終結(jié)果
comm -23 ${randSortedFile} ${months_and_migu_phones} >${noMiguFile}
##清除生成的中間文件
rm $months_and_migu_phones
}
comm_months_and_migu_phones
rm ${randSortedFile}
echo Done!!!
代碼
excel2json.js
#!/usr/bin/env node
//需要安裝node_xj: npm install xls-to-json
node_xj = require("xls-to-json");
var fs = require('fs');
var path = require('path');
fs.readdir(process.cwd(), function (err, files) {
var allPhones = [];
files.forEach(function (file) {
var basename = path.basename(file);
var ext = path.extname(file);
var jsonname = basename + '.json';
if (ext === '.xls') {
node_xj({
input: basename, // input xls
output: null, // output json
sheet: "數(shù)據(jù)" // specific sheetname
}, function (err, result) {
if (err) {
console.error(err);
} else {
var obj = result;
obj.forEach(function (person) {
allPhones.push(person["手機(jī)"]);
});
}
});
}
});
var sorted_all_file = 'sorted_all.txt';
if( fs.existsSync(sorted_all_file) ) {
fs.unlinkSync(sorted_all_file);
}
setTimeout(function () {
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var sorted = allPhones.sort();
var unique = sorted.filter(onlyUnique);
unique.forEach(function (item) {
fs.appendFileSync(sorted_all_file, item + '|' + item + '\n');
});
}, 5 * 1000);
});
js代碼生成隨機(jī)號(hào)碼
#!/usr/bin/env node
var fs = require('fs');
const createRandomPhones = (min, max, count) => {
let phones = [];
const d = max - min + 1;
while (phones.length < count) {
const randonNum = Math.floor(Math.random() * d + min);
if (phones.includes(randonNum) === false) {
phones.push(randonNum);
}
}
return phones;
}
//移動(dòng)號(hào)碼段
///////////////////////////////////////////////////
function RangePhone(min, max, ratio) {
this.min = min;
this.max = max;
this.ratio = ratio;
}
const r1340_1348 = new RangePhone(13400000000, 13489999999, 1);
const r135_139 = new RangePhone(13500000000, 13999999999, 5);
const r150_152 = new RangePhone(15000000000, 15299999999, 3);
const r157_159 = new RangePhone(15700000000, 15999999999, 3);
const r182_184 = new RangePhone(18200000000, 18499999999, 3);
const r187_188 = new RangePhone(18700000000, 18899999999, 3);
///////////////////////////////////////////////////
const mobileRanges = [r1340_1348, r135_139, r150_152, r157_159, r182_184, r187_188];
const totalCount = 3000;
const totalRatio = mobileRanges.reduce( (accumulator, currentValue) => {return accumulator + currentValue.ratio },0);
//按各個(gè)手機(jī)號(hào)碼段所占比例贡避,生成總共totalCount個(gè)隨機(jī)號(hào)碼
let phones = [];
mobileRanges.forEach( (item) => {
const {min, max} = item;//對(duì)象的解構(gòu)賦值
let count = Math.floor(item.ratio / totalRatio * totalCount);
let sorted = createRandomPhones(min, max, count);
phones = phones.concat(sorted);
});
let file = 'sorted_rand.txt';
if( fs.existsSync(file) ) {
fs.unlinkSync(file);
}
//把隨機(jī)號(hào)碼排序并寫入文件
let sorted = phones.sort();
sorted.forEach(function (item) {
fs.appendFileSync(file, `${item}|${item}\n`);//``//模板字符串
});
最新代碼
如有代碼有更新,請(qǐng)參考https://github.com/zhoujie903/Shell
[TOC]