Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HarisChandio/8473abdc9ed40f32df09b9c5410586e7 to your computer and use it in GitHub Desktop.
Save HarisChandio/8473abdc9ed40f32df09b9c5410586e7 to your computer and use it in GitHub Desktop.
some main code for server node js
db connection and schema
require("dotenv").config();
const mongoose = require("mongoose");
const dbConnect = async () => {
try {
const db_url = process.env.DB_URL;
console.log(process.env.DB_URL)
await mongoose.connect(db_url);
console.log("database connected");
} catch (e) {
console.log(e);
}
};
module.exports = {dbConnect};
const mongoose = require("mongoose");
const podcastSchema = new mongoose.Schema({
podindex: {type: String, required: true},
title: { type: String, required: true },
description: { type: String, required: true },
author: { type: String, required: true },
transcript: { type: String },
summary: { type: String },
img: { type: String, required: true },
mp3: { type: String, required: true },
duration: { type: Number },
published: {type: Number},
});
const Podcast = mongoose.model("Podcasts", podcastSchema);
module.exports = {Podcast};
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
password: { type: String, required: true },
email: { type: String, required: true },
});
const User = mongoose.model("users", userSchema);
module.exports = {User};
//to fetch the podcast info from Podcast Index API
require("dotenv").config()
var apiKey = process.env.POD_APIKEY;
var apiSecret = process.env.POD_SECRETKEY;
console.log(apiKey)
console.log(apiSecret)
let crypto = require('crypto');
var apiHeaderTime = Math.floor(Date.now()/1000); //console.log(`apiHeaderTime=[${apiHeaderTime}]`);
var sha1Algorithm = "sha1";
var sha1Hash = crypto.createHash(sha1Algorithm);
var data4Hash = apiKey + apiSecret + apiHeaderTime.toString();
sha1Hash.update(data4Hash);
var hash4Header = sha1Hash.digest('hex');
console.log(`hash4Header=[${hash4Header}]`);
import('node-fetch').then(fetchModule => {
const fetch = fetchModule.default;
let options = {
method: "get",
headers: {
"X-Auth-Date": ""+apiHeaderTime,
"X-Auth-Key": apiKey,
"Authorization": hash4Header,
"User-Agent": "SuperPodcastPlayer/1.8"
},
};
var query = "745287";
var url = "https://api.podcastindex.org/api/1.0/podcasts/byfeedid?id=745287";
fetch(url, options)
.then(res => {
if (!res.ok) {
throw new Error('Network response was not ok');
}
return res.json();
})
.then(json => {
console.log(json);
})
.catch(err => {
console.error('Error fetching data:', err);
});
}).catch(err => {
console.error('Failed to import node-fetch:', err);
});
//to uploade transcripts on aws
require("dotenv").config();
const AWS = require("aws-sdk");
const Podcast = require("../db/models/Podcast");
const dbConnect = require("../db/db");
AWS.config.update({
accessKeyId: process.env.AWS_ACCESSKEY,
secretAccessKey: process.env.AWS_SECRETKEY,
});
const s3 = new AWS.S3();
const bucketName = "myawschatpod";
async function listObjects() {
try {
const data = await s3.listObjects({ Bucket: bucketName }).promise();
return data.Contents;
} catch (error) {
console.error("Error listing objects:", error);
throw error;
}
}
async function main() {
try {
await dbConnect();
const s3Objects = await listObjects();
for (let i = 0; i < s3Objects.length; i++) {
const query = s3Objects[i].Key.split("/")[1].split(".")[0];
const updatedPodcast = await Podcast.findOneAndUpdate(
{
podindex: query,
},
{
transcript: `https://myawschatpod.s3.eu-north-1.amazonaws.com/${s3Objects[i].Key}`,
},
{ new: true }
);
if (updatedPodcast)
console.log(`Transcript URL added to podcast ${updatedPodcast.title}`);
else console.log(`Podcast with podindex ${query} not found`);
}
console.log("Transcript URLs updated successfully.");
} catch (error) {
console.log("omain function error", error);
}
}
main();
//index file
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require('cors')
app.use(cors())
app.use(express.json());
const userRouter = require("./routes/user");
const podRouter = require("./routes/podcast")
const { dbConnect } = require("./db/db");
const PodcastIndexClient = require("podcastdx-client");
const {Podcast} = require("./db/models/Podcast");
const {User} = require("./db/models/User");
app.use("/api/v1/user", userRouter);
app.use("/api/v1/podcast", podRouter)
app.listen(3001, async () => {
await dbConnect();
console.log("server running on port 3000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment