-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from eban12/refactor-api
Code refactor
- Loading branch information
Showing
10 changed files
with
255 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import 'dart:convert'; | ||
import 'dart:math'; | ||
|
||
import 'package:arxiv/models/paper.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:xml2json/xml2json.dart'; | ||
|
||
class Arxiv { | ||
static const _baseUrl = "http://export.arxiv.org/api/query?search_query=all"; | ||
|
||
static final _dio = Dio(); | ||
|
||
static const _topics = [ | ||
"acid", | ||
"atheory of justice", | ||
"attention is all you need", | ||
"augmented", | ||
"behavioural", | ||
"books", | ||
"black hole", | ||
"brain", | ||
"cats", | ||
"computer", | ||
"creative", | ||
"dog", | ||
"dna sequencing", | ||
"dysonsphere", | ||
"ecg", | ||
"emotional", | ||
"entanglement", | ||
"fear", | ||
"fuzzy sets", | ||
"fidgeting", | ||
"glucose", | ||
"garbage", | ||
"gonad", | ||
"hands", | ||
"heart", | ||
"higgs boson", | ||
"hydron", | ||
"identity", | ||
"industrial", | ||
"isolation", | ||
"laptop", | ||
"love", | ||
"labratory", | ||
"machine learning", | ||
"mathematical theory of communication", | ||
"mental state", | ||
"micro", | ||
"microchip", | ||
"mobile", | ||
"molecular cloning", | ||
"neural network", | ||
"negative", | ||
"numbers", | ||
"pc", | ||
"planet", | ||
"protein measurement", | ||
"psychology", | ||
"quantum", | ||
"quasar", | ||
"qubit", | ||
"reading", | ||
"relationship", | ||
"relativity", | ||
"robotics", | ||
"rocket", | ||
"sitting", | ||
"spider", | ||
"spiritual", | ||
"sulpher", | ||
"television", | ||
"tiered reward", | ||
"transport", | ||
"virtual reality", | ||
"volcano", | ||
"vision", | ||
]; | ||
|
||
/// Fetches papers for the requested [term]. | ||
/// [page] and [pageSize] are optional. If missing, 0 and 30 are used as defaults respectively. | ||
static Future<List<Paper>> search( | ||
String term, { | ||
int page = 0, | ||
int pageSize = 30, | ||
}) async { | ||
final xml2json = Xml2Json(); | ||
|
||
try { | ||
var response = await _dio.get( | ||
"$_baseUrl:$term&start=$page&max_results=$pageSize", | ||
); | ||
xml2json.parse(response.data); | ||
var jsonData = await json.decode(xml2json.toParker()); | ||
return jsonData["feed"]["entry"].map<Paper>((entry) => Paper.fromJson(entry)).toList(); | ||
} catch (e) { | ||
return []; | ||
} | ||
} | ||
|
||
/// Fetches papers for a random topic. | ||
static Future<List<Paper>> suggest({int pageSize = 30}) { | ||
Random random = Random(); | ||
int randomIndex = random.nextInt(_topics.length); | ||
String topic = _topics[randomIndex]; | ||
|
||
return search(topic, pageSize: pageSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import 'package:arxiv/models/paper.dart'; | ||
import 'package:hive/hive.dart'; | ||
|
||
part 'bookmarks.g.dart'; | ||
|
||
@HiveType(typeId: 0) | ||
class Bookmark extends HiveObject { | ||
@HiveField(0) | ||
late dynamic paperData; | ||
late Paper paperData; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
import 'package:hive/hive.dart'; | ||
|
||
part 'paper.g.dart'; | ||
|
||
@HiveType(typeId: 1) | ||
class Paper { | ||
@HiveField(0) | ||
final String id; | ||
@HiveField(1) | ||
final String title; | ||
@HiveField(2) | ||
final String summary; | ||
@HiveField(3) | ||
final String publishedAt; | ||
@HiveField(4) | ||
final String authors; | ||
|
||
Paper( | ||
this.id, | ||
this.title, | ||
this.summary, | ||
this.publishedAt, | ||
this.authors, | ||
); | ||
|
||
@override | ||
String toString() { | ||
return 'Paper(id: $id, title: $title, summary: $summary, publishedAt: $publishedAt, authors: $authors)'; | ||
} | ||
|
||
factory Paper.fromJson(Map<String, dynamic> jsonData) => Paper( | ||
jsonData["id"].toString(), | ||
jsonData["title"].toString(), | ||
jsonData["summary"].toString(), | ||
jsonData["published"].toString(), | ||
jsonData["author"] | ||
.toString() | ||
.replaceAll("name:", "") | ||
.replaceAll(RegExp("[\\[\\]\\{\\}]"), ""), | ||
); | ||
|
||
Map<String, dynamic> toJson() => { | ||
"id": id, | ||
"title": title, | ||
"summary": summary, | ||
"published": publishedAt, | ||
"author": authors, | ||
}; | ||
} |
Oops, something went wrong.