Skip to content

Instantly share code, notes, and snippets.

@allanx2000 allanx2000/AnimeShowsList.js Secret
Created Feb 8, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.