Jump to content

Python to JavaScript

I need to convert this code to JavaScript so that it can be embedded into a web browser. I am okay at python, but have never coded before.

I would massively appreciate any help.

from urllib.request import urlopen, Request
import json
import random
greetings = ["Hello", "Hi", "Greetings"]
response = ["I can do what ever you need", "I'm not as clever as your manager but ask me anyway", "I can't juggle. Anything else - just don't ask me to juggle"]


def main():
    print("Hello")
    print("As well as greetings I can also do other things. Type 'List' to see possible responses")
    while True:
        user = input("Enter your message: ")
        if user == "1":
            holidays()
        elif user == "2":
            shift()
        elif user == "3":
            swap()
        elif user in greetings:
            print("Hello. What can I help you with?")
        elif user == "Who are you?":
            print("I'm MyShiftPlanner, your shift working helper")
        elif user == "What can you do?":
            print(random.choice(response))
        elif user == "List" or user == "list":
            print("1. Check how many days until your next holiday\n2. Check when your next shift is\n3. Swap your shift with someone else")
        elif user == "quit":
            return
        else:
            print("Sorry, I didn't understand that")



def holidays():
    choice = input("Do you want to know how many days are left until your holiday? (Y/N) ")
    if choice == "y" or choice == "Y":
        main = urlopen("https://myshiftplannercloud.azurewebsites.net/api/ChatBot/DaysOff?employeeID=joe512")
        string = json.loads(main.read())
        print("Your next holiday is on", string["NextHoliday"])
        print("You have", string["DaysUntil"], "days to go!")
    else:
        main()
def shift():
    choice = input("Do you want to know when your next shift is? (Y/N) ")
    if choice == "y" or choice == "Y":
        main = urlopen("https://myshiftplannercloud.azurewebsites.net/api/ChatBot/NextShift?employeeID=joe512")
        string = json.loads(main.read())
        print("Your next shift is on", string["ShiftDate"], "and you are working", string["ShiftType"])
    else:
        main()
def swap():
    choice = input("Do you want to change your shift? (Y/N) ")
    if choice == "Y" or choice == "y":

        datey = int(input("Please enter the year of the shift you want to swap(YYYY): "))
        datem = int(input("Please enter the month of the shift you want to swap(MM): "))
        dated = int(input("Please enter the day of the shift you want to swap (DD): "))
        conditionsSetURL = "https://myshiftplannercloud.azurewebsites.net/api/ChatBot/SwapShift?employeeID=joe512&date={}-{}-{}&swapwith=frank".format(datey, datem, dated)
        newConditions = {"EmployeeID": "joe512", "ShiftDate": "{}-{}-{}".format(datey, datem, dated), "SwappedWith": "frank"}
        params = json.dumps(newConditions).encode('utf8')
        req = Request(conditionsSetURL, data=params, headers={'content-type': 'application/json'})
        response = urlopen(req)
        print(response.read().decode('utf8'))
        print("Your shift on {}-{}-{} has been swapped with".format(datey, datem, dated), "Frank")
    else:
        main()


main()

Lewis

Manchester, England

Manchester City Football Club <3

Lewis

Link to comment
Share on other sites

Link to post
Share on other sites

Using vanilla JavaScript may not be your best bet, have you tried looking in to jQuery?

 

It looks like you are opening request to another web-server to update shifts? To open connections to a URL you can use something like jQuery.post() or jQuery.get().

 

Also, just a heads up that anyone will be able to switch anyone else's shift providing they know the user's id. I'm not sure how much verification/validation you're planning to do, but you might want to run authentication checks against your own server before sending the request. Anyone can inspect element, change the username that they're going to post it as. With JavaScript you have to really trust the end-user.

 

Sorry if I don't make much sense, I'm ill and my head hurts :(

Bespoke Software Engineer

 

Laptop: MacBook Air (13-inch, Early 2015) - 8GB RAM - Intel Core i5 1.6GHz - Intel HD Graphics 6000 1536 MB

DesktopGAMEMAX Onyx - AMD FX6300 Black Edition 6 Core (Overclocked to 4.1GHz) - GIGABYTE NVIDIA GTX 1050Ti Overclocked - MSI NVIDIA GTX 750Ti Gaming 1085MHz - HyperX Savage 8 GB 1866 MHz DDR3 - MSI 970 Gaming - Corsair CP-9020015-UK - Kingston SSDNow UV400 120 GB Solid State Drive

Link to comment
Share on other sites

Link to post
Share on other sites

Or don't. I mean, adding a giant JS library just for communication with a webserver is ridiculous.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

I could probably do this for you but that isn't in the spirit of learning.

 

you'll need a div as your output to the users, a input form for user input.

 

read up on document.getElementById("whatEverId").innerHTML for changing the contents of your div. You can also use this to read user input from the input box.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×