Created
November 10, 2023 10:59
-
-
Save houshuang/34afe44ad4d7fdbf264f8c3003888460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const axios = require('axios'); | |
const app = express(); | |
app.use(express.json()); | |
const defaultFactor = 2.5 | |
const defaultInterval = 2 | |
const generateOutput = ({ factor, interval, due }) => | |
`[[date:${due.toISOString().slice(0, 10)}]] ${factor.toFixed(2)}/${interval.toFixed(2)}`; | |
let counter = 0 | |
let tweetCounter=0 | |
app.get('/get-tweet-text', async (req, res) => { | |
let apiUrl | |
try{ | |
const tweetUrl = req.query.url; | |
const tweetId = tweetUrl.split('/').pop().split('?')[0]; | |
console.log('tweet',tweetId, tweetCounter) | |
tweetCounter += 1 | |
apiUrl = `https://cdn.syndication.twimg.com/tweet-result?id=${tweetId}&token=43p8x53yobw`; | |
} catch(e) {} | |
try { | |
const response = await axios.get(apiUrl); | |
const tweetText = response.data.text; | |
res.write( tweetText ); | |
res.end() | |
} catch (error) { | |
console.error(error) | |
res.status(500).json({ error: 'Unable to get tweet text' }); | |
} | |
}); | |
app.get("/calculate", (req, res) => { | |
console.log("request", new Date().toISOString(), counter) | |
counter+=1 | |
const { srs, signal } = req.query; | |
const [_, factor, interval] = srs.split("/"); | |
const [calculatedFactor, calculatedInterval, calculatedDue] = [ | |
parseInt(factor, 10) || defaultFactor, | |
parseFloat(interval) || defaultInterval, | |
new Date(), | |
]; | |
const calculatedSignal = parseInt(signal, 10); | |
const newNode = ankiSchedulerGetNewMetadata( | |
{ | |
interval: calculatedInterval, | |
factor: calculatedFactor, | |
due: calculatedDue, | |
}, | |
calculatedSignal | |
); | |
res.write(generateOutput(newNode)); | |
res.end(); | |
}); | |
const port = process.env.PORT || 3000; | |
app.listen(port, () => { | |
console.log(`Server is running on port ${port}`); | |
}); | |
function ankiSchedulerGetNewMetadata(meta, signal) { | |
var maxInterval = 50 * 365; | |
var minFactor = 1.3; | |
var hardFactor = 1.2; | |
var jitterPercentage = 0.05; | |
var randomFromInterval = function (min, max) { | |
return Math.random() * (max - min) + min; | |
}; | |
var addJitter = function (node) { | |
var jitter = node.interval * jitterPercentage; | |
return { | |
interval: node.interval + randomFromInterval(-jitter, jitter), | |
factor: node.factor, | |
due: node.due, | |
}; | |
}; | |
var enforceLimits = function (node) { | |
return { | |
interval: Math.min(node.interval, maxInterval), | |
factor: Math.max(node.factor, minFactor), | |
due: node.due, | |
}; | |
}; | |
var factor = meta.factor; | |
var interval = meta.interval; | |
var newFactor = factor; | |
var newInterval = interval; | |
var factorModifier = 0.15; | |
switch (signal) { | |
case 1: | |
newFactor = factor - 0.2; | |
newInterval = 1; | |
break; | |
case 2: | |
newFactor = factor - factorModifier; | |
newInterval = interval * hardFactor; | |
break; | |
case 3: | |
newInterval = interval * factor; | |
break; | |
case 4: | |
newInterval = interval * factor; | |
newFactor = factor + factorModifier; | |
break; | |
} | |
var newMeta = enforceLimits( | |
addJitter({ interval: newInterval, factor: newFactor }) | |
); | |
function addDays(date, days) { | |
var result = new Date(date); | |
result.setDate(result.getDate() + days); | |
return result; | |
} | |
return { | |
interval: newMeta.interval, | |
factor: newMeta.factor, | |
due: signal === 1 ? new Date() : addDays(new Date(), Math.ceil(newMeta.interval)), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment