dailylog 8-13-20

less than 1 minute read

Project TikTokAPI continues

  • added this code so we can get daily blips
from datetime import datetime
today = str(datetime.now().date())
filename = '{}_trending.csv'.format(today)

trending_videos_df.to_csv(filename,index=False)

THINGS WE NEED:

  1. Cron job
    1. This needs webserver
    2. Webserver we are comfortable with is Node
    3. Can Node run python?
    4. SO says yes

HOW TO GENERATE A RANDOM NUMBER (via python) ONCE EVERY MINUTE (via node + express)

yarn add node-cron fs

random_number.py

import sys
import random
data = random.random()


print(data)
sys.stdout.flush()

index.js

const express = require("express");
const cron = require("node-cron");
const fs = require("fs");
const spawn = require("child_process").spawn;

const app = express();

app.get("/", (req, res) => {
  res.send("I love kittens!");
});

cron.schedule("* * * * *", function () {
  console.log("running a task every minute");
  const myPythonScript = spawn("python", ["./random_number.py"]);
  myPythonScript.stdout.on("data", (data) => {
    console.log("getting here");
    console.log(data);

    // Do something with the data returned from python script
  });
});

const port = process.env.PORT || 5000;

app.listen(port);