How to parse m3u in Dart - dart

I having difficult to sort all the details in the m3u file in Dart. There is no tutorial out there. Here the link for the m3u file:
https://iptv-org.github.io/iptv/languages/tha.m3u
Hope you can help, pls.

You can use the m3u package to parse the file:
import 'dart:io';
import 'package:m3u/m3u.dart';
Future<void> main() async {
final source = await File('tha.m3u').readAsString();
final m3u = await M3uParser.parse(source);
for (final entry in m3u) {
print('Title: ${entry.title} Link: ${entry.link} Logo: ${entry.attributes['tvg-logo']}');
}
}
Outputs:
Title: 13 Siam TV Link: http://203.154.83.176:1935/live/13wDt2b6g4/playlist.m3u8 Logo: https://i.imgur.com/fdopfeC.jpg
Title: 69 TV Link: https://edge1a.v2h-cdn.com/appt7/MediaOnline.stream_360p/chunklist_w559182369.m3u8 Logo: https://i.imgur.com/7KVr5UN.png
Title: 69 TV Link: https://edge1a.v2h-cdn.com/appt7/MediaOnline.stream_720p/chunklist.m3u8 Logo: https://i.imgur.com/7KVr5UN.png
Title: Amarin TV Link: https://www.doofree88.com/streaming/hd-amarin_720/index.m3u8 Logo: https://tv.guchill.com/images/img_tv/tv/ch1026.gif
Title: Bull & Chicken Showtime Link: https://1396402344.rsc.cdn77.org/LS-50041-SIN-23/tracks-v1a1/mono.m3u8 Logo: https://i.imgur.com/DIo2G4b.png
...
Can also be made like this if you want to download the file from a URL and parse it. I have made use of the http package:
import 'dart:io';
import 'package:m3u/m3u.dart';
import 'package:http/http.dart' as http;
Future<void> main() async {
final response =
await http.get('https://iptv-org.github.io/iptv/languages/tha.m3u');
final m3u = await M3uParser.parse(response.body);
for (final entry in m3u) {
print('Title: ${entry.title} Link: ${entry.link} Logo: ${entry.attributes['tvg-logo']}');
}
}

Related

How to Use file in /Library/Sounds as notification sound in ios flutter

I just got a request: Can you download sound file from remote server and make it a notification sound?
Here is my code (for ios)
//package i used
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:dio/dio.dart';
import 'package:path/path.dart' as pathService;
import 'package:path_provider/path_provider.dart';
//Download file we needed
final iosLibraryDir = await getApplicationLibrary();
final soundFilename = 'notification1.wav';
final downloadPath = pathService.join(iosLibraryDir.path, 'Sounds', soundFilename);
Dio dio = Dio();
await dio.download(remoteURL, downloadPath);
//set up notification
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin.initialize(
InitializationSettings(
iOS: IOSInitializationSettings(requestSoundPermission: true)
);
await flutterLocalNotificationsPlugin.show(
1,
'Test Title',
'Test Body',
NotificationDetails(
iOS: IOSNotificationDetails(
presentSound: true,
sound: soundFilename
)
)
);
Notification shown but no sound played
I also try path like '/Library/Sounds/notification1.wav' and full path report from getApplicationLibrary(), but none of them worked.
Is this caused by ios cannot found the path or Apple's security policy not allowed it?

Making HTTP get request to an api in dart only?

Hey I wanted to know how to make a HTTP request to an api or webpage in dart language only not combined with flutter,like requests.get() in python .
Have a look at the http package: https://pub.dev/packages/http
A simple example (not dealing with errors...):
import 'package:http/http.dart' as http;
main() async {
var response = await http.get('http://www.google.com');
print(response.body);
}
The example in the README:
import 'package:http/http.dart' as http;
var url = 'https://example.com/whatsit/create';
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print(await http.read('https://example.com/foobar.txt'));

How do I open an external url in flutter web in new tab or in same tab

I have a simple web app I have created with flutter web. I would like to know how I can open new an external url either in a new tab or in the same tab in my flutter web app. say I want to open the url https://stackoverflow.com/questions/ask
I think you want this — dart:js enables interoperability between Dart and JS —:
import 'dart:js' as js;
// ...
FlatButton(
child: Text('Button'),
onPressed: () {
js.context.callMethod('open', ['https://stackoverflow.com/questions/ask']);
},
)
One simple way is to just create a button and use dart:html's window.open() method:
import 'dart:html' as html;
// ...
html.window.open('https://stackoverflow.com/questions/ask', 'new tab');
The name parameter — which I left as 'new tab' — refers to the new tab's window name, about which you can learn more from MDN's documentation.
https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web
url_launcher has been the solution for android and ios, recently it added support for web.
You can use the url_launcher plugin
Then in your code
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher_string.dart';
void main() {
runApp(Scaffold(
body: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunchUrlString(url)) {
await launchUrlString(url);
} else {
throw 'Could not launch $url';
}
}
Example taken from the package site
As of url_launcher: ^6.1.0 The plugin introduces support for webOnlyWindowName property and few Other new apis like launchUrl, For web support now you longer have to depend on dart:html You can simply declare the below function
Future<void> launchLink(String url, {bool isNewTab = true}) async {
await launchUrl(
Uri.parse(url),
webOnlyWindowName: isNewTab ? '_blank' : '_self',
);
}
and use it like this
onTap:(){
launchLink('https://stackoverflow.com/questions/ask', isNewTab: true)
}
Answered here https://stackoverflow.com/a/56656885/361832
Flutter Web does not support plugins (yet), so you have to use replacements from dart:html
https://api.dartlang.org/stable/2.4.0/dart-html/Window/open.html window.open(url, 'tab');
or
https://api.dartlang.org/stable/2.4.0/dart-html/Window/location.html window.location.assign(url);
Extending the answer of #xuyanjun which works fine when to want to open an external link from flutter web to a new tab. But if you want to open an external link to the website in the same tab in which the flutter web app is currently running.
then you can do it like this.
import 'dart:js' as js;
// ...
FlatButton(
child: Text('Button'),
onPressed: () {
js.context.callMethod('open', ['https://blup.in/','_self']); //<= find explanation below
},
)
Explanation :- dart:js package from flutter provide the functionality to call web-specific functions like open function from flutter and all the strings in the list are parameter which are passed to the function refer this.
So if you want to open new tab then not need to pass seconds parameter but if you wan to open in same tab then pass _self as second parameter.
You can use the below code to launch the URL in the same tab.
window.top.location.href = '<your url>'
Must import
import 'dart:html';
package url_launcher now has web support.
just import url_launcher_web, and url_launcher to your pubspec.yaml
import 'package:url_launcher/url_launcher.dart';
const String _url = 'https://flutter.dev';
void _launchURL() {
launch(_url);
}
To launch URL you need to import url_launcher with latest version and you are good to launch URL through following code:
//Launch Github
_launchGithub() async {
const url = 'https://github.com/Pranav2918'; //Paste your URL string here
if (await canLaunchUrlString(url)) {
await launchUrlString(url);
} else {
throw 'Could not launch $url';
}
}
For non string URLs:
Another approach :
final Uri _url = Uri.parse('https://flutter.dev');
Future<void> _launchUrl() async {
if (!await launchUrl(_url)) {
throw 'Could not launch $_url';
}
}
Happy Fluttering !!

Send parameters to server (post Method) in flutter with Web View

how can i send parameters To server and get response using Web View in flutter ??
I want go to my URL and get response from sever . But i want do it by Web View in flutter
Lets make below example.
Suppose If we are calling here login API with required username and password as parameters.
Hope you know that we have to import below packages.
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
And below is method that I have made it for easy understanding.
Future<http.Response> callWebServiceForLofinUser() async {
final paramDic = {
"username": 'email#example.com',
"password": '123456',
};
final loginData = await http.post(
"http://www.xxxx.com/dev/api/user/userLogin.php", // change with your API
body: paramDic,
);
print(loginData.body);
return loginData;
}
If you have any confusion with above code then feel free to ask me.
This work for me.
import 'package:http/http.dart' as http;
import 'dart:async';
final Map<String,dynamic> _login={
"username":null,
"password":null
};
//manually add parameters to the URL
final loginData = await http.post(
"http://www.xxxx.com/dev/api/user/userLogin.php?username="+_login["username"]+"&password="+_login["password"], // change with your API
body: paramDic,
);
print(loginData.body);
Or Use https://pub.dev/packages/dio
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:async';
final Map<String,dynamic> _login={
"username":null,
"password":null
};
Dio dio = new Dio();
Response response = await
dio.post("http://www.xxxx.com/dev/api/user/userLogin.php,
queryParameters:_login,)
.then((onValue){
print("Success $onValue");
}).catchError((onError){
print("Error $onError");
});

Using dart to download a file

Can we use dart to download a file?
For example in python
I'm using the HTTP package a lot. If you want to download a file that is not huge, you could use the HTTP package for a cleaner approach:
import 'package:http/http.dart' as http;
main() {
http.get(url).then((response) {
new File(path).writeAsBytes(response.bodyBytes);
});
}
What Alexandre wrote will perform better for larger files. Consider writing a helper function for that if you find the need for downloading files often.
Shailen's response is correct and can even be a little shorter with Stream.pipe.
import 'dart:io';
main() async {
final request = await HttpClient().getUrl(Uri.parse('http://example.com'));
final response = await request.close();
response.pipe(File('foo.txt').openWrite());
}
The python example linked to in the question involves requesting the contents of example.com and writing the response to a file.
Here is how you can do something similar in Dart:
import 'dart:io';
main() {
var url = Uri.parse('http://example.com');
var httpClient = new HttpClient();
httpClient.getUrl(url)
.then((HttpClientRequest request) {
return request.close();
})
.then((HttpClientResponse response) {
response.transform(new StringDecoder()).toList().then((data) {
var body = data.join('');
print(body);
var file = new File('foo.txt');
file.writeAsString(body).then((_) {
httpClient.close();
});
});
});
}
We can use http.readBytes(url).
await File(path).writeAsBytes(await http.readBytes('https://picsum.photos/200/300/?random'));
Yes, first of all you have to request to file url using http dart library like:
Response response = await get(Uri.parse(link));
after that your Response object (response) will get that file in self and you can simply write the response bytes to a file and that file will be your downloaded file.
as I open file like this:
File file = File('image.jpg')
then we have to send response bytes to this file like this:
file.writeAsBytes(response.bodyBytes);
now you have downloaded a image file successfully.. Congrates.
additional, for example let me show you a sample code to download a image file :
import 'dart:io';
import 'package:http/http.dart';
main(List<String> args) async {
var link =
"https://pps.whatsapp.net/v/t61.24694-
24/72779382_449683642563635_3243701117464346624_n.jpg?ccb=11-
4&oh=23e3bc2ce3f4940a70cb464494bbda76&oe=619B3B8C";
Response response = await get(Uri.parse(link));
File file = File('image.jpg');
file.writeAsBytes(response.bodyBytes);
}
look, this is the code and a file named image.jpg is downloaded at bottom in terminal view is our downloaded image.
screen shot
this is our actual image which we downloaded.
downloaded image

Resources