scrapy library in python

i want to crawl the new part of “https://coinmarketcap.com/currencies/bitcoin/”
i do Tools > network > XHR files >
after that i found JSON file which shows news data but i cant extract that
if any one can send me the code to extract it.

this is my basic code and that code work which work replace on this code:


import sys
import json
import scrapy
from scrapy import Spider
from scrapy import Request
from scrapy import selector
from runer import crypto_name
from typing import Iterable, Any
from scrapy_splash import SplashRequest
from scrapy.crawler import CrawlerProcess
from flask import Flask, jsonify, Response
# ________________________________________
# make a spider

class cryptoInfo(scrapy.Spider):
    name = "ghost"
    crypto = sys.argv[1]
    start_urls = [
        f"https://coinmarketcap.com/currencies/{crypto}/",
    ]

    # ________________________________________
    # set the spider setting

    def start_requests(self):
        headers = {
            'User-Agent':
                #my user agent,

            'Accept': 'application/json, text/plain, */*',
            'Accept-Language': 'en-US,en;q=0.5',
        }
        for url in self.start_urls:
            yield scrapy.Request(url, headers=headers, callback=self.parse)

    # ________________________________________
    # set the parse

    def parse(self, response, **kwargs):

        # ________________________________________
        # undynamic items:

        coin_name = response.css("h1.sc-f70bb44c-0 ::text").get()
        coin_logo = response.css("div.sc-f70bb44c-0.jImtlI ::attr(class)").get()
        coin_price = response.css("span.sc-f70bb44c-0.jxpCgO.base-text ::text").get()
        coin_24hour_change = response.css("div.sc-f70bb44c-0.cOoglq ::text").get()
        coin_lower_price_in_24_hour = response.css("div.sc-f70bb44c-0.iQEJet.flexBetween span ::text").get()
        coin_higher_price_in_24_hour = response.css("div.sc-f70bb44c-0.iQEJet.tlr span ::text").get()
        coin_all_time_high = response.css("div.sc-f70bb44c-0.dVdjLB span ::text").get()
        coin_all_time_low = response.css("div.sc-f70bb44c-0.iQEJet div:nth-child(4) div.sc-f70bb44c-0.dVdjLB span ::text").get()
        about_coin = "".join(response.css("div:nth-child(2) div:nth-child(1) div.sc-5f3326dd-0.kAOboQ p ::text").getall()).strip()

        people_also_watch = []
        for i in range(1, 6):
            also_watch = response.css(
                f"a:nth-child({i}) span.sc-f70bb44c-0.eIZXVI.base-text ::text").get()
            people_also_watch.append(also_watch)

        # ________________________________________
        # dynamic items:

        coin_markets = ...
        coin_news = ...
        coin_analytics_data = ...

        # ________________________________________
        # unknow items:

        coin_blockchin_explorer = ...
        coin_suported_wallets = ...

        # ________________________________________
        # items dictionary

        data = {
            "name": coin_name,
            "logo": coin_logo,
            "price": coin_price,
            "24hour change": coin_24hour_change,
            "lower price in 24 hour": coin_lower_price_in_24_hour,
            "higher price in 24 hour": coin_higher_price_in_24_hour,
            "all time-high": coin_all_time_high,
            "all time-low": coin_all_time_low,
            "about coin": about_coin,
            "people also watch": people_also_watch,
            # "blockchin explorer": coin_blockchin_explorer,
            # "suported wallets": coin_suported_wallets,
            # "markets": coin_markets,
            # "news": coin_news,
            # "analytics data": coin_analytics_data,
        }

        # ________________________________________
        # save file as json

        with open('cryptoinfo.json', 'w') as json_file:
            j = json.dumps(data, indent=2)
            json_file.write(j)


process = CrawlerProcess()  # scrapy runer by script
process.crawl(cryptoInfo)
process.start()
# ________________________________________

i use main url to go in after that i try to give the XHR file URL and i give the headers but i not work and i got 403 error

how can I affect variables of the load function with a named action in SvelteKit?

Basically, I have an initial load from an external API but if the user decides to change the start date and end date then it’s supposed to query the external API again with updated parameters and load to the frontend.

This is my +page.server.ts:

import { auth } from '$lib/server/lucia';
import { fail, redirect } from '@sveltejs/kit';

import type { Actions, PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ locals }) => {
    const session = await locals.auth.validate();
    if (!session) throw redirect(302, '/login');

    const today = new Date().toISOString().slice(0, 10);

    const fetchTtlSales = async (startDate = today, endDate = today) => {
        const res = await fetch(
            `https://www.externalapi.com/api/rpts/ttl?startDate=${startDate}&endDate=${endDate}`
        );
        const data = await res.json();
        return data[0];
    };

    const fetchDeptSales = async (startDate = today, endDate = today) => {
        const res = await fetch(
            `https://www.externalapi.com/api/rpts/dpt?startDate=${startDate}&endDate=${endDate}`
        );
        const data = await res.json();
        return data;
    };

    const fetchTenders = async (startDate = today, endDate = today) => {
        const res = await fetch(
            `https://www.externalapi.com/api/rpts/tnd?startDate=${startDate}&endDate=${endDate}`
        );
        const data = await res.json();
        return data;
    };

    const fetchTax = async (startDate = today, endDate = today) => {
        const results = [];
        for (let i = 1; i < 4; i++) {
            const res = await fetch(
                `https://www.externalapi.com/api/rpts/tax${i}?startDate=${startDate}&endDate=${endDate}`
            );
            const data = await res.json();
            results.push(data);
        }
        return results;
    };

    const fetchPropDisc = async (startDate = today, endDate = today) => {
        const res = await fetch(
            `https://www.externalapi.com/api/rpts/prodisc?startDate=${startDate}&endDate=${endDate}`
        );
        const data = await res.json();
        return data;
    };

    const ttlSales = await fetchTtlSales(); // Await the result here
    const dptSales = await fetchDeptSales();
    const tndTtls = await fetchTenders();
    const taxTtls = await fetchTax();
    const propDisc = await fetchPropDisc();

    return {
        name: session.user.name,
        ttlSales: ttlSales,
        dptSales: dptSales,
        tndTtls: tndTtls,
        taxTtls: taxTtls,
        propDisc: propDisc
    };
};

export const actions: Actions = {
    logout: async ({ locals }) => {
        const session = await locals.auth.validate();
        if (!session) return fail(401);
        await auth.invalidateAllUserSessions(session.sessionId); // invalidate session
        locals.auth.setSession(null); // remove cookie
        throw redirect(302, '/login'); // redirect to login page
    },
    refresh: async ({ request, locals }) => {
        const session = await locals.auth.validate();
        if (!session) throw redirect(302, '/login');
        const { startDate, endDate } = Object.fromEntries(await request.formData()) as {
            startDate: string;
            endDate: string;
        };
        try {
            const fetchTtlSales = async () => {
                const res = await fetch(
                    `https://www.externalapi.com/api/rpts/ttl?startDate=${startDate}&endDate=${endDate}`
                );
                const data = await res.json();
                return data[0];
            };

            const fetchDeptSales = async () => {
                const res = await fetch(
                    `https://www.externalapi.com/api/rpts/dpt?startDate=${startDate}&endDate=${endDate}`
                );
                const data = await res.json();
                return data;
            };

            const fetchTenders = async () => {
                const res = await fetch(
                    `https://www.externalapi.com/api/rpts/tnd?startDate=${startDate}&endDate=${endDate}`
                );
                const data = await res.json();
                return data;
            };

            const fetchTax = async () => {
                const results = [];
                for (let i = 1; i < 4; i++) {
                    const res = await fetch(
                        `https://www.externalapi.com/api/rpts/tax${i}?startDate=${startDate}&endDate=${endDate}`
                    );
                    const data = await res.json();
                    results.push(data);
                }
                return results;
            };

            const fetchPropDisc = async () => {
                const res = await fetch(
                    `https://www.externalapi.com/api/rpts/prodisc?startDate=${startDate}&endDate=${endDate}`
                );
                const data = await res.json();
                return data;
            };

            const ttlSales = await fetchTtlSales(); // Await the result here
            const dptSales = await fetchDeptSales();
            const tndTtls = await fetchTenders();
            const taxTtls = await fetchTax();
            const propDisc = await fetchPropDisc();

            return {
                ttlSales: ttlSales,
                dptSales: dptSales,
                tndTtls: tndTtls,
                taxTtls: taxTtls,
                propDisc: propDisc
            };
        } catch (err) {
            console.error(err);
            return fail(500, { message: 'Could not get data' });
        }
    }
};

This is my +page.svelte:

<script lang="ts">
    // @ts-nocheck

    import Navbar from '$lib/components/Navbar.svelte';
    import { enhance } from '$app/forms';
    export let data;
    $: ({ name, ttlSales, dptSales, tndTtls, taxTtls, propDisc } = data);

    const today = new Date().toISOString().slice(0, 10);

    let startDate = today;
    let endDate = today;

    /* async function reload(event) {
        event.preventDefault();
        await goto(`/sales?startDate=${startDate}&endDate=${endDate}`);
    } */

    $: totalDptSum = data.dptSales.reduce((ttl, eachTtl) => ttl + eachTtl.TOTAL, 0).toFixed(2);

    $: totalTndSum = data.tndTtls.reduce((ttl, eachTtl) => ttl + eachTtl.TOTAL, 0).toFixed(2);

    /*  const Tax1 = taxTtls
        .map((ttl) => ttl[0].TAX_1) // Get an array with possibly undefined values
        .filter((value) => typeof value === 'number') // Filter out undefined values
        .map((value) => value.toFixed(2)); // Format numbers to fixed 2 decimal places as strings

    const Tax2 = taxTtls
        .map((ttl) => ttl[0].TAX_2)
        .filter((value) => typeof value === 'number')
        .map((value) => value.toFixed(2));
    const Tax3 = taxTtls
        .map((ttl) => ttl[0].TAX_3)
        .filter((value) => typeof value === 'number')
        .map((value) => value.toFixed(2));

    $: totaltaxSum = parseFloat(Tax1) + parseFloat(Tax2) + parseFloat(Tax3); */
</script>

<body>
    <main class="container">
        <h1 class="noMarginBottom">
            Welcome, {name}!
        </h1>
        <Navbar />

        <form method="post" action="?/refresh" use:enhance>
            <p>Start date:</p>
            <input type="date" id="start" name="startDate" bind:value={startDate} />
            <p>End date:</p>
            <input type="date" id="start" name="endDate" bind:value={endDate} />
            <button class="searchButton" type="submit">Search</button>
        </form>

        <details open>
            <!-- svelte-ignore a11y-no-redundant-roles -->
            <summary role="button" class="contrast">Total Sales</summary>

            <h1>${ttlSales.TOTAL ? ttlSales.TOTAL : '0.00'}</h1>
        </details>
        <details open>
            <!-- svelte-ignore a11y-no-redundant-roles -->
            <summary role="button" class="contrast">Department Sales</summary>
            <table>
                <thead>
                    <tr>
                        <th scope="col">Name</th>
                        <th scope="col">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    {#each dptSales as sale}
                        <tr>
                            <td>{sale.F238 ? sale.F238 : 'N/A'}</td>
                            <td>${sale.TOTAL ? sale.TOTAL : '0.00'}</td>
                        </tr>
                    {/each}
                </tbody>
                <tfoot>
                    <tr>
                        <td scope="col"></td>
                        <td scope="col" style="color:chartreuse">${totalDptSum ? totalDptSum : '0.00'}</td>
                    </tr>
                </tfoot>
            </table>
        </details>
        <details open>
            <summary role="button" class="contrast">Tenders</summary>
            <table>
                <thead>
                    <tr>
                        <th scope="col">Name</th>
                        <th scope="col">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    {#each tndTtls as tender}
                        <tr>
                            <td>{tender.F1039 ? tender.F1039 : 'N/A'}</td>
                            <td>${tender.TOTAL ? tender.TOTAL : '0.00'}</td>
                        </tr>
                    {/each}
                </tbody>
                <tfoot>
                    <tr>
                        <td scope="col"></td>
                        <td scope="col" style="color:chartreuse">${totalTndSum ? totalTndSum : '0.00'}</td>
                    </tr>
                </tfoot>
            </table>
        </details>
        <details open>
            <summary role="button" class="contrast">Taxes</summary>
            <table>
                <thead>
                    <tr>
                        <th scope="col">Name</th>
                        <th scope="col">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Tax 1</td>
                        <!-- <td>${Tax1 ? Tax1 : '0.00'}</td> -->
                    </tr>
                    <tr>
                        <td>Tax 2</td>
                        <!-- <td>${Tax2 ? Tax2 : '0.00'}</td> -->
                    </tr>
                    <tr>
                        <td>Tax 3</td>
                        <!-- <td>${Tax3 ? Tax3 : '0.00'}</td> -->
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td scope="col"></td>
                        <!-- <td scope="col" style="color:chartreuse">${totaltaxSum}</td> -->
                    </tr>
                </tfoot>
            </table>
        </details>
        <details open>
            <summary role="button" class="contrast">Discounts</summary>
            <table>
                <thead>
                    <tr>
                        <th scope="col">Name</th>
                        <th scope="col">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Proportional Disc.</td>
                        <td>$10.00</td>
                    </tr>
                    <tr>
                        <td>Global Disc.</td>
                        <td>$20.00</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td scope="col"></td>
                        <td scope="col" style="color:chartreuse">$30.00</td>
                    </tr>
                </tfoot>
            </table>
        </details>
        <button class="download-excel">Excel</button>
        <button class="download-pdf">PDF</button>
    </main>
</body>

<style>
    details h1 {
        text-align: center;
        font-size: 300%;
    }
    .noMarginBottom {
        margin-bottom: 0px;
    }
    .searchButton {
        background-color: coral;
        border-color: coral;
    }
    .download-excel {
        background-color: green;
        border-color: green;
    }
    .download-pdf {
        background-color: crimson;
        border-color: crimson;
    }
</style>

I was looking at invalidate() function to refresh the load, but then it would reload my whole page and reset my startDate and endDate inputs back to the current date even though I had changed it to another date. I found that the invalidate() function takes away from the whole “single page app” concept.

I’m looking for the best way to do this and right now I don’t think I’m on the right track because I’m being redundant calling the API in my load function as well as in a named action.

Help would be greatly appreciate it šŸ™‚

AJAX Call Not Reaching PHP File in PHP MVC Project for Email Validation

I am working on a project using the PHP MVC pattern and I’m currently facing an issue with validating email input. My goal is to check if the email already exists in the database using JavaScript and AJAX. However, it seems that the JavaScript is not able to communicate with the PHP file designated for this validation.

Checked the network tab in the browser’s developer tools to see if the request is being made.

Verified the path to the PHP file in the AJAX call.

Ensured that the PHP file is accessible and working by directly accessing it in the browser.

My voice assistant website won’t work on mobile

I have been trying to make a voice assistant with the web speech api

it works fine on pc but it has very bad hearing on mobile

i have to say “hey hub” wait 3 seconds and say my command and even then half the time it never hears. me

here is my speech recognition code:

let listeningForCommand = false;
let lastTranscript = '';
const wakeWord = "hey hub";
const silenceDelay = 2000; // 2 seconds
let silenceTimer;
let messages = [];
const maxMessages = 7; // Maximum number of messages to keep


window.onload = function() {
    const savedMessages = localStorage.getItem('messages');
    if (savedMessages) {
        messages = JSON.parse(savedMessages);
      startRecognition()
       loadModels();
        initShakeDetection()
    }
    setGif('https://i.ibb.co/WxbwbD1/Normal-gif.gif'); // Set default GIF
    updateDateTime(); // Initialize date and time

    // Speak a greeting message
    responsiveVoice.speak("oh. im back! ", "Australian Male", {
        onstart: function() {
            // You can set a specific GIF for the greeting here if you like
            setGif('https://i.ibb.co/Lp3XTHs/Smiling-eyes-gif.gif');
        },
        onend: function() {
            setGif('https://i.ibb.co/WxbwbD1/Normal-gif.gif');
        }
    });
};

let recognition;
if ('SpeechRecognition' in window) {
    recognition = new SpeechRecognition();
} else if ('webkitSpeechRecognition' in window) {
    recognition = new webkitSpeechRecognition();
} else {
    console.error('Your browser does not support SpeechRecognition.');
}

recognition.lang = 'en-US'; // Set language
recognition.continuous = true;
recognition.interimResults = true;

recognition.onresult = function(event) {
    clearTimeout(silenceTimer);

    const currentTranscript = Array.from(event.results)
                                   .map(result => result[0].transcript)
                                   .join('');

    if (!listeningForCommand && currentTranscript.toLowerCase().includes(wakeWord)) {
        listeningForCommand = true;
        lastTranscript = '';
        setGif('https://i.ibb.co/fn40PJm/I-hear-you.gif');
        addSystemMessage();
    } else if (listeningForCommand) {
        lastTranscript = currentTranscript;
        silenceTimer = setTimeout(() => {
            const commandAfterWakeWord = lastTranscript.substring(lastTranscript.toLowerCase().indexOf(wakeWord) + wakeWord.length).trim();
            if (commandAfterWakeWord) {
                setGif('https://i.ibb.co/B2JmjJc/Thinking.gif');
                sendCommandToAPI(commandAfterWakeWord);
            } else {
                setGif('https://i.ibb.co/x1XZHHm/Huh.gif');
            }
            listeningForCommand = false;
            recognition.stop(); // Stop recognition while processing the command
        }, silenceDelay);
    }
};

recognition.onerror = function(event) {
    if (event.error === 'no-speech') {
        console.error('No speech detected.');
        restartRecognition();
    }
};

function restartRecognition() {
    if (recognition) {
        recognition.stop();
        setTimeout(() => {
            startRecognition();
        }, 1000);
    }
}

recognition.start();

I tried on both pc and mobile on 4 deferent devices

it works fine on pc but it has very bad hearing on mobile

i have to say “hey hub” wait 3 seconds and say my command and even then half the time it never hears. me

How to update params from parent component to all child screen in order to have updated data in react native

I have a components which basically rely on params they receive, they don’t have any internal state, I want whenever there is a change in data in their parent component from where the params are being passed i want to update that data in params too so all the component who has access to that particular params should have the updated one’s.

I tried to update params through setParams in my parent component whenever there is change but the components who are dependent on that params doesn’t recieve the updated params, infact their parent has the updated data. I want if somebody has done this before and what could be the best strategy

React Native making calls after navigating to another page

const update=async()=>{
        
        setUpdatedUser({
          ...updatedUser,
          userType:user?.userType,
          hostStatus:(user?.userType=='host')?true:false,
          verificationStatus:false
        })
        await actors.userActor?.updateUserInfo(updatedUser).then(async(res)=>{
            console.log(res)
            alert('processing')
            alert(`Your profile is updated ${updatedUser?.firstName} !`)
            await actors.userActor?.getUserInfo().then((res)=>{
                dispatch(setUser(res[0]))
                console.log("response user",res[0])
                setEditProfile(false)
            })
        })
        console.log("request user",updatedUser)
    }

I am facing an issue with asynchronous handling of functions, I am trying to execute my update function on a button click but it is not returning any response or error but when I am navigating to another screen I am getting my responses, it seems like update is executing when I am navigating to another screen. Why is it happening and how to solve this?
I am using redux toolkit for state management and I have verified my backend as well where I am making my calls

Purpose of interface in react native

how to add interface with functional components in react native? what is the difference between interface and types? Can we pass interface as props from one component to another component? what are the advantages of using interface over types in react native typescript?

Why wont css for hr tag show up in react code?

I am using this code to create a border in my react code:

<hr className="borderLine" />

however, when styling it with css, I am using this code:

.divider {
    margin: 2rem;
    width: 100%;
    color: white;
    border: 1px solid white; /* Adjust the color and style as needed */
  }

This issue is that when I use .borderLine for the css, it doesn’t show up. The hr line only shows up on the page using .divider, but no class is named .divider.

If anyone could explain why that is I’d appreciate it.

I have tried renaming all of the className’s but it did not make a difference, it just didn’t show up unless I used .divider. I even cleared my cache.

I am trying to create an object with 2 separate arrays in it , is there a way to create 2 objects from these arrays in the new object i create

I am looking to create a single object by adding 2 arrays into the object. the individual arrays will have a name e.g. one will be sent and one will be received, so essentially the end result will look like:

sent is the name of the first array which is saved as a variable and received is the name of the other array saved as a variable

{
sent :[array of objects],
received: [array of objects]
}

Below is the code for the 2 separate calls before i get downvoted to oblivion.

Essentially im trying to fetch whats received and whats sent in terms of posts by a user and display them on the same page in 1 call so once i get it on the frontend, from there ill figure out how to display this separately (shouldnt be much of an issue)

const sentSuggestions = Suggestion.find({author})
    .populate({path: 'author', select: 'username'})
    const receivedSuggestions = Suggestion.find({creatorId})
    .populate({path: 'creatorId', select: 'username'})

    const sent = sentSuggestions.exec()
    const received = receivedSuggestions.exec()

this is an idea of what each call produces:

[
  {
    _id: new ObjectId('658b55b10800a691b4373e05'),
    title: 'blah blah blah',
    description: 'blah b;lah blah',
    reserveAmount: null,
    author: {
      _id: new ObjectId('658b54d55c87f13cbd70c63c'),
      username: 'fredpll100'
    },
    creatorId: new ObjectId('658a26fc5c87f13cbd2032d5'),
    likes: [],
    createdAt: 2023-12-26T22:37:37.040Z,
    updatedAt: 2023-12-26T22:37:37.040Z,
    __v: 0
  },
  {
    _id: new ObjectId('658b56010800a691b4373e09'),
    title: 'blah blah blah',
    description: 'blah blah blah',
    reserveAmount: null,
    author: {
      _id: new ObjectId('658b54d55c87f13cbd70c63c'),
      username: 'fredpll100'
    },
    creatorId: new ObjectId('658a26fc5c87f13cbd2032d5'),
    likes: [],
    createdAt: 2023-12-26T22:38:57.182Z,
    updatedAt: 2023-12-26T22:38:57.182Z,
    __v: 0
  }
]

I passed an entire JSON DB to the gpt-4-1106-preview api but it’s costing me a lot. How can I optimize it?

So, I have the following real estate db:

[
  {
    "Bodegas": [
      {
        "file": "....pdf",
        "Tipo Operacion": "Renta",
        "Zona": "HUMBOLDT",
        "Presupuesto Renta": 24000
      },
      {
        "file": "B_R_V_HUMBOLDT (a-s) 715.pdf",
        "Tipo Operacion": "Renta y Venta",
        "Zona": "HUMBOLDT",
        "Presupuesto Renta": 60000,
        "Presupuesto Venta": 11000000
      }
    ]
  },
  {
    "Houses": [
      {
        "file": "....pdf",
        "Tipo Operacion": "Renta",
        "Zona": "Camino Real Olmedillas",
        "Presupuesto Renta": 13500,
        "Recamaras": 3,
        "Amueblado": false,
        "Mascotas": false,
        "Amenidades": [
          "Cochera 2 autos",
          "2.5 baƱos",
          "Cocina",
          "Sala de TV",
          "Cuarto de lavado",
          "Cuarto de servicio",
          "Bar"
        ]
      },
      {
        "file": "....pdf",
        "Tipo Operacion": "Renta",
        "Zona": "El Carmen",
        "Presupuesto Renta": 37000,
        "Recamaras": 4,
        "Amueblado": false,
        "Mascotas": false,
        "Amenidades": [
          "Cochera 2 autos",
          "3.5 baƱos",
          "Cocina",
          "Sala de TV",
          "Ɓrea de servicio",
          "2 patios",
          "Biblioteca",
          "Estudio"
        ]
      },
      ...

And I’m passing it to the gpt-4-1106-preview api like this:

const fs = require('fs');  // Assuming 'fs' module is required in the actual code

const jsonFile = fs.readFileSync("./db.json", "utf8");
const db = JSON.stringify(jsonFile);

async function sendMessage(message) {
  const chatCompletion = await openai.chat.completions.create({
    messages: [
      {
        role: "system",
        content: `
        
        Based on the user's message, choose one of the following options and return only the name of the catalog file that corresponds to the user's search.

        You can return multiple files depending on the user's search:
        
        ${db}
        
        If no file is found, or the user is selling, offering, or it is noticed that they do not request or want a property, return the following message: nothing 

        So ignore the following words: offer, sell, offers, sells, referred, ref 
        
        `,
      },
      {
        role: "user",
        content: message,
      },
    ],
    model: "gpt-4-1106-preview",
  });

  return chatCompletion.choices[0].message.content;
}

As you can imagine, this implementation works like a charm for retrieving and searching properties. However, the caveat is that the costs are massive.

How can I reduce the costs or have a context of the db? Pinecone? Langchain?

Upload video to S3 with React-Native

I am trying to include a video upload functionality in my react-native app. I have gone through the many tutorials and questions on here about the issue but I think the problem I’m having is different than what I’ve seen. The videos are being recorded and saved using a native component I created. I’m developing for android at the moment, and I’m saving the videos using a MediaRecorder object, and then the component sends the filepath back to react native code for storage. this seems to work fine for playing videos in react-native but I also want to upload the videos to s3 and be able to watch the video on the AWS console. When i go to play the videos in the AWS Console i get an error “No video with supported format and mimetype found.” Which leads me to believe that when the files are being uploaded, there’s an encoding issue. Here is the code snippet that i believe got me closest to success


  const s3 = new AWS.S3()

  const uploadFileToS3 = (dat) => {
    const params = {
      Bucket: bucket,
      Key: keyName,
      Body: dat,
      ContentType: 'video/mp4', 
    }

    return s3.upload(params).promise();
  }

  try{
    console.log("Here! " + filename)
    const file = await RNFetchBlob.fs.readFile(filename);

    console.log("Starting upload! ")
    await uploadFileToS3(file);
    console.log("Finished upload! ")
      
  } catch (err) {
      console.error(err);
  }

Ive tried using PutObjectCommand and S3Client from “@aws-sdk/client-s3”, Ive tried multipart uploads, and aws’ amplify library. All of which i would ultimately get stuck at “No video with supported format and mimetype found.” I also tried RNS3 from ‘react-native-aws3’ and that could not correctly read the filepath that i had stored from the android component.

Why won’t my AJAX Contact Form stay on the same page?

I have a contact form on my site that I originally set up over 5 years ago. I remember that it used to display error/success messages directly on the page in the #form-messages div, but now it changes the page to send.php with the unformatted text displayed instead of staying on the contact form page and outputting it there, and I can’t for the life of me figure out why. Shouldn’t the event.preventDefault(); prevent the default behavior of changing the page?

Here’s the relevant HTML:

<form id="ajax-contact" method="post" action="send.php">
    <div class="field">
        <label for="name">Name</label>
        <input type="text" id="name" name="name" autocomplete="name" required>
    </div>

    <div class="field">
        <label for="email">Email</label>
        <input type="email" id="email" name="email" autocomplete="email" required>
    </div>

    <div class="field">
        <label for="message">Message</label>
        <textarea id="message" name="message" required></textarea>
    </div>
    <div class="field">
        <button type="submit" class="button g-recaptcha" data-sitekey="X" data-callback='onSubmit' data-action='submit'>Send</button>
    </div>
</form>
<div id="form-messages"></div>

Here’s the contact.js:

$(function() {
    // Get the form.
    var form = $('#ajax-contact');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    form.submit(function(event) {
        // Stop the browser from submitting the form.
        event.preventDefault();

        // Serialize the form data.
        var formData = form.serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: form.attr('action'),
            data: formData,
            captcha: grecaptcha.getResponse()

        }).done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            formMessages.removeClass('error');
            formMessages.addClass('success');

            // Set the message text.
            if (data.responseText !== '') {
                formMessages.text(data.responseText);
            } else {
                formMessages.text('Oops! An error occurred and your message could not be sent.');
            }

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
        }).fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            formMessages.removeClass('success');
            formMessages.addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                formMessages.text(data.responseText);
            } else {
                formMessages.text('Oops! An error occurred and your message could not be sent.');
            }
        });
    });
});

And here’s the send.php:

<?php
// If the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // If the Google Recaptcha box was clicked
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        $captcha=$_POST['g-recaptcha-response'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=X&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        $obj = json_decode($response);

        // If the Google Recaptcha check was successful
        if($obj->success == true) {
          // Clean up the data
          $name = strip_tags(trim($_POST["name"]));
          $name = str_replace(array("r","n"),array(" "," "),$name);
          $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
          $message = trim($_POST["message"]);

          // Check for empty fields
          if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
          }

          // Set up the email to me
          $email_sender = "[email protected]";
          $email_receiver = "[email protected]";
          $subject = "New message from $name";
          $email_content = "Name: $namenEmail: $emailnnMessage:n$messagen";
          $email_headers = "From: $name <$email_sender>" . "rn" . "Reply-To: $name <$email>";

          // Set up the confirmation email
          $confirm_content =  "Hi $name,nnI'll get back to you as soon as I can. For your convenience, here is a copy of the message you sent:nn----------nn$messagen";
          $confirm_headers = "From: My Name <$email_sender>"  . "rn" . "Reply-To: My Name <$email_receiver>";

          // Send the email to me
          if (mail($email_receiver, $subject, $email_content, $email_headers)) {
            http_response_code(200);
            echo "Thank You! Your message has been sent, and you should have received a confirmation email. I'll get back to you as soon as I can!";
            // Send the confirmation email
            mail($email, "Thank you for your message!", $confirm_content, $confirm_headers);
          } 
          // If the server was unable to send the mail
          else {
            http_response_code(500);
            echo "Oops! Something went wrong, and we couldn't send your message. Please try again.";
          }
      } 
      // If the Google Recaptcha check was not successful    
      else {
        http_response_code(400);
        echo "Robot verification failed. Please try again.";
      }
  } 
  // If the Google Recaptcha box was not clicked   
  else {
    http_response_code(400);
    echo "Please click the reCAPTCHA box.";
  }      
} 
// If the form was not submitted
// Not a POST request, set a 403 (forbidden) response code.         
else {
  http_response_code(403);
  echo "There was a problem with your submission, please try again.";
}      
?>

AJAX Contact Form directs to white page rather than staying on same page

I have a contact form on my site that I originally set up over 5 years ago. I remember that it used to display error/success messages directly on the page in the #form-messages div, but now it changes the page to send.php with the unformatted text displayed instead of staying on the contact form page and outputting it there, and I can’t for the life of me figure out why. Shouldn’t the event.preventDefault(); prevent the default behavior of changing the page?

Here’s the relevant HTML:

<form id="ajax-contact" method="post" action="send.php">
    <div class="field">
        <label for="name">Name</label>
        <input type="text" id="name" name="name" autocomplete="name" required>
    </div>

    <div class="field">
        <label for="email">Email</label>
        <input type="email" id="email" name="email" autocomplete="email" required>
    </div>

    <div class="field">
        <label for="message">Message</label>
        <textarea id="message" name="message" required></textarea>
    </div>
    <div class="field">
        <button type="submit" class="button g-recaptcha" data-sitekey="X" 
        data-callback='onSubmit' data-action='submit'>Send</button>
    </div>
</form>
<div id="form-messages"></div>

Here’s the contact.js:

$(function() {
    // Get the form.
    var form = $('#ajax-contact');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(event) {
        // Stop the browser from submitting the form.
        event.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData,
            captcha: grecaptcha.getResponse()
        }).done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occurred and your message could not be sent.');
            }

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
        }).fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occurred and your message could not be sent.');
            }
        });
    });
});

And here’s the send.php:

<?php
// If the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // If the Google Recaptcha box was clicked
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        $captcha=$_POST['g-recaptcha-response'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=X&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        $obj = json_decode($response);

        // If the Google Recaptcha check was successful
        if($obj->success == true) {
          // Clean up the data
          $name = strip_tags(trim($_POST["name"]));
          $name = str_replace(array("r","n"),array(" "," "),$name);
          $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
          $message = trim($_POST["message"]);

          // Check for empty fields
          if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
          }

          // Set up the email to me
          $email_sender = "[email protected]";
          $email_receiver = "[email protected]";
          $subject = "New message from $name";
          $email_content = "Name: $namenEmail: $emailnnMessage:n$messagen";
          $email_headers = "From: $name <$email_sender>" . "rn" . "Reply-To: $name <$email>";

          // Set up the confirmation email
          $confirm_content =  "Hi $name,nnI'll get back to you as soon as I can. For your convenience, here is a copy of the message you sent:nn----------nn$messagen";
          $confirm_headers = "From: My Name <$email_sender>"  . "rn" . "Reply-To: My Name <$email_receiver>";

          // Send the email to me
          if (mail($email_receiver, $subject, $email_content, $email_headers)) {
            http_response_code(200);
            echo "Thank You! Your message has been sent, and you should have received a confirmation email. I'll get back to you as soon as I can!";
            // Send the confirmation email
            mail($email, "Thank you for your message!", $confirm_content, $confirm_headers);
          } 
          // If the server was unable to send the mail
          else {
            http_response_code(500);
            echo "Oops! Something went wrong, and we couldn't send your message. Please try again.";
          }
      } 
      // If the Google Recaptcha check was not successful    
      else {
        http_response_code(400);
        echo "Robot verification failed. Please try again.";
      }
  } 
  // If the Google Recaptcha box was not clicked   
  else {
    http_response_code(400);
    echo "Please click the reCAPTCHA box.";
  }      
} 
// If the form was not submitted
// Not a POST request, set a 403 (forbidden) response code.         
else {
  http_response_code(403);
  echo "There was a problem with your submission, please try again.";
}      
?>