Last active
March 13, 2026 07:09
-
-
Save PlugFox/85af305b1e4b6470521afdbe293657f5 to your computer and use it in GitHub Desktop.
Collect AppStore rating
This file contains hidden or 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
| import 'dart:convert'; | |
| import 'dart:io' as io; | |
| import 'package:http/http.dart' as http; | |
| final $log = io.stdout.writeln; // Log to stdout | |
| final $err = io.stderr.writeln; // Log to stderr | |
| void main() => Future<void>(() async { | |
| const appId = "6743122346"; // Doctorina app ID on the App Store, without the 'id' prefix | |
| const countries = { | |
| // Americas | |
| 'us', 'ca', 'mx', 'br', 'ar', 'cl', 'co', 'pe', 've', 'ec', | |
| 'gt', 'cr', 'pa', 'do', 'sv', 'hn', 'ni', 'py', 'uy', 'bo', | |
| 'jm', 'tt', 'bs', 'bb', 'bz', 'gy', 'sr', 'ag', 'dm', 'gd', | |
| 'kn', 'lc', 'vc', 'bm', | |
| // Europe | |
| 'gb', 'de', 'fr', 'it', 'es', 'nl', 'be', 'at', 'ch', 'se', | |
| 'no', 'dk', 'fi', 'ie', 'pt', 'pl', 'cz', 'hu', 'gr', 'ro', | |
| 'bg', 'hr', 'sk', 'si', 'lt', 'lv', 'ee', 'cy', 'mt', 'lu', | |
| 'is', 'al', 'ba', 'xk', 'me', 'mk', 'md', 'rs', | |
| // CIS | |
| 'ru', 'ua', 'by', 'kz', 'uz', 'az', 'ge', 'am', 'kg', 'tj', | |
| 'tm', | |
| // Middle East | |
| 'il', 'sa', 'ae', 'qa', 'kw', 'om', 'bh', 'jo', 'lb', 'tr', | |
| 'iq', 'ye', | |
| // Asia & Pacific | |
| 'jp', 'cn', 'in', 'kr', 'au', 'nz', 'sg', 'hk', 'tw', 'th', | |
| 'my', 'id', 'ph', 'vn', 'pk', 'bd', 'lk', 'np', 'kh', 'la', | |
| 'mm', 'mn', 'mo', 'bn', 'mv', 'bt', 'fj', 'pg', 'sb', 'ws', | |
| // Africa | |
| 'za', 'eg', 'ng', 'ke', 'gh', 'tz', 'ug', 'rw', 'sn', 'ci', | |
| 'cm', 'ma', 'tn', 'dz', 'et', 'mz', 'mg', 'mu', 'bw', 'na', | |
| 'zw', 'zm', 'ao', 'ml', 'bf', 'ne', 'td', 'gn', 'bj', 'tg', | |
| 'ga', 'cg', 'cd', 'lr', 'sl', 'gm', 'gw', 'cv', 'mr', 'sc', | |
| 'sz', 'mw', 'ly', | |
| }; | |
| final client = http.Client(); | |
| final futures = countries.map((c) async { | |
| try { | |
| final url = Uri.parse("https://itunes.apple.com/lookup?id=$appId&country=$c"); | |
| final response = await client.get(url); | |
| final json = jsonDecode(response.body); | |
| if (json case {'results': List<Object?> results} when results.isNotEmpty) { | |
| for (final result in results) { | |
| if (result case <String, Object?>{'averageUserRating': num rating, 'userRatingCount': int reviews}) { | |
| return (country: c, rating: rating, reviews: reviews); | |
| } | |
| } | |
| } | |
| } on Object catch (e) { | |
| $err('Error fetching rating for country $c: $e'); | |
| } | |
| return (country: c, rating: 0.0, reviews: 0); // No-op if error occurs or no data found | |
| }); | |
| // Filter out results with zero ratings or reviews, then sort by rating in descending order | |
| final results = await Stream.fromFutures(futures).where((result) => result.rating > 0 && result.reviews > 0).toList(); | |
| results.sort((a, b) => b.reviews.compareTo(a.reviews)); // Sort by number of reviews in descending order | |
| { | |
| // Log ratings by country and calculate the average rating across all countries | |
| final buffer = StringBuffer('Ratings by country:\n'); | |
| var totalRating = 0.0; | |
| var totalReviews = 0; | |
| for (final result in results) { | |
| buffer.writeln('• ${result.country}: ${result.rating.toStringAsFixed(2)} (${result.reviews})'); | |
| totalRating += result.rating * result.reviews; | |
| totalReviews += result.reviews; | |
| } | |
| buffer | |
| ..writeln() | |
| ..writeln('Average rating:'); | |
| if (totalReviews > 0) { | |
| final averageRating = totalRating / totalReviews; | |
| buffer.writeln('${averageRating.toStringAsFixed(2)} based on $totalReviews reviews'); | |
| } else { | |
| buffer.writeln('No ratings found.'); | |
| } | |
| $log(buffer.toString()); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment