Habib Mahdi

Habib Mahdi

// Name: Frozen
// Description: Kill a process and open it again
// Author: Positron
import "@johnlindquist/kit"
let fkill = await npm('fkill')
let open = await npm ('open')
let pslist = await npm ('ps-list')
let processes = await pslist()
let procArray = []
for (let i of processes){
procArray.push(i.name)
}
let appChoice = await arg("Choose a process", procArray)
console.log(`Closing ${appChoice}`)
// requires the `force` option to work. Also needs the .exe
fkill(appChoice, {
force: true
})
await wait(1000)
console.log(`Opening ${appChoice}`)
// Doesn't work with the .exe so need to remove it
open(appChoice.replace(".exe", ""))

// Name: Habitica
// Description: Browse and manage your Habitica tasks, dailies, and habits
// Author: Positron
// Shortcuts:
import "@johnlindquist/kit"
// Get the user API token
const apiToken = await env("HABITICA_API_TOKEN");
// Get the user ID which is also used as the client id
const userID = await env("HABITICA_USER_ID");
const actionType = await arg("What would you like to do?", ["Browse", "Create new tasks"])
if(actionType == "Browse"){
const requestType = await arg("What would you like to browse?", ["Todos", "Dailies", "Habits"])
// if the user wants to browse their todos, then show them to him/her then let them choose one to mark as done
if(requestType == "Todos"){
const todos = await getUserData("todos")
// console.log(todos)
const todoID = await arg("Which todo would you like to mark as done?", todos.data.map(({_id, text, checklist, updatedAt}) => ({
name: text,
value: _id,
description: `Updated at ${updatedAt}`,
preview: `
<h3>${text}</h3>
<p>${checklist?.map(({text}) => text) ?? ""}</p>
`
})))
// mark the todo as done
await markTaskDone(todoID)
}
// Same thing but for dailies
else if(requestType == "Dailies"){
const dailies = await getUserData("dailys")
const dailyID = await arg("Which daily would you like to mark as done?", dailies.data.map(({_id, text, checklist, updatedAt}) => ({
name: text,
value: _id,
description: `Updated at ${updatedAt}`,
preview: `
<h3>${text}</h3>
<p>${checklist?.map(({text}) => text) ?? ""}</p>
`
})))
await markTaskDone(dailyID)
}
// Same thing but for habits
else if(requestType == "Habits"){
const habits = await getUserData("habits")
const habitID = await arg("Which habit would you like to mark as done?", habits.data.map(({_id, text, updatedAt}) => ({
name: text,
value: _id,
description: `Updated at ${updatedAt}`,
// preview: `
// <h3>${text}</h3>
// <p>${checklist?.map(({text}) => text) ?? ""}</p>
// `
})))
await markTaskDone(habitID)
}
}
// Logic for creating new tasks
else if(actionType == "Create new tasks"){
const type = await arg("What type of task would you like to create?", ["Todo", "Daily", "Habit"])
const text = await arg("What would you like the task to say?")
if (type == "Habit"){
const upOrDown = await arg("Would you like to create a positive or negative habit?", ["Positive", "Negative"])
const upOrDownBool = upOrDown == "Positive" ? true : false
await createTask(type, text, upOrDownBool)
}
else {
await createTask(type, text)
}
}
// Gets the user's tasks from Habitica
async function getUserData(type) {
const response = await get(`https://habitica.com/api/v3/tasks/user?type=${type}`, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
return response.data
}
// function to mark task as done
async function markTaskDone(id) {
// need to put an empty second argument in the post request
const response = await post(`https://habitica.com/api/v3/tasks/${id}/score/up`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`}})
// console.log(response.data)
}
// function to create a new task
async function createTask(type, text, habitDirection = undefined) {
if (habitDirection == true){
const response = await post(`https://habitica.com/api/v3/tasks/user?text=${text}&type=${type}&up=true`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
}
else if(habitDirection == false){
const response = await post(`https://habitica.com/api/v3/tasks/user?text=${encodeURI(text)}&type=${type}&down=true`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
}
else{
const response = await post(`https://habitica.com/api/v3/tasks/user?text=${encodeURI(text)}&type=${type}`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
}
}

// Name: Search for Wikipedia articles
// Description: Prompts for a search string and lists articles from Wikipedia
// Author: Positron
// Shortcut:
import "@johnlindquist/kit"
let searchQuery = await arg("Enter a search query: ");
const url = `https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrlimit=20&prop=pageimages%7Cextracts&exintro&explaintext&exlimit=max&format=json&gsrsearch=${searchQuery}`;
const result = await get(url);
let articleId = await arg("Please choose an article: ", Object.keys(result.data.query.pages).map(
(id) => ({
name: result.data.query.pages[id].title,
description: `https://en.wikipedia.org/wiki/${result.data.query.pages[id].title}`,
preview: () => `
<h3>${result.data.query.pages[id].title}</h3>
<p>${result.data.query.pages[id].extract}</p>`,
value: result.data.query.pages[id].pageid
})
))
browse(`https://en.wikipedia.org/?curid=${articleId}`)