Featured image of post Creating a Pinterest Affiliate Marketing Automation Tool using GPT-3.5 Turbo (Part 1)

Creating a Pinterest Affiliate Marketing Automation Tool using GPT-3.5 Turbo (Part 1)

In this multipart series, we will explore how to automate affiliate marketing by generating engaging Pinterest descriptions using GPT-3.5 Turbo and automating the pin creation process. This is Part 1, where we’ll cover using GPT-3.5 Turbo to generate descriptions and processing the input CSV file.

Check out my Pinterest pins to see the results of this automation tool in action.

Introduction

Affiliate marketing can be a lucrative way to earn passive income. One popular platform for promoting affiliate links is Pinterest. However, manually creating and managing pins can be time-consuming. In this blog post, we will discuss how to automate this process using OpenAI’s GPT-3.5 Turbo and some other tools.

GPT-3.5 Turbo is a powerful language model developed by OpenAI. It can be used for various tasks, including generating human-like text. We will use it to create engaging and informative descriptions for our Pinterest pins.

Overview of the Project

Our project involves the following steps:

  1. Read data from a CSV file containing affiliate link names and URLs (obtained from Rakuten Advertising)
  2. Generate Pinterest descriptions for each link using GPT-3.5 Turbo
  3. Shorten the URLs using the Bitly API (To be covered in Part 2)
  4. Create pins on Pinterest using the Pinterest API (To be covered in Part 2)
  5. Check image availability for each pin (To be covered in Part 2)
  6. Update the original CSV file with the generated descriptions

In this first part of the series, we’ll focus on steps 1, 2, and 6.

Setting Up the Environment

Before we begin, make sure you have the following prerequisites installed:

  • Node.js
  • npm (Node Package Manager)

We’ll also be using the following libraries:

  • csv-parser
  • axios
  • csv-writer

To install these libraries, run the following command:

npm install csv-parser axios csv-writer

Reading Data from a CSV File

First, we need to read the data from the CSV file containing affiliate link names and URLs. We’ll use the csv-parser library to parse the file and store its content in an array.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
async function parseCSV(csvFile) {
    const data = [];

    return new Promise((resolve, reject) => {
        fs.createReadStream(csvFile)
            .pipe(csvParser())
            .on('data', (row) => {
                // Add 'Link Name' and 'URL' fields to the row object
                row['Link Name'] = row['LINK NAME'];
                row['URL'] = row['LINK CODE'].match(/href="(.*?)"/)[1];
                data.push(row);
            })
            .on('end', () => {
                resolve(data);
            })
            .on('error', (error) => {
                reject(error);
            });
    });
}

Generating Pinterest Descriptions with GPT-3.5 Turbo

Next, we’ll create a function that takes an affiliate link name as input and generates a Pinterest description using the GPT-3.5 Turbo model. This will help make your pins more engaging and informative, increasing the likelihood of users clicking on your affiliate links.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
async function generateDescription(linkName) {
    const prompt = `Generate a Pinterest description for the following link name: "${linkName}". The description should be engaging and informative, suitable for a Pinterest pin.`;
    const maxRetries = 3;
    let retries = 0;

    while (retries < maxRetries) {
        try {
            const response = await axios.post(openaiEndpoint, {
                model: 'gpt-3.5-turbo',
                messages: [
                    {
                        role: 'user',
                        content: prompt,
                    },
                ],
                temperature: 0.8,
            }, {
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${openaiApiKey}`,
                },
            });
            return response.data.choices[0].message.content.trim();
        } catch (error) {
            if (error.response && error.response.status === 429) {
                retries++;
                console.log(`Error 429: Too many requests. Retrying in ${2 ** retries} seconds...`);
                await sleep(1000 * (2 ** retries));
            } else {
                console.error(`Error while generating description: ${error}`);
                throw error;
            }
        }
    }

    throw new Error('Failed to generate description after maximum retries.');
}

Updating the Original CSV File

Finally, we’ll update the original CSV file with the generated descriptions using the csv-writer library.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
async function writeOutputCSV(row, filePath) {
    const csvWriter = createCsvWriter({
        path: filePath,
        append: true,
        header: [
            { id: 'Link Name', title: 'Link Name' },
            { id: 'URL', title: 'URL' },
            { id: 'DESCRIPTION', title: 'Description' },
            { id: 'PROCESSED', title: 'Processed' }
        ],
    });

    return csvWriter.writeRecords([{
        'Link Name': row['Link Name'],
        'URL': row['URL'],
        'DESCRIPTION': row['DESCRIPTION'],
        'PROCESSED': row['PROCESSED']
    }]);
}

Now, let’s put it all together in a main function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
async function main() {
    try {
        const csvFile = 'csv/links.csv';
        const csvData = await parseCSV(csvFile);
        console.log(csvData);

        for (const row of csvData) {
            await sleep(5000); // Add a 5-second delay between each API call
            const description = await generateDescription(row['LINK NAME']);
            console.log(`Generated description for ${row['LINK NAME']}: ${description}`);

            row['PROCESSED'] = 'Yes';
            row['DESCRIPTION'] = description;

            await writeOutputCSV(row, 'csv/results.csv');
        }

        console.log('Results CSV file updated with descriptions.');
    } catch (error) {
        console.error('Error:', error);
    }
}

In Part 2, we’ll cover the remaining steps, including shortening URLs using the Bitly API, creating pins on Pinterest, and checking image availability for each pin. Stay tuned for more on how to automate your Pinterest affiliate marketing efforts using GPT-3.5 Turbo!

Built with Hugo
Theme Stack designed by Jimmy