-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
175 lines (157 loc) · 5.09 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Instagram Embed component for React Native
* https://github.com/GaborWnuk
* @flow
*/
import React, { PureComponent } from 'react';
import { View, Image, Text } from 'react-native';
import styles from './index-styles';
export default class InstagramEmbed extends PureComponent {
constructor(props) {
super(props);
this.state = {
response: null,
height: 240,
width: 320,
avatar: null,
likes: 0,
views: 0,
comments: 0,
thumbnail: null,
};
}
_onLayout = layout => {
this.setState({
height: layout.nativeEvent.layout.height,
width: layout.nativeEvent.layout.width,
});
};
/*
* This is fairly experimental and probably not the best way to supplement
* existing API (official) data with missing properties we need.
*/
_fetchComplementaryData = url => {
let regex = /\/p\/([a-zA-Z0-9]+)/g;
let match = regex.exec(url);
const id = match[1];
if (!id) {
return;
}
fetch(`https://www.instagram.com/p/${id}/embed/captioned/`)
.then(response => response.text())
.then(responseText => {
let avatarRegex = /class\=\"ehAvatar\"\s+src=\"([a-zA-Z0-9\-\\\:\/\.\_]+)\"/g;
let avatarMatch = avatarRegex.exec(responseText);
let likesRegex = /span\s+class\=\"espMetricTextCollapsible\"><\/span>([0-9\,\.km]+)<span\s+class\=\"espMetricTextCollapsible\">\s+likes?/g;
let likesMatch = likesRegex.exec(responseText);
let viewsRegex = /span\s+class\=\"espMetricTextCollapsible\"><\/span>([0-9\,\.km]+)<span\s+class\=\"espMetricTextCollapsible\">\s+views?/g;
let viewsMatch = viewsRegex.exec(responseText);
let commentsRegex = /span\s+class\=\"espMetricTextCollapsible\"><\/span>([0-9\,\.km]+)<span\s+class\=\"espMetricTextCollapsible\">\s+comments?/g;
let commentsMatch = commentsRegex.exec(responseText);
let thumbnailRegex = /class\=\"efImage\"\s+src=\"([a-zA-Z0-9\-\\\:\/\.\_]+)\"/g;
let thumbnailMatch = thumbnailRegex.exec(responseText);
this.setState({
thumbnail: thumbnailMatch ? thumbnailMatch[1] : null,
avatar: avatarMatch ? avatarMatch[1] : null,
likes: likesMatch ? likesMatch[1] : null,
views: viewsMatch ? viewsMatch[1] : null,
comments: commentsMatch ? commentsMatch[1] : null,
});
})
.catch(error => {});
};
componentDidMount = () => {
const { url } = this.props;
fetch(`https://api.instagram.com/oembed/?url=${url}`)
.then(response => response.json())
.then(responseJson => {
this._fetchComplementaryData(url);
this.setState({ response: responseJson });
})
.catch(error => {
this.setState({ response: null });
});
};
render(): JSX.JSXElement {
const { style } = this.props;
const {
response,
height,
width,
avatar,
likes,
comments,
thumbnail,
views,
} = this.state;
if (!response) {
return <View style={[{ width: 0, height: 0 }, style]} />;
}
return (
<View
style={[
styles.container,
style,
{
height: height,
},
]}
>
<View onLayout={this._onLayout}>
<View style={styles.headerContainer}>
{avatar && (
<Image
source={{
uri: avatar,
}}
style={styles.avatar}
/>
)}
<Text style={styles.author}>{response.author_name}</Text>
</View>
{!!thumbnail && (
<Image
source={{ uri: thumbnail }}
style={{
height:
response.thumbnail_height * width / response.thumbnail_width,
}}
/>
)}
<View style={{ flexDirection: 'column', margin: 8 }}>
<View style={styles.statsContainer}>
{!!views && (
<View style={{ flexDirection: 'row' }}>
<Image
source={require('./assets/images/icon_views.png')}
style={styles.statIcon}
/>
<Text style={styles.statLabel}>{views} views</Text>
</View>
)}
{!!likes && (
<View style={{ flexDirection: 'row' }}>
<Image
source={require('./assets/images/icon_likes.png')}
style={styles.statIcon}
/>
<Text style={styles.statLabel}>{likes} likes</Text>
</View>
)}
{!!comments && (
<View style={{ flexDirection: 'row' }}>
<Image
source={require('./assets/images/icon_comments.png')}
style={styles.statIcon}
/>
<Text style={styles.statLabel}>{comments} comments</Text>
</View>
)}
</View>
<Text>{response.title}</Text>
</View>
</View>
</View>
);
}
}