From d0f27c1a77e433f02fa8f5e66b43eb99b54aea87 Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 20 Dec 2024 17:18:32 +1100 Subject: [PATCH 1/3] added a random api end point that returns a random emoji --- README.md | 17 ++++--- app.py | 141 +++++++++++++++++++++++++++++------------------------- 2 files changed, 87 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index ae4e83b..3ed3b7b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Emoji Library +# Emoji Library 😎 A simple and fun way to find, copy, and use emojis! Browse our collection through the web interface or access emojis programmatically via our API. @@ -6,7 +6,7 @@ A simple and fun way to find, copy, and use emojis! Browse our collection throug ### Web Interface -1. Visit the website and browse through our emoji collection +1. Visit [emoji-app.com](https://emoji-app.com) to browse through our emoji collection 2. Click on any emoji card to instantly copy it to your clipboard 3. See how many times each emoji has been used 4. The count updates in real-time when you or others copy the emoji @@ -19,13 +19,16 @@ Need a specific emoji in your application? Just make a GET request: ```bash # Get a smiling face emoji -curl http://localhost:5000/api/emoji/smiling%20face +curl https://emoji-app.com/api/emoji/smiling%20face # Get a heart emoji -curl http://localhost:5000/api/emoji/red%20heart +curl https://emoji-app.com/api/emoji/red%20heart + +# Get a random emoji +curl https://emoji-app.com/api/random ``` -Response: +Response for specific or random emoji: ```json { "emoji": "😊", @@ -39,7 +42,7 @@ Response: Want to see what's trending? Get the top 10 most used emojis: ```bash -curl http://localhost:5000/api/stats +curl https://emoji-app.com/api/stats ``` Response: @@ -67,7 +70,7 @@ Here are some popular emojis you can try: Just replace the spaces with %20 when using the API: ```bash -curl http://localhost:5000/api/emoji/thinking%20face +curl https://emoji-app.com/api/emoji/thinking%20face ``` ## For Developers diff --git a/app.py b/app.py index 0011b45..0bedb21 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import sqlite3 from datetime import datetime import os +import random # Add import for random selection app = Flask(__name__) @@ -15,69 +16,6 @@ # Ensure the directory exists os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) -# Database initialization -def init_db(): - conn = sqlite3.connect(DB_PATH) - c = conn.cursor() - # Create table for emoji usage counts - c.execute(''' - CREATE TABLE IF NOT EXISTS emoji_counts ( - emoji_name TEXT PRIMARY KEY, - copy_count INTEGER DEFAULT 0, - api_count INTEGER DEFAULT 0, - last_used TIMESTAMP - ) - ''') - # Create table for usage history - c.execute(''' - CREATE TABLE IF NOT EXISTS usage_history ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - emoji_name TEXT, - usage_type TEXT, - timestamp TIMESTAMP, - FOREIGN KEY (emoji_name) REFERENCES emoji_counts (emoji_name) - ) - ''') - # Initialize counts for all emojis if they don't exist - for emoji_name in EMOJIS.keys(): - c.execute('INSERT OR IGNORE INTO emoji_counts (emoji_name, copy_count, api_count) VALUES (?, 0, 0)', (emoji_name,)) - conn.commit() - conn.close() - -def get_emoji_counts(): - conn = sqlite3.connect(DB_PATH) - c = conn.cursor() - c.execute('SELECT emoji_name, copy_count + api_count as total_count FROM emoji_counts') - counts = {row[0]: row[1] for row in c.fetchall()} - conn.close() - return counts - -def increment_count(emoji_name, usage_type): - conn = sqlite3.connect(DB_PATH) - c = conn.cursor() - if usage_type == 'copy': - c.execute('UPDATE emoji_counts SET copy_count = copy_count + 1, last_used = ? WHERE emoji_name = ?', - (datetime.now(), emoji_name)) - else: # api - c.execute('UPDATE emoji_counts SET api_count = api_count + 1, last_used = ? WHERE emoji_name = ?', - (datetime.now(), emoji_name)) - - # Record in history - c.execute('INSERT INTO usage_history (emoji_name, usage_type, timestamp) VALUES (?, ?, ?)', - (emoji_name, usage_type, datetime.now())) - - conn.commit() - - # Get updated count - if usage_type == 'copy': - c.execute('SELECT copy_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,)) - else: - c.execute('SELECT api_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,)) - count = c.fetchone()[0] - - conn.close() - return count - # Manual emoji dictionary EMOJIS = { # Faces and Expressions @@ -216,6 +154,72 @@ def increment_count(emoji_name, usage_type): "books": "📚" } +# Database initialization +def init_db(): + conn = sqlite3.connect(DB_PATH) + c = conn.cursor() + # Create table for emoji usage counts + c.execute(''' + CREATE TABLE IF NOT EXISTS emoji_counts ( + emoji_name TEXT PRIMARY KEY, + copy_count INTEGER DEFAULT 0, + api_count INTEGER DEFAULT 0, + last_used TIMESTAMP + ) + ''') + # Create table for usage history + c.execute(''' + CREATE TABLE IF NOT EXISTS usage_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + emoji_name TEXT, + usage_type TEXT, + timestamp TIMESTAMP, + FOREIGN KEY (emoji_name) REFERENCES emoji_counts (emoji_name) + ) + ''') + # Initialize counts for all emojis if they don't exist + for emoji_name in EMOJIS.keys(): + c.execute('INSERT OR IGNORE INTO emoji_counts (emoji_name, copy_count, api_count) VALUES (?, 0, 0)', (emoji_name,)) + conn.commit() + conn.close() + +# Initialize database on app startup +init_db() + +def get_emoji_counts(): + conn = sqlite3.connect(DB_PATH) + c = conn.cursor() + c.execute('SELECT emoji_name, copy_count + api_count as total_count FROM emoji_counts') + counts = {row[0]: row[1] for row in c.fetchall()} + conn.close() + return counts + +def increment_count(emoji_name, usage_type): + conn = sqlite3.connect(DB_PATH) + c = conn.cursor() + if usage_type == 'copy': + c.execute('UPDATE emoji_counts SET copy_count = copy_count + 1, last_used = ? WHERE emoji_name = ?', + (datetime.now(), emoji_name)) + else: # api + c.execute('UPDATE emoji_counts SET api_count = api_count + 1, last_used = ? WHERE emoji_name = ?', + (datetime.now(), emoji_name)) + + # Record in history + c.execute('INSERT INTO usage_history (emoji_name, usage_type, timestamp) VALUES (?, ?, ?)', + (emoji_name, usage_type, datetime.now())) + + conn.commit() + + # Get updated count + if usage_type == 'copy': + c.execute('SELECT copy_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,)) + else: + c.execute('SELECT api_count FROM emoji_counts WHERE emoji_name = ?', (emoji_name,)) + count = c.fetchone()[0] + + conn.close() + return count + def get_all_emojis(): return EMOJIS @@ -272,6 +276,15 @@ def get_stats(): conn.close() return jsonify(top_emojis) +@app.route('/api/random') +def get_random_emoji(): + emoji_name = random.choice(list(EMOJIS.keys())) + count = increment_count(emoji_name, 'api') + return jsonify({ + 'emoji': EMOJIS[emoji_name], + 'name': emoji_name, + 'count': count + }) + if __name__ == '__main__': - init_db() app.run(debug=True) \ No newline at end of file From 84c7a71cb9108870657f27b86d7926f615c3169c Mon Sep 17 00:00:00 2001 From: Brendan Date: Fri, 20 Dec 2024 17:27:23 +1100 Subject: [PATCH 2/3] added an API page --- app.py | 4 ++ templates/api.html | 118 +++++++++++++++++++++++++++++++++++++++++++ templates/base.html | 55 ++++++++++++++++++++ templates/index.html | 101 ++++++++++++++++++------------------ 4 files changed, 228 insertions(+), 50 deletions(-) create mode 100644 templates/api.html create mode 100644 templates/base.html diff --git a/app.py b/app.py index 0bedb21..58e5415 100644 --- a/app.py +++ b/app.py @@ -286,5 +286,9 @@ def get_random_emoji(): 'count': count }) +@app.route('/api-docs') +def api_docs(): + return render_template('api.html') + if __name__ == '__main__': app.run(debug=True) \ No newline at end of file diff --git a/templates/api.html b/templates/api.html new file mode 100644 index 0000000..7b042c2 --- /dev/null +++ b/templates/api.html @@ -0,0 +1,118 @@ +{% extends "base.html" %} + +{% block title %}API Documentation - Emoji Library{% endblock %} + +{% block extra_css %} + +{% endblock %} + +{% block content %} +

API Documentation

+

Access our emoji collection programmatically through our REST API endpoints.

+ +
+

GET Get a Specific Emoji

+

Retrieve a specific emoji by its name.

+

https://emoji-app.com/api/emoji/{emoji_name}

+
curl https://emoji-app.com/api/emoji/smiling%20face
+
+

Response:

+
{
+    "emoji": "😊",
+    "name": "smiling face",
+    "count": 42
+}
+
+
+ +
+

GET Get a Random Emoji

+

Get a random emoji from our collection.

+

https://emoji-app.com/api/random

+
curl https://emoji-app.com/api/random
+
+

Response:

+
{
+    "emoji": "🚀",
+    "name": "rocket",
+    "count": 17
+}
+
+
+ +
+

GET View Popular Emojis

+

Get the top 10 most used emojis with their usage statistics.

+

https://emoji-app.com/api/stats

+
curl https://emoji-app.com/api/stats
+
+

Response:

+
[
+    {
+        "emoji": "😊",
+        "name": "smiling face",
+        "copy_count": 42,
+        "api_count": 12,
+        "last_used": "2024-01-01 12:00:00"
+    },
+    // ... more emojis
+]
+
+
+ +
+

GET Record Emoji Copy

+

Record when an emoji is copied from the website.

+

https://emoji-app.com/api/increment/{emoji_name}

+
curl https://emoji-app.com/api/increment/rocket
+
+

Response:

+
{
+    "success": true,
+    "count": 18
+}
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..0086cb3 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,55 @@ + + + + + + {% block title %}Emoji Library{% endblock %} + + {% block extra_css %}{% endblock %} + + + +
+ {% block content %}{% endblock %} +
+ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 174f665..1c22797 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,54 +1,55 @@ - - - - - - Emoji Library - - - -
-

Emoji Library

-

Click on any emoji to copy it! Each emoji also has its own API endpoint.

- -
- {% for name, char in emojis.items() %} -
-
{{ char }}
-
{{ name }}
-
- Copied: {{ counts[name] }} times -
-
- API: /api/emoji/{{ name }} -
-
- {% endfor %} +{% extends "base.html" %} + +{% block extra_css %} + + +{% endblock %} + +{% block content %} +

Emoji Library

+

Click on any emoji to copy it! Each emoji also has its own API endpoint.

+ +
+ {% for name, char in emojis.items() %} +
+
{{ char }}
+
{{ name }}
+
+ Copied: {{ counts[name] }} times +
+
+ API: /api/emoji/{{ name }}
+ {% endfor %} +
- - - \ No newline at end of file + // Increment count + fetch(`/api/increment/${name}`) + .then(response => response.json()) + .then(data => { + if (data.success) { + document.querySelectorAll(`.count-${name.replace(' ', '_')}`).forEach(el => { + el.textContent = data.count; + }); + } + }); + }); + } + +{% endblock %} \ No newline at end of file From e78ce722db1cefed56fd45b562148e19f755e515 Mon Sep 17 00:00:00 2001 From: Brendan Date: Tue, 24 Dec 2024 09:25:53 +1100 Subject: [PATCH 3/3] added flags, sorted by popularity on main page, added favicon --- app.py | 57 ++++++++++++++++++++++++++++++++++++++++++++- templates/base.html | 1 + 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 58e5415..16d11cf 100644 --- a/app.py +++ b/app.py @@ -131,6 +131,57 @@ "flower": "🌸", "four leaf clover": "🍀", + # Flags + "flag argentina": "🇦🇷", + "flag australia": "🇦🇺", + "flag austria": "🇦🇹", + "flag belgium": "🇧🇪", + "flag brazil": "🇧🇷", + "flag canada": "🇨🇦", + "flag chile": "🇨🇱", + "flag china": "🇨🇳", + "flag colombia": "🇨🇴", + "flag denmark": "🇩🇰", + "flag egypt": "🇪🇬", + "flag finland": "🇫🇮", + "flag france": "🇫🇷", + "flag germany": "🇩🇪", + "flag greece": "🇬🇷", + "flag hong kong": "🇭🇰", + "flag iceland": "🇮🇸", + "flag india": "🇮🇳", + "flag indonesia": "🇮🇩", + "flag ireland": "🇮🇪", + "flag israel": "🇮🇱", + "flag italy": "🇮🇹", + "flag japan": "🇯🇵", + "flag malaysia": "🇲🇾", + "flag mexico": "🇲🇽", + "flag morocco": "🇲🇦", + "flag netherlands": "🇳🇱", + "flag new zealand": "🇳🇿", + "flag nigeria": "🇳🇬", + "flag norway": "🇳🇴", + "flag pakistan": "🇵🇰", + "flag philippines": "🇵🇭", + "flag poland": "🇵🇱", + "flag portugal": "🇵🇹", + "flag russia": "🇷🇺", + "flag saudi arabia": "🇸🇦", + "flag singapore": "🇸🇬", + "flag south africa": "🇿🇦", + "flag south korea": "🇰🇷", + "flag spain": "🇪🇸", + "flag sweden": "🇸🇪", + "flag switzerland": "🇨🇭", + "flag taiwan": "🇹🇼", + "flag thailand": "🇹🇭", + "flag turkey": "🇹🇷", + "flag uk": "🇬🇧", + "flag ukraine": "🇺🇦", + "flag usa": "🇺🇸", + "flag vietnam": "🇻🇳", + # Other "rocket": "🚀", "star": "⭐", @@ -227,7 +278,11 @@ def get_all_emojis(): def index(): emojis = get_all_emojis() counts = get_emoji_counts() - return render_template('index.html', emojis=emojis, counts=counts) + + # Sort emojis by total usage count (descending) + sorted_emojis = dict(sorted(emojis.items(), key=lambda x: counts.get(x[0], 0), reverse=True)) + + return render_template('index.html', emojis=sorted_emojis, counts=counts) @app.route('/api/emoji/') def get_emoji(emoji_name): diff --git a/templates/base.html b/templates/base.html index 0086cb3..5947432 100644 --- a/templates/base.html +++ b/templates/base.html @@ -4,6 +4,7 @@ {% block title %}Emoji Library{% endblock %} +