main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. const express = require('express')
  2. const app = express()
  3. const axios = require('axios')
  4. const query = require('./mysqler')
  5. const bodyParser = require("body-parser");
  6. const schedule = require('node-schedule');
  7. const data = ['3948352701','5208966720','2589432731','3917225829','3231047610','1005570772','2448184413','5667617555','1785533160','5672166950','5668213132','5357670210','5625133989','5178549320','3054615393','5973233937','5973637065','6003809445','1988127344','6017601357','6359395544','5972859195','5664785772','5305147245','6030122701','6026399343','2960359192','3280880197','2295650225','6017599835','5971792547','5669689463','5669328912','2253203771','6359373279','6031302315','5719743142','6039760659','1822890914','6030402215','6030660675','2782636765','6039367340','6361527487','6356947724'];
  8. app.use('/', express.static(__dirname + '/'))
  9. app.use(bodyParser.urlencoded({ extended: false }));
  10. // app.all('*', (req, res, next) => {
  11. // res.header("Access-Control-Allow-Origin", req.headers.origin); //设置来源
  12. // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  13. // res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  14. // res.header("Access-Control-Max-Age", "604800000");
  15. // res.header("Access-Control-Allow-Credentials", true);
  16. // res.header("X-Powered-By", ' 3.2.1');
  17. // // res.header("Content-Type", "application/json;charset=utf-8");
  18. // next();
  19. // });
  20. const httpreq = async (msg) => {
  21. try {
  22. let {data} = await axios({
  23. method: 'get',
  24. url: 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + msg
  25. });
  26. let redata = {
  27. weiboname: data.data.userInfo.screen_name,
  28. weiboid: msg,
  29. weibofan: data.data.userInfo.followers_count,
  30. weibopost: data.data.userInfo.statuses_count
  31. }
  32. return redata
  33. } catch (error) {
  34. console.log(error)
  35. }
  36. }
  37. Date.prototype.format = function (fmt) {
  38. var o = {
  39. "M+": this.getMonth() + 1, //月份
  40. "d+": this.getDate(), //日
  41. "h+": this.getHours(), //小时
  42. "m+": this.getMinutes(), //分
  43. "s+": this.getSeconds(), //秒
  44. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  45. "S": this.getMilliseconds() //毫秒
  46. };
  47. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  48. for (var k in o)
  49. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  50. return fmt;
  51. }
  52. const retime = (msg) => {
  53. var day1 = new Date();
  54. day1.setDate(day1.getDate() + msg);
  55. var s1 = day1.format("yyyy-MM-dd")
  56. return s1
  57. }
  58. const guid=()=>{
  59. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  60. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  61. return v.toString(16);
  62. });
  63. }
  64. const inssql = (msg) => {
  65. try {
  66. let sql = "INSERT INTO `digital`(`name`, `id`, `fan`, `time`, `post`) VALUES ('"+msg.weiboname+"', '"+msg.weiboid+"', '"+msg.weibofan+"', NOW(), '"+msg.weibopost+"');"
  67. query(sql, [1], function(err,results,fields){
  68. });
  69. } catch (error) {
  70. console.log(error)
  71. }
  72. }
  73. const select = (t1, t2, callback) => {
  74. try {
  75. let time1 = retime(t1)
  76. let time2 = retime(t2)
  77. let sql = "SELECT * FROM digital WHERE time BETWEEN '"+ time1 +"' and '"+ time2 +"';"
  78. query(sql, [1], function(err,results,fields){
  79. callback(results)
  80. });
  81. } catch (error) {
  82. console.log(error)
  83. }
  84. }
  85. const selintime = (msg, callback) => {
  86. try {
  87. let sql = "select * from digital where DATE(time) = '"+ msg +"';"
  88. query(sql, [1], function(err,results,fields){
  89. callback(results)
  90. });
  91. } catch (error) {
  92. }
  93. }
  94. const selintimeer = (callback) => {
  95. try {
  96. let sql = "SELECT * FROM `digital` WHERE DATE_FORMAT( time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ORDER BY id;"
  97. query(sql, [1], function(err,results,fields){
  98. callback(results)
  99. });
  100. } catch (error) {
  101. }
  102. }
  103. const selintimeer2 = (tm1, tm2, callback) => {
  104. try {
  105. let sql = "select * from `digital` where time BETWEEN '"+tm1+"' and '"+tm2+"' ORDER BY id;"
  106. query(sql, [1], function(err,results,fields){
  107. callback(results)
  108. });
  109. } catch (error) {
  110. }
  111. }
  112. const start = () => {
  113. data.map(async(i) => {
  114. let data = await httpreq(i)
  115. console.log('每日更新完毕')
  116. inssql(data)
  117. })
  118. }
  119. schedule.scheduleJob('30 1 8 * * *', function(){
  120. start()
  121. });
  122. app.get('/getIndex', async (req, res) => {
  123. select(0, 1, (msg1)=>{
  124. select(1, 2, (msg2)=>{
  125. res.send({
  126. d1: msg1,
  127. d2: msg2
  128. })
  129. })
  130. })
  131. })
  132. app.get('/gettime', (req, res) => {
  133. let time = req.query.time;
  134. selintime(time,(results)=> {
  135. res.send(results)
  136. })
  137. })
  138. app.get('/all', (req, res) => {
  139. selintimeer((results)=> {
  140. res.send(results)
  141. })
  142. })
  143. app.get('/setime', (req, res) => {
  144. let time1 = req.query.starttime;
  145. let time2 = req.query.endtime;
  146. selintimeer2(time1,time2,(msg)=>{
  147. res.send(msg)
  148. })
  149. })
  150. app.get('/selectall',(req,res)=>{
  151. let sql="select * from WBAccountNumber where WbState='1' order by AddTime desc";
  152. query(sql,(error, results, fields)=> {
  153. if (error){
  154. res.json('error')
  155. }else{
  156. res.json(results);
  157. };
  158. },()=>{})
  159. })
  160. app.get('/selectbyid',(req,res)=>{
  161. let wbId=req.query.id;
  162. let sql="select * from WBAccountNumber where WbId='"+wbId+"'";
  163. query(sql,(error, results, fields)=> {
  164. if (error){
  165. res.json('error')
  166. }else{
  167. res.json(results);
  168. };
  169. },()=>{})
  170. })
  171. app.get('/insert',(req,res)=>{
  172. let wbNum=req.query.num;
  173. let wbName=req.query.name;
  174. let id=guid();
  175. let day1 = new Date();
  176. let s1 = day1.format("yyyy-MM-dd hh:mm:ss");
  177. let sql='insert into WBAccountNumber values("'+id+'","'+wbNum+'","'+wbName+'","'+s1+'","1")';
  178. query(sql, function (error, results, fields) {
  179. if (error){
  180. res.json('error')
  181. }else{
  182. res.json('success');
  183. };
  184. },()=>{});
  185. })
  186. app.get('/update',(req,res)=>{
  187. let wbId=req.query.id;
  188. let wbName=req.query.name;
  189. let sql='update WBAccountNumber set WbName="'+wbName+'" where WbId="'+wbId+'"';
  190. query(sql, function (error, results, fields) {
  191. if (error){
  192. res.json('error')
  193. }else{
  194. res.json('success');
  195. };
  196. },()=>{});
  197. })
  198. app.get('/delete',(req,res)=>{
  199. let wbId=req.query.id;
  200. let sql='update WBAccountNumber set WbState="0" where WbId="'+wbId+'"';
  201. query(sql, function (error, results, fields) {
  202. if (error){
  203. res.json('error')
  204. }else{
  205. res.json('success');
  206. };
  207. },()=>{});
  208. })
  209. app.listen(2080, function() {
  210. console.log('http://localhost:2080');
  211. })