Skip to content

Instantly share code, notes, and snippets.

@allanx2000
Created February 8, 2019 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanx2000/0bd16f5815c22ef18cb13033ea6fd950 to your computer and use it in GitHub Desktop.
Save allanx2000/0bd16f5815c22ef18cb13033ea6fd950 to your computer and use it in GitHub Desktop.
import React, {Component} from "react";
import {FlatList, Text, TextInput, Button, View, StyleSheet, Alert } from "react-native";
import RNFetchBlob from 'react-native-fetch-blob'
import DOMParser from 'react-native-html-parser';
const ANIME_SHOWS = "http://www.animeshow.tv/"
const PARSER = new DOMParser.DOMParser();
export default class AnimeShowsList extends React.Component {
constructor(props) {
super(props)
this.onPressButton = this.onPressButton.bind(this)
this.refreshShows = this.refreshShows.bind(this)
this.state = {
lastRefreshed: 0,
data: null,
statusMessage: null
}
}
onPressButton(item)
{
var fullUrl = ANIME_SHOWS + item;
this.props.onClick(fullUrl)
//console.log(item)
}
render() {
return (
<View style={styles.container}>
<Button title="Refresh" onPress={this.refreshShows}/>
{
this.state.data ?
<View>
<FlatList
data={this.state.data}
renderItem={({item}) => <Text onPress={() => this.onPressButton(item)} style={styles.resultsText}>{item}</Text>}
/>
</View>
:
<View>
<Text>{this.state.statusMessage}</Text>
</View>
}
</View>
);
}
refreshShows() {
this.setState({
statusMessage: "Refreshing...",
data: null})
var _this = this
RNFetchBlob.fetch('GET', ANIME_SHOWS)
.then((res) => {
let html = res.text()
let doc = PARSER.parseFromString(html, 'text/html')
//TODO: Just list the Links without the animeshows.part
var latest = doc.getElementById('latest_anime')
var items = latest.getElementsBySelector('a')
var list = []
for (var i = 0; i < items.length; i++)
{
var ep_item = items[i].getAttribute("href");
var newEp = ep_item.replace(ANIME_SHOWS, "")
newEp = newEp.substr(0, newEp.length - 1)
list.push(newEp);
}
this.setState({
statusMessage: null,
data: list})
})
// Status code is not 200
.catch((errorMessage, statusCode) => {
Alert.alert(errorMessage);
})
}
}
const styles = StyleSheet.create({
container: {
padding: 20,
flexDirection: 'column',
},
resultsText: {
padding: 5,
borderColor: '#451e2f',
borderBottomWidth: 2,
borderRadius: 5
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment