forked from codemaniac-sahil/news-webapp-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
31 lines (25 loc) · 1.25 KB
/
script.js
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
// https://gnews.io/api/v4/top-headlines?token=a175ac9bf19a19e7e95f50136ebffcbb&country=in&lang=en
//https://newsapi.org/v2/top-headlines?sources=bbc-news&apiKey=efbe6953b5e24dc7857c47ac7e35e650
async function getgeolocation(){
let url='https://ipapi.co/json';
let response=await fetch(url)
let data = await response.json()
var current_location = data.country_code
var current__lang = data.languages;
document.getElementById("country").innerHTML = data.country_name;
async function getnews(){
let full_url ='https://gnews.io/api/v4/top-headlines?token=a175ac9bf19a19e7e95f50136ebffcbb&country=' + current_location.toLowerCase()+ '&lang=' + current__lang.toLowerCase();
console.log(full_url)
let response1 = await fetch(full_url);
let data1 = await response1.json()
for(var i=0;i<data1.articles.length;i++){
document.getElementById("news").innerHTML+=` <div class='news'><p>${data1.articles[i].title} <a class="read" href='${data1.articles[i].url}'> Read full article</p>
<div class='img'><img src=${data1.articles[i].image}>
</div>
<hr>
`
}
}
getnews()
}
getgeolocation()