· 5 years ago · Feb 27, 2021, 11:40 AM
1import React, { useState } from "react";
2
3import axios from "axios";
4
5var shortUrl = require("node-url-shortener");
6
7function App() {
8 const [topic, setTopic] = useState("");
9 const [articles, setArticles] = useState([]);
10 const [hashtags, setHashtags] = useState([]);
11
12 const getArticles = async () => {
13 // Create new Date instance
14 var date = new Date();
15
16 // Add a day
17 date.setDate(date.getDate() - 1);
18
19 let res = await axios.get(
20 "http://newsapi.org/v2/everything?q=" +
21 topic +
22 "&from=" +
23 date +
24 "&to=" +
25 new Date() +
26 "&sortBy=popularity&apiKey=" +
27 "INSERT YOUR API KEY HERE!!!" //like, seriously
28 );
29
30 let arts = res.data.articles;
31
32 for (let i = 0; i < arts.length; i++) {
33 const element = arts[i].url;
34 await shortUrl.short(element, function (err, url) {
35 arts[i].url = url;
36
37 if (i == arts.length - 1) setArticles(arts);
38 });
39 }
40 };
41
42 const getHashtags = async () => {
43 let URL = `https://www.instagram.com/web/search/topsearch/?context=blended&query=${topic}`;
44
45 let tags = [];
46 let res = await axios.get(URL);
47 res.data.hashtags.forEach((element) => {
48 console.log(element.hashtag.name);
49 tags.push("#" + element.hashtag.name);
50 });
51 setHashtags(tags);
52 };
53
54 return (
55 <React.Fragment>
56 <input
57 type="text"
58 id="topic"
59 name="topic"
60 onChange={(e) => setTopic(e.target.value)}
61 ></input>
62 <button
63 onClick={() => {
64 getArticles();
65 }}
66 >
67 GO
68 </button>
69 <button
70 onClick={() => {
71 getHashtags();
72 }}
73 >
74 Hashtags
75 </button>
76 <br />
77 {articles.map((article, i) => (
78 <div key={i}>
79 {article.url && (
80 <div>
81 <p>{article.title + " - " + article.url}</p>
82 </div>
83 )}
84 </div>
85 ))}
86 {hashtags.map((ht, i) => (
87 <p key={i}>{ht}</p>
88 ))}
89 </React.Fragment>
90 );
91}
92
93export default App;
94
95
96//ENJOY!