-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
147 lines (138 loc) · 4.72 KB
/
App.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import cheerio from "cheerio-without-node-native";
import * as React from "react";
import {
Dimensions,
StyleSheet,
View,
Image,
Text,
ActivityIndicator,
FlatList,
TouchableOpacity,
Linking,
} from "react-native";
const { width, height } = Dimensions.get("window");
function extractHostname(url) {
var hostname;
if (url?.indexOf("//") > -1) {
hostname = url?.split("/")[2];
} else {
hostname = url?.split("/")[0];
}
hostname = hostname?.split(":")[0];
hostname = hostname?.split("?")[0];
return hostname?.replace("www.", "");
}
const sample = [
"https://tribune.com.pk/story/2310940/pakistan-sees-crypto-boom",
"https://www.thenews.com.pk/latest/865493-daily-covid-19-report-2783-test-positive-for-coronavirus-in-pakistan",
"https://www.dawn.com/news/1635491",
"https://tribune.com.pk/story/2310856/extremely-unfair-to-blame-pakistan-for-afghan-unrest",
"https://www.bbc.com/news/world-asia-57877239",
"https://www.dawn.com/news/1635301",
"https://www.bbc.co.uk/sport/football/57836300",
"https://www.bavarianfootballworks.com/2021/7/17/22581051/koln-vs-bayern-munich-lineups-live-stream-how-to-watch-international-friendly-updates-highlights",
"https://www.geo.tv/latest/360535-iggy-azalea-going-on-hiatus-in-music-career-will-be-away-a-few-years",
"https://www.thenews.com.pk/latest/865060-sajal-aly-atif-aslams-rafta-rafta-music-video-release-date-confirmed",
"https://www.reuters.com/world/europe/music-banned-greeces-mykonos-new-covid-19-restrictions-2021-07-17/",
"https://www.theguardian.com/tv-and-radio/2021/jul/14/an-american-riddle-the-black-music-trailblazer-who-died-white-harry-pace",
"https://www.nbcnews.com/media/netflix-eyes-foray-video-games-rcna1425",
"https://www.katymagazineonline.com/post/musicians-spread-the-love-of-music-at-a-local-katy-nonprofit",
"https://www.npr.org/2021/07/13/1015630514/new-music-friday-the-top-8-albums-out-on-july-16",
"https://www.al.com/news/huntsville/2021/07/city-of-huntsville-hiring-music-officer-with-salary-up-to-89211.html",
"https://www.wric.com/business/record-store-day-brings-lines-of-customers-to-local-shops-music-means-so-much-to-people/",
];
const App = () => {
const [data, setData] = React.useState(sample);
const [loading, setLoading] = React.useState(false);
const fetchArticles = async () => {
let state = [];
setLoading(true);
for (const i of sample) {
const response = await fetch(i); // fetch page
const htmlString = await response.text(); // get response text
const $ = cheerio.load(htmlString); // parse HTML string
let local = { title: "", image: "", url: "", description: "" };
for (const i of Object.keys(local)) {
local[i] = $(`meta[property="og:${i}"]`).attr("content")?.trim();
}
state.push(local);
}
setData(state);
setLoading(false);
};
React.useEffect(() => {
fetchArticles();
}, []);
const renderItem = ({ item, index }) => (
<TouchableOpacity style={styles.view} activeOpacity={1} onPress={async () => await Linking.openURL(item.url)}>
<Image source={{ uri: item.image }} style={styles.image} />
<Text style={styles.title}>{item.title}</Text>
<Text numberOfLines={2} style={styles.desc}>
{item.description}
</Text>
<Text numberOfLines={2} style={styles.website}>
{extractHostname(item.url)}
</Text>
</TouchableOpacity>
);
return (
<View style={styles.container}>
{loading ? (
<ActivityIndicator animating size="large" />
) : (
<FlatList
data={data}
ListHeaderComponent={<Text style={styles.header}>{"react-native-fetch-news"}</Text>}
keyExtractor={(item, index) => item + index.toString()}
renderItem={renderItem}
contentContainerStyle={styles.contentContainerStyle}
/>
)}
</View>
);
};
export default App;
const styles = StyleSheet.create({
header: { fontSize: 30, marginHorizontal: 10, fontWeight: "bold" },
contentContainerStyle: { paddingVertical: 80 },
website: {
fontWeight: "400",
fontSize: 12,
color: "gray",
paddingHorizontal: 10,
marginVertical: 10,
},
desc: {
fontWeight: "400",
fontSize: 15,
paddingHorizontal: 10,
marginVertical: 5,
},
title: {
fontWeight: "700",
fontSize: 17,
paddingHorizontal: 10,
marginVertical: 5,
},
image: {
width: width * 0.95,
height: height * 0.25,
borderTopLeftRadius: 15,
},
view: {
borderWidth: 1,
borderColor: "#d3d3d3",
borderRadius: 15,
flexGrow: 0,
marginHorizontal: 5,
marginVertical: 10,
width: width * 0.95,
},
container: {
flex: 1,
backgroundColor: "#fff",
justifyContent: "center",
alignItems: "center",
},
});