Last active
January 18, 2025 18:59
-
-
Save malys/4eafc7051599f4b4601a99f652de105c to your computer and use it in GitHub Desktop.
[Newreleases.io] integration #newreleases #userscript #xonsh #python #violentmonkey
This file contains hidden or 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
import requests | |
import os | |
import json | |
def getKey(): | |
with open(os.path.expanduser("~/.newreleases.yaml")) as f: | |
rawKey=f.read() | |
###auth-key: 121sd65f4sfxxxx | |
return rawKey.split(":")[1].strip() | |
def getProjects(): | |
response=requests.get(f"https://api.newreleases.io/v1/projects/",headers={"X-Key":KEY}) | |
result=response.json() | |
return result["projects"] | |
#[ | |
# { | |
# "id":"f2hgpe3ycw87rvzbahsnmh0zh4", | |
# "name":"Exafunction/codeium", | |
# "provider":"github", | |
# "url":"https://github.com/Exafunction/codeium", | |
# "email_notification":"weekly" | |
# }, | |
# { | |
# "id":"4jc7kn8ycdhg7thgdcy93094s0", | |
# "name":"gethomepage/homepage", | |
# "provider":"github", | |
# "url":"https://github.com/gethomepage/homepage", | |
# "email_notification":"weekly", | |
# "exclude_prereleases":true, | |
# "note":"{\n\"version\":\"\";\n\"action\":{\n \"cmd\":\"\",\n \"cwd\":\"\"\n }\n}", | |
# "tags":[ | |
# "7c3a7b7a441b" | |
# ] | |
# } | |
#] | |
def pushConfig(): | |
print("Pushing config") | |
git add "project/newreleases/config.json" | |
git commit -m "Update config of new release" | |
git push origin main | |
def getActions(CONFIGURATION): | |
actionToProcess=[] | |
for config in CONFIGURATION: | |
try: | |
provider=config["provider"] | |
name=config["name"] | |
response=requests.get(f"https://api.newreleases.io/v1/projects/{provider}/{name}/latest-release",headers={"X-Key":KEY}) | |
result=response.json() | |
#{ | |
# "version": "v11.12.0", | |
# "date": "2019-03-15T21:16:54Z", | |
# "has_note": true | |
#} | |
if result["version"]!=config["version"] and 'rc' not in result["version"]: | |
print(f"Updating {config['name']} to {result['version']}") | |
actionToProcess.append(config["action"]) | |
config["version"]=result["version"] | |
except Exception as e: | |
print(f"Error on {config['name']}") | |
pass | |
#print(actionToProcess) | |
return actionToProcess | |
def launching(actionToProcess): | |
print("Launching actions:" + str(len(actionToProcess))) | |
for action in actionToProcess: | |
print(f"{action['cmd']}") | |
actionList=action["cmd"].split(" ") | |
cd @(action["cwd"]) | |
@(actionList) | |
def loadConfig(): | |
print("Loading conf") | |
with open("config.json","r") as f: | |
return json.loads(f.read()) | |
def saveConfig(): | |
print("Saving conf") | |
with open("config.json","w") as f: | |
json.dump(CONFIG,f, indent=4, ensure_ascii=False) | |
KEY=getKey() | |
#print(f"Key: {KEY}") | |
CONFIG=loadConfig() | |
#print(f"Config: {CONFIG}") | |
actions=getActions(CONFIG) | |
# remove duplicate | |
actions = [dict(t) for t in {tuple(d.items()) for d in actions}] | |
#print(f"Actions: {actions}") | |
if (len(actions)>0): | |
saveConfig() | |
launching(actions) | |
pushConfig() |
This file contains hidden or 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
// ==UserScript== | |
// @name Newreleases.io | |
// @namespace Violentmonkey Scripts | |
// @version 1.1 | |
// @description Ajouter un button pour envoyer une requête HTTP POST avec une clé API sécurisée via une interface de configuration | |
// @author Vous | |
// @match https://github.com/* | |
// @grant GM_xmlhttpRequest | |
// @grant GM_addStyle | |
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM.getValue | |
// @grant GM.setValue | |
// @run-at document-end | |
// ==/UserScript== | |
// | |
'use strict'; | |
const HEIGHT=180 | |
const WIDTH=550 | |
const COLOR="5f9fcf" | |
const BG_COLOR="black" | |
GM_addStyle(` | |
.popup { | |
position: fixed; | |
bottom: 20px; | |
left: 50%; | |
transform: translateX(-50%); | |
background-color: #5f9fcf; | |
color: white; | |
padding: 15px; | |
border-radius: 5px; | |
font-size: 16px; | |
display: none; | |
z-index: 9999; | |
} | |
.popup.error { | |
background-color: #f44336; | |
} | |
.popup.success { | |
background-color: #5f9fcf; | |
} | |
.newreleasesio { | |
height: 300px; | |
} | |
`); | |
CONFIG_CSS=`#newreleasesio { | |
background-color: ${BG_COLOR}; | |
color: #${COLOR}; | |
height:${HEIGHT}px; | |
width: ${WIDTH}px; | |
}` | |
//=========== Configuration =========== | |
/** | |
* Returns a configuration object for the Newreleases.io userscript. | |
* @return {{endpoint: {label: string, type: string, default: string}, apikey: {label: string, type: string}, email_notification: {type: string, options: string[], default: string}}} | |
*/ | |
function defineConfiguration() { | |
//https://github.com/sizzlemctwizzle/GM_config/wiki/Fields#select | |
return { | |
'endpoint': { | |
'label': 'Endpoint', // Appears next to field | |
'type': 'text', // Makes this setting a text field | |
'default': 'https://api.newreleases.io/v1' // Default value if user doesn't change it | |
}, | |
'apikey': { | |
'label': 'API Key', | |
'type': 'text' | |
}, | |
'email_notification': { | |
'label': "Email notification", | |
'type': 'select', | |
'options': ['none', 'instant', 'hourly', 'daily', 'weekly'], | |
'default': 'none' | |
} | |
} | |
} | |
/** | |
* Creates a GM_config object for Newreleases.io userscript configuration. | |
* The configuration form will have a title and will contain the fields | |
* defined in defineConfiguration. The 'save' event will log the saved | |
* value of the Name field. | |
* @return {GM_config} The created GM_config object | |
*/ | |
function createConfiguration() { | |
return new GM_config ( | |
{ | |
'id': 'newreleasesio', // The id used for this instance of GM_config | |
'title': 'Newreleases.io', // Panel Title | |
'fields': defineConfiguration(), | |
'events': | |
{ | |
'save': function () { // runs after values are saved | |
// log the saved value of the Name field | |
this.log(this.get('Name')); | |
} | |
}, | |
'css': CONFIG_CSS | |
} | |
); | |
} | |
/** | |
* Opens the GM_config configuration form. | |
* @function | |
*/ | |
function showConfiguration() { | |
CONFIGURATION.open(); | |
CONFIGURATION.frame.style =` | |
background-color: ${BG_COLOR}; | |
color: #${COLOR}; | |
height:${HEIGHT+30}px; | |
width: ${WIDTH+30}px; | |
border: 1px solid; | |
border-radius: 3px; | |
position: fixed; | |
z-index: 9999; | |
`; | |
} | |
/** | |
* Checks if the user has configured an API key. If not, opens the | |
* configuration form. | |
*/ | |
function checkConfiguration() { | |
if (!CONFIGURATION.get("apikey") || (CONFIGURATION.get("apikey") && CONFIGURATION.get("apikey").trim().length === 0)) { | |
showConfiguration(); | |
} | |
} | |
/** | |
* Returns an object with 'provider' and 'name' properties for the given | |
* window.location. The object is determined by the value of the | |
* window.location.host property. | |
* | |
* Currently, only 'github.com' is supported. | |
* | |
* @return {{provider: string, name: string}} | |
*/ | |
function getParameters() { | |
const EQUIVALENCE = { | |
'github.com': { provider: 'github', name: window.location.pathname.split('/').slice(1, 3).join('/') } | |
} | |
return EQUIVALENCE[window.location.host] | |
} | |
//=========== POST =========== | |
/** | |
* Shows a popup with the given message and type for 5 seconds. | |
* @param {HTMLElement} popup - The element to be used as the popup. | |
* @param {string} message - The message to be displayed in the popup. | |
* @param {string} [type='success'] - The type of the popup, either 'success' or 'error'. | |
*/ | |
function showPopup(popup, message, type = 'success') { | |
popup.textContent = message; | |
popup.classList.remove('success', 'error'); | |
popup.classList.add(type); | |
popup.style.display = 'block'; | |
setTimeout(() => { | |
popup.style.display = 'none'; | |
}, 5000); | |
} | |
/** | |
* Sends a POST request to the newreleases.io API to add the given provider/name | |
* as a new project. Shows a popup with the result of the request. | |
* | |
* @function | |
*/ | |
function sendPost() { | |
checkConfiguration(); | |
const popup = document.createElement('div'); | |
popup.classList.add('popup'); | |
document.body.appendChild(popup); | |
const { provider, name } = getParameters() | |
const data = { | |
provider: provider, | |
name: name, | |
email_notification: CONFIGURATION.get("email_notification") | |
}; | |
const query = { | |
method: 'POST', | |
url: CONFIGURATION.get("endpoint") + '/projects', | |
headers: { | |
'Content-Type': 'application/json', | |
'X-Key': CONFIGURATION.get("apikey") | |
}, | |
data: JSON.stringify(data), | |
onload: function (response) { | |
console.log('newreleases return:', response); | |
if (response.status < 400) { | |
showPopup(popup, `${provider} ${name} added.`); | |
} else { | |
showPopup(popup, `${provider} ${name} failed: ${response.responseText}`, 'error'); | |
} | |
}, | |
onerror: function (error) { | |
console.error('newreleases fail:', error); | |
showPopup(popup, `${provider} ${name} failed: ${e.message}`, 'error'); | |
} | |
} | |
console.log(query) | |
GM_xmlhttpRequest(query); | |
} | |
//=============================================================================== | |
let CONFIGURATION = createConfiguration(); | |
(function () { | |
const post = document.createElement('button'); | |
post.textContent = '✔️'; | |
post.style.position = 'fixed'; | |
post.style.top = '20px'; | |
post.style.right = '70px'; | |
post.style.zIndex = '9999'; | |
post.style.padding = '5px 10px'; | |
post.style.fontSize = '20px'; | |
post.style.backgroundColor = '#5f9fcf'; | |
post.style.color = 'white'; | |
post.style.border = 'none'; | |
post.style.borderRadius = '5px'; | |
post.style.cursor = 'pointer'; | |
document.body.appendChild(post); | |
post.addEventListener('click', sendPost); | |
const conf = document.createElement('button'); | |
conf.textContent = '⚙️'; | |
conf.style.position = 'fixed'; | |
conf.style.top = '20px'; | |
conf.style.right = '20px'; | |
conf.style.zIndex = '9999'; | |
conf.style.padding = '5px 10px'; | |
conf.style.fontSize = '20px'; | |
conf.style.backgroundColor = '#5f9fcf'; | |
conf.style.color = 'white'; | |
conf.style.border = 'none'; | |
conf.style.borderRadius = '5px'; | |
conf.style.cursor = 'pointer'; | |
document.body.appendChild(conf); | |
conf.addEventListener('click', showConfiguration); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment