Skip to content

Commit

Permalink
Fix standard lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzoneill committed Oct 17, 2024
1 parent 35e2be5 commit b050cc6
Show file tree
Hide file tree
Showing 30 changed files with 511 additions and 403 deletions.
9 changes: 6 additions & 3 deletions strategies/Amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Amazon extends MessageStrategy {

async Preview (message) {
try {
// eslint-disable-next-line no-undef
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox', '--headless=new'] })
const page = await browser.newPage()

Expand Down Expand Up @@ -98,19 +99,21 @@ class Amazon extends MessageStrategy {
return null
})

const image_url = await page.evaluate(() => {
const imageUrl = await page.evaluate(() => {
const image = document.body.querySelector('#landingImage')
if (image) {
return image.getAttribute('src')
}
return null
})

if (image_url == null) {
if (imageUrl == null) {
return [null, null]
} else {
MessageStrategy.typing(message)
const responseImage = await axios(image_url, { responseType: 'arraybuffer', headers: MessageStrategy.browser_config.headers })
// eslint-disable-next-line no-undef
const responseImage = await axios(imageUrl, { responseType: 'arraybuffer', headers: MessageStrategy.browser_config.headers })
// eslint-disable-next-line no-undef
const image = await resizeImg(responseImage.data, { width: 200, format: 'jpg' })
const buffer64 = Buffer.from(image, 'binary').toString('base64')
const data = 'data:image/jpeg;base64,' + buffer64
Expand Down
48 changes: 27 additions & 21 deletions strategies/Ashtanga.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const MessageStrategy = require('../MessageStrategy.js')

// eslint-disable-next-line no-extend-native
Array.prototype.myJoin = function (seperator, start, end) {
if (!start) start = 0
if (!end) end = this.length - 1
Expand Down Expand Up @@ -136,16 +137,18 @@ class Ashtanga extends MessageStrategy {
try {
Ashtanga.self.yoga_keywords.sort()

// eslint-disable-next-line no-undef
const paths = await globby('strategies/poses/*.png')

Ashtanga.self.yoga_keywords.forEach(async function (move) {
let nearest_distance = 999
let nearestDistance = 999
let nearest = ''

paths.forEach(async function (path) {
// eslint-disable-next-line no-undef
const distance = levenshtein('strategies/poses/' + move + '.png', path)
if (distance < nearest_distance && distance < 5) {
nearest_distance = distance
if (distance < nearestDistance && distance < 5) {
nearestDistance = distance
nearest = path
console.log(nearest)
}
Expand Down Expand Up @@ -276,20 +279,22 @@ class Ashtanga extends MessageStrategy {

async post_yoga_image (client, message, move) {
try {
// eslint-disable-next-line no-undef
const paths = await globby('strategies/poses/*.png')

let nearest_distance = 999
let nearestDistance = 999
let nearest = ''

paths.forEach(async function (path) {
// eslint-disable-next-line no-undef
const distance = levenshtein('strategies/poses/' + move + '.png', path)
if (distance < nearest_distance) {
nearest_distance = distance
if (distance < nearestDistance) {
nearestDistance = distance
nearest = path
}
})

if (nearest_distance > 3) {
if (nearestDistance > 3) {
return
}

Expand All @@ -305,31 +310,32 @@ class Ashtanga extends MessageStrategy {
// arr = [the, boy, is, in, the, river]
const len = arr.length
const wanted = pos + seqlen
const max_indice = wanted > len - 1 ? len - 1 : wanted - 1
return arr.myJoin(' ', pos, max_indice)
const maxIndice = wanted > len - 1 ? len - 1 : wanted - 1
return arr.myJoin(' ', pos, maxIndice)
}

YogaEnabled (message) {
if (Ashtanga.self.enabled) {
let nearest_distance = 9999
let nearestDistance = 9999
let nearest = 9999

Ashtanga.self.yoga_keywords.forEach(async function (pose) {
try {
const yoga_pose = pose.toLowerCase()
const yogaPose = pose.toLowerCase()
// get the length of the pose
// use this length to match sentance in the message
const yoga_pose_arr = yoga_pose.indexOf(' ') > -1 ? yoga_pose.split(' ') : [yoga_pose]
const target_string_arr = message.body.toLowerCase().indexOf(' ') > -1 ? message.body.toLowerCase().split(' ') : [message.body.toLowerCase()]
const yogaPoseArr = yogaPose.indexOf(' ') > -1 ? yogaPose.split(' ') : [yogaPose]
const targetStringArr = message.body.toLowerCase().indexOf(' ') > -1 ? message.body.toLowerCase().split(' ') : [message.body.toLowerCase()]

for (let x = 0; x < target_string_arr.length; x++) {
// create a string of the next yoga_pose_arr.lenght indices from the target string
const substring = Ashtanga.self.get_next_indices(target_string_arr, x, yoga_pose_arr.length)
for (let x = 0; x < targetStringArr.length; x++) {
// create a string of the next yogaPoseArr.lenght indices from the target string
const substring = Ashtanga.self.get_next_indices(targetStringArr, x, yogaPoseArr.length)
// e.g: substring = "string of the next"

const distance = levenshtein(yoga_pose, substring)
if (distance < nearest_distance) {
nearest_distance = distance
// eslint-disable-next-line no-undef
const distance = levenshtein(yogaPose, substring)
if (distance < nearestDistance) {
nearestDistance = distance
nearest = pose
}
}
Expand All @@ -338,9 +344,9 @@ class Ashtanga extends MessageStrategy {
}
})

const check_distance = nearest === 'Navasana' ? 1 : 5
const checkDistance = nearest === 'Navasana' ? 1 : 5

if (nearest_distance < check_distance) {
if (nearestDistance < checkDistance) {
MessageStrategy.typing(message)
Ashtanga.self.post_yoga_image(MessageStrategy.client, message, nearest)
return true
Expand Down
67 changes: 34 additions & 33 deletions strategies/Chan.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Chan extends MessageStrategy {
provides: {
'Chan subscriptions': {
test: function (message) {
return message.body.toLowerCase() == 'chan subscriptions'
return message.body.toLowerCase() === 'chan subscriptions'
},
access: function (message, strategy, action) {
return MessageStrategy.hasAccess(message.sender.id, strategy.constructor.name + action.name)
Expand Down Expand Up @@ -76,10 +76,10 @@ class Chan extends MessageStrategy {
for (let y = 0; y < chatkeys.length; y++) {
const subkeys = Object.keys(MessageStrategy.state.Chan.Notified[chatkeys[y]])
for (let h = 0; h < subkeys.length; h++) {
MessageStrategy.state.Chan.LastNotified[message.from][chatkeys[y]][subkeys[h]] = 0
console.log('Popped: ' + MessageStrategy.state.Chan.Notified[chatkeys[y]][subkeys[h]].pop().toString())
}
}
MessageStrategy.state.Chan.LastNotified[message.from][url] = 0
} catch (err) {
}
}
Expand All @@ -89,7 +89,7 @@ class Chan extends MessageStrategy {
let msg = ''
const subkeys = Object.keys(MessageStrategy.state.Chan.Subscribed[message.from])

if (subkeys.length == 0) {
if (subkeys.length === 0) {
MessageStrategy.client.sendText(message.from, 'This chat has 0 subscriptions')
return
}
Expand All @@ -106,25 +106,25 @@ class Chan extends MessageStrategy {
try {
const parts = message.body.split(' ')
const isno = parseInt(parts[2])
let to_be_removed = parts[2]
let toBeRemoved = parts[2]

if (Number.isNaN(isno) == false) {
if (Number.isNaN(isno) === false) {
const subkeys = MessageStrategy.state.Chan.Subscribed[message.from]
for (let h = 0; h < subkeys.length; h++) {
if (h == isno) {
to_be_removed = subkeys[h]
if (h === isno) {
toBeRemoved = subkeys[h]
break
}
}
}

const index = MessageStrategy.state.Chan.Subscribed[message.from].indexOf(to_be_removed)
const index = MessageStrategy.state.Chan.Subscribed[message.from].indexOf(toBeRemoved)
if (index !== -1) {
MessageStrategy.state.Chan.Subscribed[message.from].splice(index, 1)
}

delete MessageStrategy.state.Chan.Notified[message.from][to_be_removed]
delete MessageStrategy.state.Chan.LastNotified[message.from][to_be_removed]
delete MessageStrategy.state.Chan.Notified[message.from][toBeRemoved]
delete MessageStrategy.state.Chan.LastNotified[message.from][toBeRemoved]

MessageStrategy.client.sendText(message.from, 'Un-subscribed')
} catch (err) {
Expand All @@ -135,7 +135,7 @@ class Chan extends MessageStrategy {
async GetSiteMaps (message, urlstr) {
const url = new URL(urlstr)
const robotstxt = await MessageStrategy.axiosHttpRequest(message, 'GET', url.protocol + '//' + url.hostname + '/robots.txt', false, 200, false)
const regex1 = RegExp('Sitemap: .*', 'g')
const regex1 = /Sitemap: .*/g
let array1
const resshort = []
const resfull = []
Expand All @@ -154,16 +154,17 @@ class Chan extends MessageStrategy {
const urls = []
try {
const xml = await MessageStrategy.axiosHttpRequest(message, 'GET', url, false, 200, false)
// eslint-disable-next-line no-undef
let json = x2j.toJson(xml)
json = JSON.parse(json)

if (json.hasOwnProperty('urlset')) {
if (Object.prototype.hasOwnProperty.call(json, 'urlset')) {
for (let i = 0; i < json.urlset.url.length; i++) {
urls.push(json.urlset.url[i].loc)
}
}

if (json.hasOwnProperty('rss')) {
if (Object.prototype.hasOwnProperty.call(json, 'rss')) {
for (let i = 0; i < json.rss.channel.item.length; i++) {
const item = json.rss.channel.item[i]
urls.push(item.link)
Expand All @@ -180,19 +181,19 @@ class Chan extends MessageStrategy {
try {
const parts = message.body.trim().split(' ')

if (Chan.self.validURL(parts[2]) == false) {
if (Chan.self.validURL(parts[2]) === false) {
MessageStrategy.client.sendText(message.from, 'Not a valid subscription url')
return
}

const sitemaps = await Chan.self.GetSiteMaps(message, parts[2])

if (sitemaps[0].length == 0) {
if (sitemaps[0].length === 0) {
MessageStrategy.client.sendText(message.from, 'No sitemaps or robots.txt found.')
return
}

if (sitemaps[0].length > 1 && parts.length == 3) {
if (sitemaps[0].length > 1 && parts.length === 3) {
MessageStrategy.client.sendText(message.from, 'More than 1 sitemap found, please specify which one, e.g:\n\nChan subscribe http://x.com/ 1\n\n')
let msg = ''
for (let h = 0; h < sitemaps[0].length; h++) {
Expand All @@ -202,59 +203,59 @@ class Chan extends MessageStrategy {
return
}

const index = (parts.length == 3) ? 0 : parseInt(parts[3])
const index = (parts.length === 3) ? 0 : parseInt(parts[3])

const urls = await Chan.self.GetLocsFromXML(message, sitemaps[1][index])

if (urls.length == 0) {
if (urls.length === 0) {
MessageStrategy.client.sendText(message.from, 'Not a valid subscription url')
return
}

if (Object.keys(MessageStrategy.state).indexOf('Chan') == -1) {
if (Object.keys(MessageStrategy.state).indexOf('Chan') === -1) {
MessageStrategy.state.Chan = {}
}

if (Object.keys(MessageStrategy.state.Chan).indexOf('enabled') == -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('enabled') === -1) {
MessageStrategy.state.Chan.enabled = true
}

if (Object.keys(MessageStrategy.state.Chan).indexOf('Subscribed') == -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('Subscribed') === -1) {
MessageStrategy.state.Chan.Subscribed = {}
}

if (Object.keys(MessageStrategy.state.Chan).indexOf('Notified') == -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('Notified') === -1) {
MessageStrategy.state.Chan.Notified = {}
}

if (Object.keys(MessageStrategy.state.Chan).indexOf('LastNotified') == -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('LastNotified') === -1) {
MessageStrategy.state.Chan.LastNotified = {}
}

if (Object.keys(MessageStrategy.state.Chan.Subscribed).indexOf(message.from) == -1) {
if (Object.keys(MessageStrategy.state.Chan.Subscribed).indexOf(message.from) === -1) {
MessageStrategy.state.Chan.Subscribed[message.from] = []
}

if (MessageStrategy.state.Chan.Subscribed[message.from].indexOf(sitemaps[1][index]) == -1) {
if (MessageStrategy.state.Chan.Subscribed[message.from].indexOf(sitemaps[1][index]) === -1) {
MessageStrategy.state.Chan.Subscribed[message.from].push(sitemaps[1][index])
} else {
MessageStrategy.client.sendText(message.from, 'Already subscribed')
return
}

if (Object.keys(MessageStrategy.state.Chan.Notified).indexOf(message.from) == -1) {
if (Object.keys(MessageStrategy.state.Chan.Notified).indexOf(message.from) === -1) {
MessageStrategy.state.Chan.Notified[message.from] = {}
}

if (Object.keys(MessageStrategy.state.Chan.Notified[message.from]).indexOf(sitemaps[1][index]) == -1) {
if (Object.keys(MessageStrategy.state.Chan.Notified[message.from]).indexOf(sitemaps[1][index]) === -1) {
MessageStrategy.state.Chan.Notified[message.from][sitemaps[1][index]] = []
}

if (Object.keys(MessageStrategy.state.Chan.LastNotified).indexOf(message.from) == -1) {
if (Object.keys(MessageStrategy.state.Chan.LastNotified).indexOf(message.from) === -1) {
MessageStrategy.state.Chan.LastNotified[message.from] = {}
}

if (Object.keys(MessageStrategy.state.Chan.LastNotified[message.from]).indexOf(sitemaps[1][index]) == -1) {
if (Object.keys(MessageStrategy.state.Chan.LastNotified[message.from]).indexOf(sitemaps[1][index]) === -1) {
MessageStrategy.state.Chan.LastNotified[message.from][sitemaps[1][index]] = 0
}

Expand All @@ -272,11 +273,11 @@ class Chan extends MessageStrategy {
}

async Post (message) {
if (Object.keys(MessageStrategy.state).indexOf('Chan') == -1) {
if (Object.keys(MessageStrategy.state).indexOf('Chan') === -1) {
return
}

if (Object.keys(MessageStrategy.state.Chan).indexOf('Subscribed') == -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('Subscribed') === -1) {
return
}

Expand Down Expand Up @@ -308,8 +309,8 @@ class Chan extends MessageStrategy {
}

async queuePost (chat, subscription, url) {
if (MessageStrategy.state.Chan.Notified[chat][subscription].indexOf(url) == -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('Queue') == -1) {
if (MessageStrategy.state.Chan.Notified[chat][subscription].indexOf(url) === -1) {
if (Object.keys(MessageStrategy.state.Chan).indexOf('Queue') === -1) {
MessageStrategy.state.Chan.Queue = {}
}
MessageStrategy.state.Chan.Notified[chat][subscription].push(url)
Expand Down
Loading

0 comments on commit b050cc6

Please sign in to comment.