├── README.md ├── app.js ├── package.json └── views └── index.ejs /README.md: -------------------------------------------------------------------------------- 1 | # Reddit-api-random-image 2 | Use the reddit api to take pictures 3 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const ejs = require('ejs'); 3 | const app = express(); 4 | const snekfetch = require('node-fetch'); 5 | // EJS 6 | app.set('view engine', 'ejs'); 7 | /*app.get('/', (req, res) => res.render('index')); 8 | */ 9 | 10 | 11 | app.get('/', async(req, res) => { 12 | var facts = ["burdurland", "TurkeyJerky", "KGBTR", "bgy", "duznamemes", "kucukinsanlaryoutube", "lanetliyorumlar", "ShitpostTC", "tamamahbapengelli", "Turkey"]; 13 | var fact = Math.floor(Math.random() * (facts.length - 0 + 1 ) + 0 ) 14 | var facts1 = ["hot", "new", "top"]; 15 | var fact1 = Math.floor(Math.random() * (facts.length - 0 + 1 ) + 0 ) 16 | var facts2 = ["hour", "day", "week", "month", "year", "all"]; 17 | var fact2 = Math.floor(Math.random() * (facts.length - 0 + 1 ) + 0 ) 18 | const body = await snekfetch(`https://www.reddit.com/r/${facts[fact]}.json?sort=${facts1[fact1]}&t=${facts2[fact2]}`) 19 | .then(response => response.json()); 20 | const allowed = body.data.children 21 | const randomnumber = Math.floor(Math.random() * allowed.length) 22 | console.log(allowed[randomnumber].data.url) 23 | res.render('index', { 24 | Resim: `${allowed[randomnumber].data.url}`, 25 | boy: `${allowed[randomnumber].height}`, 26 | wile: `${allowed[randomnumber].width}` 27 | }); 28 | }); 29 | 30 | app.get('/:sub', async(req, res) => { 31 | var subs = req.params.sub 32 | var facts1 = ["hot", "new", "top"]; 33 | var fact1 = Math.floor(Math.random() * (facts1.length - 0 + 1 ) + 0 ) 34 | var facts2 = ["hour", "day", "week", "month", "year", "all"]; 35 | var fact2 = Math.floor(Math.random() * (facts2.length - 0 + 1 ) + 0 ) 36 | const body = await snekfetch(`https://www.reddit.com/r/${subs}.json?sort=${facts1[fact1]}&t=${facts2[fact2]}`) 37 | .then(response => response.json()); 38 | const allowed = body.data.children 39 | const randomnumber = Math.floor(Math.random() * allowed.length) 40 | res.render('index', { 41 | Resim: `${allowed[randomnumber].data.url}`, 42 | boy: `${allowed[randomnumber].height}`, 43 | wile: `${allowed[randomnumber].width}` 44 | }); 45 | }); 46 | 47 | //width=<%- wile ? wile :`1000`%> height=<%- boy ? boy :`600`%> 48 | 49 | const port = 3000; 50 | 51 | app.listen(port, () => console.log(`Port: ${port}`)); 52 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "reddit-api", 3 | "version": "0.0.1", 4 | "description": "Reddit api.", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "node app.js" 8 | }, 9 | "dependencies": { 10 | "express": "^4.17.1", 11 | "node-fetch": "^2.6.0", 12 | "ejs": "^3.1.3" 13 | }, 14 | "engines": { 15 | "node": "12.x" 16 | }, 17 | "repository": { 18 | "url": "https://github.com/Furtsy/Reddit-api-random-image" 19 | }, 20 | "license": "MIT", 21 | "keywords": [ 22 | "node", 23 | "glitch", 24 | "express", 25 | "Furtsy", 26 | "Reddit" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |