diff --git a/README.md b/README.md index e946188..9b43181 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,12 @@ curl https://emoji-app.com/api/emoji/smiling%20face # Get a heart emoji 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": "😊", diff --git a/app.py b/app.py index 96ff128..098a7e5 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 @@ -402,6 +340,57 @@ def increment_count(emoji_name, usage_type): "checkered flag": "🏁", "triangular flag": "🚩", + # 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": "⭐", @@ -425,6 +414,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 @@ -432,7 +487,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): @@ -481,6 +540,19 @@ 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 + }) + +@app.route('/api-docs') +def api_docs(): + return render_template('api.html') + if __name__ == '__main__': - init_db() - app.run(debug=True) + app.run(debug=True) 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..5947432 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,56 @@ + + + + + + {% 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