main.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 inssql = (msg) => {
  59. try {
  60. let sql = "INSERT INTO `digital`(`name`, `id`, `fan`, `time`, `post`) VALUES ('"+msg.weiboname+"', '"+msg.weiboid+"', '"+msg.weibofan+"', NOW(), '"+msg.weibopost+"');"
  61. query(sql, [1], function(err,results,fields){
  62. });
  63. } catch (error) {
  64. console.log(error)
  65. }
  66. }
  67. const select = (t1, t2, callback) => {
  68. try {
  69. let time1 = retime(t1)
  70. let time2 = retime(t2)
  71. let sql = "SELECT * FROM digital WHERE time BETWEEN '"+ time1 +"' and '"+ time2 +"';"
  72. query(sql, [1], function(err,results,fields){
  73. callback(results)
  74. });
  75. } catch (error) {
  76. console.log(error)
  77. }
  78. }
  79. const selintime = (msg, callback) => {
  80. try {
  81. let sql = "select * from digital where DATE(time) = '"+ msg +"';"
  82. query(sql, [1], function(err,results,fields){
  83. callback(results)
  84. });
  85. } catch (error) {
  86. }
  87. }
  88. const selintimeer = (callback) => {
  89. try {
  90. let sql = "SELECT * FROM `digital` WHERE DATE_FORMAT( time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) ORDER BY id;"
  91. query(sql, [1], function(err,results,fields){
  92. callback(results)
  93. });
  94. } catch (error) {
  95. }
  96. }
  97. const selintimeer2 = (tm1, tm2, callback) => {
  98. try {
  99. let sql = "select * from `digital` where time BETWEEN '"+tm1+"' and '"+tm2+"' ORDER BY id;"
  100. query(sql, [1], function(err,results,fields){
  101. callback(results)
  102. });
  103. } catch (error) {
  104. }
  105. }
  106. const start = () => {
  107. data.map(async(i) => {
  108. let data = await httpreq(i)
  109. console.log('每日更新完毕')
  110. inssql(data)
  111. })
  112. }
  113. schedule.scheduleJob('30 1 8 * * *', function(){
  114. start()
  115. });
  116. app.get('/getIndex', async (req, res) => {
  117. select(0, 1, (msg1)=>{
  118. select(1, 2, (msg2)=>{
  119. res.send({
  120. d1: msg1,
  121. d2: msg2
  122. })
  123. })
  124. })
  125. })
  126. app.get('/gettime', (req, res) => {
  127. let time = req.query.time;
  128. selintime(time,(results)=> {
  129. res.send(results)
  130. })
  131. })
  132. app.get('/all', (req, res) => {
  133. selintimeer((results)=> {
  134. res.send(results)
  135. })
  136. })
  137. app.get('/setime', (req, res) => {
  138. let time1 = req.query.starttime;
  139. let time2 = req.query.endtime;
  140. selintimeer2(time1,time2,(msg)=>{
  141. res.send(msg)
  142. })
  143. })
  144. app.listen(2080, function() {
  145. console.log('http://localhost:2080');
  146. })