Is it possible to get multiple random api key from .env? - reactjs

In my .env file there is something like this:
GOOGLE_MAP_API=AIzaSyByfXuuwxOIaWlefhSxqhMweF-0
Another_API_KEY=5893978af2537e042beb233b1
I would like to create a .env file in which "Another_API_KEY" is randomly choosen from multiple api keys list.
Something like this:
GOOGLE_MAP_API=AIzaSyByfXuuwxOIaWlefhSxqhMweF-0
var r_text = new Array();
r_text[0] = "a3219d4e2772db6e34c62144b27f";
r_text[1] = "5bbe61fe6db548e665a49663eba2";
r_text[2] = "d74ae61790a9937e3f6d5d3ddc83";
var nn = Math.floor(3 * Math.random());
var Another_API_KEY = r_text[nn]
But this is'n working. Is this possible to get random keys from the list to React JS Application from .env file?

.env files store strings so it's not possible to run Javascript inside it.
What you can do, however, is store all keys you want to randomly pick up from in .env and, when you use them inside your app, you pick one of them.
.env
GOOGLE_MAP_API=AIzaSyByfXuuwxOIaWlefhSxqhMweF-0
RANDOM_KEYS="a3219d4e2772db6e34c62144b27f 5bbe61fe6db548e665a49663eba2 d74ae61790a9937e3f6d5d3ddc83"
Then on your code:
// Choose random key from all options
const RANDOM_KEYS_ARRAY = process.env.RANDOM_KEYS.split(" ")
const RANDOM_KEY = RANDOM_KEYS_ARRAY[Math.floor(RANDOM_KEYS_ARRAY.length * Math.random())]
Here's a sandbox with working code:
https://codesandbox.io/s/getting-random-key-from-env-xuup4r

I managed it.
I the file app.js where the command was to get api key from .env: &appid=${process.env.REACT_API_KEY}
I deleted: process.env.
and inserted
'var r_text = new Array();
r_text[0] = "456afa82537e042beb233b25";
r_text[1] = "s54667e042b33b1fa525";
r_text[2] = "456a82537e042beb233b525";
var nn = Math.floor(3 * Math.random());
var REACT_API_KEY = r_text[nn];'
And it works. Thank you

Related

How to get an array from appsettings.json file in .Net 6?

I've read this excellent SO post on how to get access to the appsettings.json file in a .Net 6 console app.
However, in my json file I have several arrays:
"logFilePaths": [
"\\\\server1\\c$\\folderA\\Logs\\file1.log",
"\\\\server2\\c$\\folderZ\\Logs\\file1A1.log",
"\\\\server3\\c$\\folderY\\Logs\\file122.log",
"\\\\server4\\c$\\folderABC\\Logs\\fileAB67.log"
],
And I get the results if I do something like this:
var builder = new ConfigurationBuilder().AddJsonFile($"appsettings.json", true, true);
var config = builder.Build();
string logFile1 = config["logFilePaths:0"];
string logFile2 = config["logFilePaths:1"];
string logFile3 = config["logFilePaths:2"];
But I don't want to have to code what is effectively an array into separate lines of code, as shown.
I want to do this:
string[] logFiles = config["logFilePaths"].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
But it gives me an error on config["logFilePaths"] saying it's null.
Why would that be null?
To access the logFilePaths as an array, you want to use the Get<T> extension method:
string[] logFilePaths = config.GetSection("logFilePaths").Get<string[]>();
One option is to install Microsoft.Extensions.Configuration.Binder nuget and use Bind (do not forget to setup CopyToOutputDirectory for appsettings.json):
var list = new List<string>();
config.Bind("logFilePaths", list);
Another - via GetSection (using the same nuget to bind a collection):
var list = config.GetSection("logFilePaths").Get<List<string>>();

I need to pull the text of an image file name from an array but am only getting "pyimage#" or "tkinter.PhotoImage object at X" as replies

I'm trying to create an array with an assortment of different randomized image files in it to display on a set of buttons in Tkinter. When a given button is clicked I'd like to add the text of that file's name to a new array. Basically, when button with imageX is clicked add "imageX" to a new array.
Unfortunately, I always get a return that isn't the image's filename, or the variable that I've set to correspond to that image, but instead either:
"tkinter.PhotoImage object at X" (where is X is a location like "0x0000020FC894D2E0") if the command is populationbeta.append (population[0])
or
"pyimage#" (where # is an integer that seems to relate to the number of images in the source file), if I change the command to populationbeta.append (str(population[0]))
I feel like there should be a simple way of doing this and I've tried every work around I can think of but I'm not getting it to work. Any help would be very much appreciated! Thanks!
Here's a shortened/simplified version of the code in question:
master=tkinter.Tk()
master.title("Not working")
a1b1c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c1.png")
a1b1c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c2.png")
a1b1c3 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c3.png")
a1b2c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c1.png")
a1b2c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c2.png")
population = [a1b1c1, a1b1c2, a1b1c3, a1b2c1, a1b2c2]
populationbeta = []
populationbeta.append(population[0])
print(populationbeta)
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
Correct. That shows that you have a list of PhotoImage objects. If you want the filenames you can use .cget('file') on the objects. cget is a common tkinter method for getting the value of a configured option.
filenames = [image.cget('filename') for image in population]
Or, if you don't want to use a list comprehension to create a list of filenames, you can do it on an individual image like so:
populationbeta.append(population[0].cget("file"))

How to append an existing delta to the end of current Quill content

I have stored existing Quill content as JSON in a database. I need to append this to the end of the existing Quill content and have been unable to do it so far.
UpdateContents(delta) adds it to the beginning of the current content and I have not been successful in specifying an insertion point.
I can specify where to insert plain text with InsertText. How can I accomplish this with inserting deltas?
I found a "Concat" reference at https://github.com/quilljs/delta#construction-1 that allows me to combine the two deltas. Here's an implementation that works for my purposes:
var Delta = Quill.import('delta');
var passedDelta = new Delta(JSON.parse(passedValue));
var existingDelta = this.get("quill").getContents()
var combinedDelta = existingDelta.concat(passedDelta);
this.get("quill").setContents(combinedDelta)

Video.js - How to reference multiple videos in one page?

My goodness, I cannot find an answer for this and I spent several hours already.
How can you reference multiple videos at the same time in video.js?
The API documentation says:
Referencing the Player: You just need to make sure your video tag has an ID. The example embed code has an ID of "example_video_1". If you have multiple videos on one page, make sure every video tag has a unique ID.
var myPlayer = V("example_video_1");
This example shows a single ID, but it doesnt show how I can reference multiple IDs at the same time.
If I have 3 different tags: "video_1", "video_2", "video_3", how do I reference them all?
I tried an array and it didnt work. I also tried listing the videos like this:
var myPlayer = _V_("video_1", "video_2");
and didnt work neither.
Can somebody help me here?
Thank you.
You can't pass multiple ids to _V_(). Either do them one at a time:
var myPlayer1 = _V_("video_1");
var myPlayer2 = _V_("video_2");
var myPlayer3 = _V_("video_3");
Or if you want them as an array:
var myPlayers = Array(_V_("video_1"), _V_("video_2"), _V_("video_3"));
myPlayers[1].play();
Note: this was written for an older version of video.js. _V_() still works but is deprecated: use videojs() instead.
This would also work:
var video = [];
video[1] = _V_("Video1");
video[2] = _V_("Video2");
video[3] = _V_("Video3");
video[4] = _V_("Video4");
video[5] = _V_("Video5");
video[6] = _V_("Video6");
video[7] = _V_("Video7");
video[8] = _V_("Video8");
video[9] = _V_("Video9");
video[10] = _V_("Video10");

zend framework multiple input file upload

i have a form that contains two file element with unique name. i want to reach each file element from Zend_File_Transfer_Adapter_Http(); how can i do that?
if i use
$apt = new Zend_File_Transfer_Adapter_Http();
.
.
$apt->receive();
two files have been uploaded. but i execute different queries for each file element.
for example
$smallPic = small pic name that i get from input smallPic
i execute
update products set smallPic = '$smallPic'
and for large pic
$largePic = large pic name that i get from input largePic
i execute
update products set largePic = '$largePic'
how can i reach each input file element with $apt ?
Here is some code I have used in the past to receive multiple files from a Zend_Form where many of the file upload fields were dynamic and not part of the form.
Since you know the names of the two file uploads, you can also say $apt->getFileInfo('my_file');
$apt = new Zend_File_Transfer_Adapter_Http();
$files = $apt->getFileInfo();
foreach($files as $file => $fileInfo) {
if ($apt->isUploaded($file)) {
if ($apt->isValid($file)) {
if ($apt->receive($file)) {
$info = $apt->getFileInfo($file);
$tmp = $info[$file]['tmp_name'];
$data = file_get_contents($tmp);
// here $tmp is the location of the uploaded file on the server
// var_dump($info); to see all the fields you can use
}
}
}
}
Hope that helps.

Resources