Skip to main content

Custom OneLink generator for Android and iOS.

Project description

OneLink Generator

A Python package for generating platform-specific URLs for Android and iOS with support for custom parameters and fallback URLs.

Installation

You can install the package via pip from PyPI:

pip install flask
pip install onelink

Usage

1. Basic Setup

To generate OneLink-style URLs for Android and iOS, you need to set the App IDs for each platform and any other parameters you want to include.

2. Creating a Custom Link - Flask

from flask import Flask, request, jsonify, render_template_string
import urllib.parse
from onelink.generator import DeeplinkGenerator

app = Flask(__name__)

@app.route('/create_deeplink', methods=['POST'])
def create_deeplink():
    try:
        # Getting JSON payload from the request
        config = request.json
        
        # Generate the deep link using DeeplinkGenerator class
        onelink = DeeplinkGenerator(config)
        short_link = onelink.create_deeplink()
        
        # Return the generated short link as response
        return jsonify({"short_link": short_link})
    
    except Exception as e:
        return jsonify({"error": str(e)}), 400

@app.route('/redirect', methods=['GET'])
def redirect_user():
    # Extract the 'android' and 'ios' deep links from the query parameters
    android_deeplink = request.args.get('android')
    parsed_url = urllib.parse.urlparse(android_deeplink)
    query_params = urllib.parse.parse_qs(parsed_url.query)
    android_fallback = query_params.get('fallback_url', [None])[0]  # Default to None if not found

    ios_deeplink = request.args.get('ios')
    ios_parsed_url = urllib.parse.urlparse(ios_deeplink)
    ios_query_params = urllib.parse.parse_qs(ios_parsed_url.query)
    ios_fallback = ios_query_params.get('fallback_url', [None])[0]  # Default to None if not found

    if not android_deeplink or not ios_deeplink or not android_fallback or not ios_fallback:
        return "Error: Missing platform deep links or fallback URLs", 400

    # Extract User-Agent to detect platform (Android or iOS)
    user_agent = request.headers.get('User-Agent', '').lower()

    # Create an HTML page to handle the redirect with JavaScript
    html_content = """
    <html>
        <head>
            <script type="text/javascript">
                var timeout;
                // Redirect to the deep link (Android or iOS)
                function redirectToApp(deepLink, fallbackUrl) {
                    var isAndroid = /android/i.test(navigator.userAgent);
                    var isIos = /iphone|ipod|ipad/i.test(navigator.userAgent);

                    if (isAndroid) {
                        window.location = deepLink;
                        timeout = setTimeout(function() {
                            window.location = fallbackUrl;
                        }, 2000); // 2 seconds timeout for Android
                    } else if (isIos) {
                        window.location = deepLink;
                        timeout = setTimeout(function() {
                            window.location = fallbackUrl;
                        }, 2000); // 2 seconds timeout for iOS
                    }
                    else{
                    window.location = deepLink;
                        timeout = setTimeout(function() {
                            window.location = fallbackUrl;
                        }, 2000);
                    }
                }
                // Call the function to redirect based on user-agent
                redirectToApp("{{ deep_link }}", "{{ fallback_url }}");
            </script>
        </head>
        <body>
            <h1>Redirecting...</h1>
        </body>
    </html>
    """
    
    # Detect platform based on user-agent and generate the appropriate link
    if 'android' in user_agent:
        # Redirect Android users
        deep_link = android_deeplink
        fallback_url = android_fallback
    elif 'iphone' in user_agent or 'ipad' in user_agent:
        # Redirect iOS users
        deep_link = ios_deeplink
        fallback_url = ios_fallback
    else:
        deep_link = android_deeplink
        fallback_url = android_fallback

    # Render the HTML page with the deep link and fallback URL
    return render_template_string(html_content, deep_link=deep_link, fallback_url=fallback_url)


if __name__ == '__main__':
    app.run(debug=True, port=8080)

Payload - create_deeplink

{
    "return_url": "http://app.in/redirect",
    "events": "invite-email",
    "params": {
        "email": "abc@yopmail.com",
        "invite_code": "ABJGD867252JJHG%$#KLFJKLFG"
    },
    "platform": {
        "android": {
            "package": "com.abcandroid",
            "fallback_url": "https://play.google.com/store/apps/details?id=com.aiapp"
        },
        "ios": {
            "package": "com.abcios",
            "fallback_url": "https://apps.apple.com/in/app/app-ai/id6738657557"
        }
    }
}

Generated Links - Production

{
    "short_link": "https://is.gd/RyvDfA"
}

Generated Links - Localhost

{
    "short_link": "http://127.0.0.1:8080/redirect?android=com.abcandroid%3A%2F%2Finvite-email%3Femail%3Dabc%2540yopmail.com%26invite_code%3DABJGD867252JJHG%2525%2524%2523KLFJKLFG%26fallback_url%3Dhttps%253A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%253Fid%253Dcom.aiapp&ios=com.abcios%3A%2F%2Finvite-email%3Femail%3Dabc%2540yopmail.com%26invite_code%3DABJGD867252JJHG%2525%2524%2523KLFJKLFG%26fallback_url%3Dhttps%253A%2F%2Fapps.apple.com%2Fin%2Fapp%2Fapp-ai%2Fid6738657557"
}

Features

  • Customizable URLs: Define your own base URL, parameters, and platform-specific links.
  • Fallback URL: If the app is not installed, users will be redirected to the App Store or Play Store.
  • Custom Parameters: Pass UTM parameters or other query parameters.
  • Simple and Extendable: Built with minimal dependencies and easy to extend.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onelink-0.1.5.tar.gz (4.8 kB view details)

Uploaded Source

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page