diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3cceda5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ + +# Avoid committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..60d7015 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "imgflip_api", + "request": "launch", + "type": "dart", + "program": "example/imgflip_api_example.dart", + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6c634a1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "[dart]": { + "editor.rulers": [ + 80 + ], + }, + "dart.lineLength": 80, +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b9f7fed --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 1.0.0 + +- Initial version. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5829a53 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +# IMGFLIP_API by YarosMallorca + +Copyright (c) 2024 Yaroslav Syubayev + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..03e1701 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# ImgFlip API + +This project provides a Dart library for interacting with the ImgFlip API. It allows you to retrieve a list of available memes and generate custom memes using the ImgFlip service. + +## Installation + +To use this library in your Dart project, add the following dependency to your `pubspec.yaml` file: + +```yaml +dependencies: + imgflip: ^1.0.0 +``` + +Then, run `pub get` to download and install the package. + +## Usage + +To use this library, you first need to import it: + +```dart +import 'package:imgflip/imgflip.dart'; +``` + +Then, you can create an instance of the `ImgFlip` class and use it to interact with the ImgFlip API: + +```dart +final imgFlip = ImgFlip('your_username', 'your_password'); +``` + +### Retrieving a list of available memes + +To retrieve a list of available memes, you can use the `getMemes` method: + +```dart +final memes = await imgFlip.getMemes(); +print(memes); +``` + +### Generating a custom meme + +To generate a custom meme, you can use the `generateMeme` method: + +```dart +final meme = await imgFlip.generateMeme('meme_id', 'top_text', 'bottom_text'); +print(meme); +``` diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/example/imgflip_api_example.dart b/example/imgflip_api_example.dart new file mode 100644 index 0000000..ce1df39 --- /dev/null +++ b/example/imgflip_api_example.dart @@ -0,0 +1,17 @@ +import 'package:imgflip_api/imgflip_api.dart'; + +void main() async { + /// Retrieves a list of available memes from the ImgFlip API. + final memes = await ImgFlip.getMemes(); + + print("Generating meme with template ID: ${memes.first.id}"); + + /// Generates a meme using the specified template ID and texts. + ImgFlip( + username: 'YOUR USERNAME', + password: 'YOUR PASSWORD', + ).generateMeme(templateId: memes.first.id, texts: ['Hello', 'World']).then( + (url) { + print(url); + }); +} diff --git a/lib/imgflip_api.dart b/lib/imgflip_api.dart new file mode 100644 index 0000000..44c4aac --- /dev/null +++ b/lib/imgflip_api.dart @@ -0,0 +1,8 @@ +/// A library to interact with the Imgflip API. +/// +/// The library provides methods to retrieve a list of available memes +/// and generate memes using the Imgflip API. +library imgflip_api; + +export 'src/imgflip_core.dart'; +export 'src/models/meme.dart'; diff --git a/lib/src/imgflip_core.dart b/lib/src/imgflip_core.dart new file mode 100644 index 0000000..609772c --- /dev/null +++ b/lib/src/imgflip_core.dart @@ -0,0 +1,82 @@ +import 'dart:convert'; +import 'models/meme.dart'; +import 'package:http/http.dart' as http; + +/// A class that provides methods to interact with the ImgFlip API. +/// +/// Username and password are required for authentication with the ImgFlip API. +/// Recommended to use Env variables for storing sensitive data. +class ImgFlip { + /// The username used for authentication with the ImgFlip API. + final String username; + + /// The password used for authentication with the ImgFlip API. + final String password; + + /// Creates a new instance of the [ImgFlip] class. + ImgFlip({required this.username, required this.password}); + + /// Retrieves a list of available memes from the ImgFlip API. + /// + /// Returns a list of [Meme] objects representing the available memes. + static Future> getMemes() async { + Map jsonResponse = + await http.get(Uri.parse('https://api.imgflip.com/get_memes')).then( + (value) => jsonDecode(value.body), + ); + + return List.from( + jsonResponse["data"]['memes'].map( + (meme) => Meme.fromJson(meme), + ), + ); + } + + /// Generates a meme using the specified template ID and texts. + /// + /// The [templateId] parameter specifies the ID of the meme template to use. + /// The [texts] parameter is a list of texts to be added to the meme. + /// The [noWatermark] parameter indicates whether to include a watermark on the generated meme. + /// The [font] parameter specifies the font to be used for the texts (optional). + /// + /// Returns the URL of the generated meme. + Future generateMeme( + {required String templateId, + required List texts, + bool noWatermark = false, + String? font}) async { + Map body = { + 'template_id': templateId, + 'username': username, + 'password': password, + 'no_watermark': noWatermark ? '1' : '0', + }; + + if (font != null) { + body['font'] = font; + } + + for (int i = 0; i < texts.length; i++) { + body['boxes[$i][text]'] = texts[i]; + } + + Map jsonResponse = await http + .post( + Uri.parse('https://api.imgflip.com/caption_image'), + body: body, + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + encoding: Encoding.getByName('utf-8'), + ) + .then( + (value) => jsonDecode(value.body), + ); + + if (jsonResponse['success'] == false) { + throw Exception(jsonResponse['error_message']); + } + + return jsonResponse['data']['url']; + } +} diff --git a/lib/src/models/meme.dart b/lib/src/models/meme.dart new file mode 100644 index 0000000..a5c17be --- /dev/null +++ b/lib/src/models/meme.dart @@ -0,0 +1,45 @@ +/// Represents a meme. +class Meme { + final String id; + final String name; + final Uri url; + final int width; + final int height; + final int boxCount; + + /// Creates a new instance of the [Meme] class. + /// + /// The [id] is the unique identifier of the meme. + /// The [name] is the name of the meme. + /// The [url] is the URL of the meme image. + /// The [width] is the width of the meme image. + /// The [height] is the height of the meme image. + /// The [boxCount] is the number of text boxes in the meme. + Meme({ + required this.id, + required this.name, + required this.url, + required this.width, + required this.height, + required this.boxCount, + }); + + /// Creates a new instance of the [Meme] class from a JSON object. + /// + /// The [json] parameter is a JSON object containing the meme data. + factory Meme.fromJson(Map json) { + return Meme( + id: json['id'], + name: json['name'], + url: Uri.parse(json['url']), + width: json['width'], + height: json['height'], + boxCount: json['box_count'], + ); + } + + @override + String toString() { + return 'Meme{id: $id, name: $name, url: $url, width: $width, height: $height, boxCount: $boxCount}'; + } +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..dddffcf --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,15 @@ +name: imgflip_api +description: An API for Imgflip made in Dart. This package allows you to interact with the Imgflip API to create memes. +version: 1.0.0 +repository: https://github.com/YarosMallorca/imgflip_api + +environment: + sdk: ^3.4.0 + +dependencies: + http: ^1.2.1 + +dev_dependencies: + lints: ^4.0.0 + mockito: ^5.4.4 + test: ^1.25.5