I'm basically looking for functionality equivalent to this Python code in JS:
It read a pdf file and send the bytes through a Post request
from requests import get, post
source = "my_pdf.pdf"
with open(source, "rb") as f:
data_bytes = f.read()
post_url = "my_url"
resp = post(url = post_url, data = data_bytes)
At the moment I'm using the react-jsonschema-form library, which has and option to read/load files form the following code:
{
"title": "Files",
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "data-url",
"title": "Single file"
}
}
}
The above json schema generates this form:
With that I can load a file form my computer:
But from here, I don't know how to send the data to an API
Related
I have a list of data in my JSON file. I am trying to output certain strings and arrays from my JSON file via my JS. How do I go on about this? All these files are saved on my desktop.
I've tried Xhttp code. But I think I need some server going on for that, and I don't have that. Also, I'm pretty sure this should be possible without having to use a server?
PS: the json file is named: movie.json
JSON CODE
{
"movie": {
"name": "drive",
"year": "2011",
"people": {
"actors": [
{
"name": "ryan gosling"
},
{
"name": "cary mulligan"
},
{
"name": "bryan cranston"
}
]
}
}
}
JS CODE
function preload() {
var movie = load.JSON("movie.json");
}
function(movie) {
var movie = JSON.parse(movie);
console.log(movie[0].name);
console.log(movie[0].year);
console.log(movie[0].actors);
}();
drive, 2011, ryan gosling, cary mulligan, bryan cranston
var movie;
var http = new XMLHttpRequest();
xhhtp.open( "GET", "movie.json", true);
xhttp.onreadystatechange = function () {
if(http.readyState == 4 && http.status == 200) {
movie = JSON.parse(http.responseText);
}
}
http.send();
console.log(movie[0].name);
console.log(movie[0].year);
console.log(movie[0].actors);
I do not know if the code above will help you. Using XMLHttpRequest will help you fetch the json file then you can parse and sort into array. Note: you do not need a server to use XMLHttpRequest, if you have text editor like VSCode you can us it to run live HTML codes then you can get the full link to the JSON file you want to parse e.g localhost:9000/movie.json
I'm trying to scrape the viewers on www.twitch.tv/directory using Python. I have tried the basic BeautifulSoup script:
url= 'https://www.twitch.tv/directory'
html= urlopen(url)
soup = BeautifulSoup(url, "html5lib") #also tried using html.parser, lxml
soup.prettify()
This gives me html without the actual viewer numbers shown.
Then I tried using param ajax data. From this thread
param = {"action": "getcategory",
"br": "f21",
"category": "dress",
"pageno": "",
"pagesize": "",
"sort": "",
"fsize": "",
"fcolor": "",
"fprice": "",
"fattr": ""}
url = "https://www.twitch.tv/directory"
# Also tried with the headers parameter headers={"User-Agent":"Mozilla/5.0...
js = requests.get(url,params=param).json()
But I get a JSONDecodeError: Expecting value: line 1 column 1 (char 0) error.
From then I moved on to selenium
driver = webdriver.Edge()
url = 'https://www.twitch.tv/directory'
driver.get(url)
#Also tried driver.execute_script("return document.documentElement.outerHTML") and innerHTML
html = driver.page_source
driver.close()
soup = BeautifulSoup(html, "lxml")
These just yield the same result I get from the standard BeautifulSoup call.
Any help on scraping the view count would be appreciated.
The stats are not present in the page when its first loaded. The page makes a graphql request to https://gql.twitch.tv/gql to fetch the game data. When a user isn't logged in the graphql request asks for the query AnonFrontPage_TopChannels.
Here is a working request in python:
import requests
import json
resp = requests.post(
"https://gql.twitch.tv/gql",
json.dumps(
{
"operationName": "AnonFrontPage_TopChannels",
"variables": {"platformType": "all", "isTagsExperiment": True},
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "d94b2fd8ad1d2c2ea82c187d65ebf3810144b4436fbf2a1dc3af0983d9bd69e9",
}
},
}
),
headers = {'Client-Id': 'kimne78kx3ncx6brgo4mv6wki5h1ko'},
)
print(json.loads(resp.content))
I've included the Client-Id in the request. The id doesn't seem to be unique to the session, but I imagine Twitch expires them, so this likely won't work forever. You'll have to inspect future graphql requests and grab a new Client-Id in the future or figure out how to programmatically scrape one from the page.
This request actually seems to be the Top Live Channels section. Here's how you can get the view counts and titles:
edges = json.loads(resp.content)["data"]["streams"]["edges"]
games = [(f["node"]["title"], f["node"]["viewersCount"]) for f in edges]
# games:
[
("Let us GAME", 78250),
("(REBROADCAST) Worlds Play-In Knockouts: Cloud9 vs. Gambit Esports", 36783),
("RuneFest 2018 - OSRS Reveals !schedule", 35042),
(None, 25237),
("Front Page of TWITCH + Fortnite FALL SKIRMISH Training!", 22380),
("Reckful - 3v3 with barry and a german", 20399),
]
You'll need to check the chrome network inspector and figure out the structure of the other requests to get more data.
And here's an example for the directory page:
import requests
import json
resp = requests.post(
"https://gql.twitch.tv/gql",
json.dumps(
{
"operationName": "BrowsePage_AllDirectories",
"variables": {
"limit": 30,
"directoryFilters": ["GAMES"],
"isTagsExperiment": True,
"tags": [],
},
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "75fb8eaa6e61d995a4d679dcb78b0d5e485778d1384a6232cba301418923d6b7",
}
},
}
),
headers={"Client-Id": "kimne78kx3ncx6brgo4mv6wki5h1ko"},
)
edges = json.loads(resp.content)["data"]["directoriesWithTags"]["edges"]
games = [f["node"] for f in edges]
I need help with API translation of SAP Leonardo. I building a translation app for studing, and following the documentation a create the method translate:
translate: function () {
//Create JSON Model with URL
var oModel = new sap.ui.model.json.JSONModel();
var langTo = this.getView().byId("idTo").getSelectedKey();
var langFrom = this.getView().byId("idFrom").getSelectedKey();
var textOld = this.getView().byId("idOldText").getValue();
//API Key for API Sandbox
var sHeaders = {
"Content-Type": "application/json",
"APIKey": "My api Key"
};
//Available Security Schemes for productive API Endpoints
//OAuth 2.0
//sending request
//API endpoint for API sandbox
var oData = {
"sourceLanguage": langTo,
"targetLanguages": [
langFrom
],
"units": [{
"value": textOld,
"key": "ANALYZE_SALES_DATA"
}]
};
oModel.loadData("https://sandbox.api.sap.com/ml/translation/translation", oData, true, "POST", null, false, sHeaders);
//Available API Endpoints
//https://mlfproduction-machine-translation.cfapps.eu10.hana.ondemand.com
//https://mlfproduction-machine-translation.cfapps.us10.hana.ondemand.com
//You can assign the created data model to a View and UI5 controls can be bound to it. Please refer documentation available at the below link for more information.
//https://sapui5.hana.ondemand.com/#docs/guide/96804e3315ff440aa0a50fd290805116.html#loio96804e3315ff440aa0a50fd290805116
//The below code snippet for printing on the console is for testing/demonstration purpose only. This must not be done in real UI5 applications.
oModel.attachRequestCompleted(function (oEvent) {
var oData = oEvent.getSource().oData;
// console.log(oData);
});
}
I used two selectBox for to get language keys both calls "idTo" and "idFrom". And I used too a input for get a text will be translate with id "idOldText". But nothing happen. the oData value always empty in the last instruction. I'm used SAP WEBIDE and I guess that it is not need configure IDE for to use the API.
Someone can help me?
it would be helpful if you provide the error from your console.
But I already have a feeling that this ends up in a cross site request, and thus will be blocked because of using a full qualified URL. Also your header whitelist is maybe missing.
Do it like this and it should work:
1) create a destination in SAP CP
2) create a new sapui5 project in SAP WebIDE and adapt neo-app.json by addin a new destination path and header whitelist your request headers
{
"welcomeFile": "/webapp/index.html",
"routes": [{
"path": "/resources",
"target": {
"type": "service",
"name": "sapui5",
"entryPath": "/resources"
},
"description": "SAPUI5 Resources"
}, {
"path": "/test-resources",
"target": {
"type": "service",
"name": "sapui5",
"entryPath": "/test-resources"
},
"description": "SAPUI5 Test Resources"
}, {
"path": "/ml-dest",
"target": {
"type": "destination",
"name": "sapui5ml-api"
},
"description": "ML API destination"
}],
"sendWelcomeFileRedirect": true,
"headerWhiteList": [
"APIKey", "Accept", "Content-Type"
]
}
3) add your method and post the request || possible issues in your version: JSON object and request headers
onInit: function () {
var oModel = new sap.ui.model.json.JSONModel();
var sHeaders = {
"Content-Type": "application/json",
"Accept": "application/json",
"APIKey": "<<yourAPIKey>>"
};
var oData = {
"sourceLanguage": "en",
"targetLanguages": [
"de"
],
"units": [{
"value": "I would like to analyze my sales data.",
"key": "ANALYZE_SALES_DATA"
}]
};
var ODataJSON = JSON.stringify(oData);
oModel.loadData("/ml-dest/translation/translation", ODataJSON, true, "POST", null, false, sHeaders);
oModel.attachRequestCompleted(function (oEvent) {
var oData = oEvent.getSource().oData;
console.log(oData.units[0].translations[0]);
});
}
4) get a successful response object when loading your app :-)
References used:
Destination creation (my own blog entry btw.) https://blogs.sap.com/2018/09/05/successfactors-extensions-with-sapui5-and-the-correct-usage-of-sap-cp-destination-services/
SAPUI5 examples for SAP ML Inference Services (see multiple examples) https://developers.sap.com/tutorials/ml-fs-sapui5-img-classification.html
Currently I am sending the data as parameter in URL.
Ex
var url = "http://localhost:8080/test?param1=" + param1Value+ "¶m2="+ param2Value;
I am using XMLHttpRequest to communicated.
But by doing this I can see the params in requested URL.
How can I send the data without passing as parameter? (Basically how do I hide those data).
And on server how do I retrieve that?
You could simply use jquery to create a POST form, fill it up, and submit it on the fly.
$("body").append("<form id='form1' action='http://localhost:8080/test' method='POST'></form>");
$("<input></input>", {
"type": "text",
"name": "param1",
"value": param1Value
}).appendTo("#form1");
$("<input></input>", {
"type": "text",
"name": "param2",
"value": param2Value
}).appendTo("#form1");
var theForm = $("#form1");
theForm.hide();
theForm.submit();
I want to download a text/json file using string that contains the data in json format.
I am adding a ArrayList containing the a object of Service Class to the model from controller. Below is the code.
#RequestMapping("/Application.html")
public ModelAndView getdetails() throws Exception {
ArrayList<Service> servicesList = new ArrayList<Service>();
ServiceBean service=new ServiceBean(); // This is bean class
String jsonData = "{\"menu\": { \"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, ] }}}";
service.setJsonData(jsonData);
servicesList.add(service);
ServiceBean service=new ServiceBean(); // This is bean class
String jsonData = "{ version-info: { minVersion: 2.0.1, currentVersion: 2.0.1, configuration: { id: {}, language: {}, url: {}, version: 2.0.1 }, clientApp: ABC }}";
service.setJsonData(jsonData);
servicesList.add(service);
ModelAndView mav = new ModelAndView("showVersion");
mav.addObject("servicesList",servicesList);
}
Now I want to download each jsonData file.
<button>Download First File</button>
<button>Download Second File</button>
On Clicking any button a text/json file containing the relevant data should download.
And the data in file should be in proper JSON format like:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
I am unable to retrieve the data in JSON format. please help me to sort out this.
Try some thing like this:
$json = json_encode( array( 'test' => 'test' ));
header('Content-disposition: attachment; filename=jsonFile.json');
header('Content-type: application/json');
echo( $json);