1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-14 01:15:12 +01:00

refactor: extract github trending api as seperated package

This commit is contained in:
Rongjian Zhang 2019-03-06 11:26:10 +08:00
parent ec1d995660
commit 2256b86eba
2 changed files with 16 additions and 63 deletions

View File

@ -1,20 +1,8 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' show parse;
import 'package:github_trending/github_trending.dart';
import '../scaffolds/refresh.dart';
import '../widgets/repo_item.dart';
// class TrendingRepo {
// String owner;
// String name;
// String description;
// String languageName;
// String languageColor;
// int starCount;
// int forkCount;
// int stars;
// }
class TrendingScreen extends StatefulWidget {
@override
_TrendingScreenState createState() => _TrendingScreenState();
@ -22,65 +10,30 @@ class TrendingScreen extends StatefulWidget {
class _TrendingScreenState extends State<TrendingScreen> {
Future<List<dynamic>> _fetchTrendingRepos() async {
var res = await http.get('https://github.com/trending');
// print(res.body);
var document = parse(res.body);
var items = document.querySelectorAll('.repo-list>li');
var items = await getTrendingRepositories();
return items.map((item) {
Map<String, String> lang;
var colorNode = item.querySelector('.repo-language-color');
if (colorNode != null) {
lang = {
'name': colorNode.nextElementSibling.innerHtml.trim(),
'color': RegExp(r'(#\w{6})')
.firstMatch(colorNode.attributes['style'])
.group(0),
};
}
var payload = {
return {
'owner': {
'login': item
.querySelector('h3>a>span')
?.innerHtml
?.replaceFirst('/', '')
?.trim()
'login': item.owner,
},
'name': item
.querySelector('h3>a')
?.innerHtml
?.replaceFirst(RegExp(r'^[\s\S]*span>'), '')
?.trim(),
'description': item.children[2]
.querySelector('p')
?.innerHtml
?.trim()
?.replaceAll(RegExp(r'<g-emoji.*?>'), '')
?.replaceAll(RegExp(r'</g-emoji>'), ''),
'name': item.name,
'description': item.description,
'stargazers': {
'totalCount': item.children[3]
.querySelector('.octicon-star')
?.parent
?.innerHtml
?.replaceFirst(RegExp(r'^[\s\S]*svg>'), '')
?.trim()
'totalCount': item.starCount,
},
'forks': {
'totalCount': item
.querySelector('.octicon-repo-forked')
?.parent
?.innerHtml
?.replaceFirst(RegExp(r'^[\s\S]*svg>'), '')
?.trim() ??
0,
'totalCount': item.forkCount,
},
'primaryLanguage': lang,
'primaryLanguage': item.primaryLanguage == null
? null
: {
'name': item.primaryLanguage.name,
'color': item.primaryLanguage.color,
},
'isPrivate': false,
'isFork': false // TODO:
};
// print(payload);
return payload;
}).toList();
}

View File

@ -15,7 +15,7 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^0.11.3
http: ^0.12.0
rxdart: ^0.20.0
uri: ^0.11.3
intl: ^0.15.7
@ -27,7 +27,7 @@ dependencies:
nanoid: ^0.0.6
share: ^0.6.0
flutter_vector_icons: ^0.0.2
html: ^0.13.3
github_trending: ^0.0.1
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.