Skip to content

Commit

Permalink
Initial Version
Browse files Browse the repository at this point in the history
  • Loading branch information
YarosMallorca committed May 24, 2024
0 parents commit 8825258
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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",
}
]
}
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"[dart]": {
"editor.rulers": [
80
],
},
"dart.lineLength": 80,
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 1.0.0

- Initial version.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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);
```
30 changes: 30 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions example/imgflip_api_example.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}
8 changes: 8 additions & 0 deletions lib/imgflip_api.dart
Original file line number Diff line number Diff line change
@@ -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';
82 changes: 82 additions & 0 deletions lib/src/imgflip_core.dart
Original file line number Diff line number Diff line change
@@ -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<List<Meme>> getMemes() async {
Map jsonResponse =
await http.get(Uri.parse('https://api.imgflip.com/get_memes')).then(
(value) => jsonDecode(value.body),
);

return List<Meme>.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<String> generateMeme(
{required String templateId,
required List<String> texts,
bool noWatermark = false,
String? font}) async {
Map<String, String> 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'];
}
}
45 changes: 45 additions & 0 deletions lib/src/models/meme.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> 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}';
}
}
15 changes: 15 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8825258

Please sign in to comment.