Home > Software engineering >  How do I upload my code to a server or somewhere else so that it runs by itself without needing my c
How do I upload my code to a server or somewhere else so that it runs by itself without needing my c

Time:02-03

I created the following code that updates a database every 3 hours. But I would like to know how I can make it run automatically without needing my computer on for the code to run in the background. Can someone help me out?

This is the code I created:

# IMPORT PACKAGES
import requests
import json
from pandas.io.json import json_normalize
import pandas as pd
import numpy as np
import schedule
import time


def update_database():
    # Criar a URL Decodificada
    url_decoded = ('https://api.supermetrics.com/enterprise/v2/query/data/'
     'keyjson?json='
     '{"ds_id":"FA"'","
     '"ds_accounts":"act_685614429535811"'","
     '"ds_user":"113182444838228"'","
     '"date_range_type":"last_year_inc"'","
     '"fields":"dataSourceName,Date,adcampaign_name,adcampaign_id,adset_name,adset_id,ad_name,ad_id,instagram_preview_url,destinationURL,placement,publisher_platform,platform_position,cost,reach,impressions,link_CTR,CPLC,action_link_click,offsite_conversions,offsite_conversions_fb_pixel_lead,offsite_conversions_fb_pixel_custom,video_thruplay_watched_actions,c_action_5785882244799864,c_action_1133825170595983,c_action_1626778367724470,c_action_2745509738917672,c_action_874795197207155,c_action_1207127133480516,on_facebook_view_content"'","
     '"max_rows":1000000'","
     '"api_key":"My_Key"}')

    # Faz a chamada para a API
    response = requests.get(url_decoded)
    print(response)

    if 'data/json?' in url_decoded:
        data = json.loads(json.dumps(response.json()))
        df = pd.DataFrame.from_dict(data['data'])
        headers = df.iloc[0]
        df2 = pd.DataFrame(df.values[1:],columns=headers)
        df2.head()
        print(df2.head())
    elif 'data/keyjson?' in url_decoded:
        df2 = pd.read_json(json.dumps(response.json()))
        df2.head()
        print(df2.head())
    else:
        import sys
        sys.exit("DEFINE JSON OR KEYJSON AS OUTPUT FORMAT: https://supermetrics.com/docs/product-api-output-formats/")

    # Fazer conexão com o MySQL
    from urllib.parse import quote_plus
    from sqlalchemy import create_engine
    import pymysql
    conn = create_engine('mysql pymysql://agidb:%[email protected]/analytics' % quote_plus("My_Password"))

    df2.to_sql(name='facebook_ads',con=conn,if_exists = 'replace', index=False)
    frame = pd.read_sql('select * from analytics.facebook_ads', con=conn)
    print(frame.head())

schedule.every(3).hours.do(update_database)

while True:
    schedule.run_pending()
    time.sleep(1)

CodePudding user response:

Refactor it to work as an AWS Lambda function that is triggered by a cloudwatch timer. It is actually very easy to setup and runs serverless so it would be very low cost. The challenge would be setting up access between your AWS account and the database. If the database is running on on internal network, you will have to open up access to the database from AWS. If this is at your office, see if you have a network engineer that could help. If this is your home, you can potentially set up "port forwarding" on your router... but ideally you would only do this if your router allowed you to restrict the IP addresses that are allowed so that you do not open yourself to an attack. To do this you would have to setup your Lambda to have a static IP.

If this sounds like too much... then maybe don't attempt it. But I did something similar over an afternoon but I have a very technical background.

Edit: That is of course unless you are able/open to migrate the database to AWS as well because that would be easier to secure.

CodePudding user response:

You can either create a socket, or ssh into the server and use screen. Create a session and enter it. Run a command inside of the screen session and it will continuesly run.

  • Related