Beall619
Beall619

Reputation: 65

datetime.now().strftime will not update inside while loop

    import facebook
    from datetime import datetime
    import time
    TIME = datetime.now().strftime
    while(1):
      STARTINGMIN = 0
      STARTINGSEC = 0
      STARTINGMIN = TIME("%M")
      STARTINGSEC = TIME("%S")
      print("Preparing API, Functions, and tokens... Current Time : " + TIME("%H:%M:%S"))

      def checklink(link):
        links = open("C:\\Users\\BEALL619\\Desktop\\Python\\FACEBOOKPAGEBOT\\usedlinks.txt")
        for CHECKING in links.readlines():
          CHECKING = CHECKING.rstrip('\n')
          print("New link - " + link + " Compared to used link - " + CHECKING)
          if str(link) == str(CHECKING):
            print("[!] Duplicate link found")
            linkstat = "BAD"
            break

          elif(not link == CHECKING):
              print("[*] This is not duplicated... So Far")
        print("Link is not duplicated. Preparing to post")
        linkstat = "GOOD"
        return(linkstat)
      """    
      cfg = {
        "page_id"      : "i am not",
        "access_token" : "dumb enough to",
        "appsecret_proof" : "leave these values in the code"
        }

      def get_api(cfg):
        graph = facebook.GraphAPI(cfg['access_token'])
        resp = graph.get_object('me/accounts')
        page_access_token = None
        for page in resp['data']:
          if page['id'] == cfg['page_id']:
            page_access_token = page['access_token']
        graph = facebook.GraphAPI(page_access_token)
        return graph

      api = get_api(cfg)

      def post(link):
        attach = {
        "link":link,
        }
        status = api.put_wall_post(attachment=attach, message = "")
      """
      print("Done Preparing API, Functions, And Tokens. Took - " + str(int(TIME("%M")) - int(STARTINGMIN)) + "M " + str(int(TIME("%S")) - int(STARTINGSEC)) + "S")

      STARTINGMIN = 0
      STARTINGSEC = 0
      while(not(TIME("%H") == "00" and TIME("%M") == "00" or TIME("%H") == "02" and TIME("%M") == "00" or TIME("%H") == "04" and TIME("%M") == "00" or TIME("%H") == "06" and TIME("%M") == "00") or TIME("%H") == "08" and TIME("%M") == "00" or TIME("%H") == "10" and TIME("%M") == "00" or TIME("%H") == "12" and TIME("%M") == "00" or TIME("%H") == "14" and TIME("%M") == "00" or TIME("%H") == "16" and TIME("%M") == "00" or TIME("%H") == "18" and TIME("%M") == "00" or TIME("%H") == "20" and TIME("%M") == "00" or TIME("%H") == "22" and TIME("%M") == "00"):
        time.sleep(.2)
        print("Awaiting Next 2H invertal. Current time is - " + TIME("%H:%M:%S"))

ignore everything but the problem.. i am a messy programmer(this script is not done yet.. obvisouly)

the line print("Awaiting Next 2H invertal. Current time is - " + TIME("%H:%M:%S"))

prints the same thing... i need help with it printing correctly

while typing this question i realize the problem might be TIME = datetime.now().strftime

Edit

The reason I had thought what I did would work is because I personally am used to calling functions from variables.

After the help I received from this question the mistake I made was I accidentally stored a value in the variable instead of "half a function"(if that makes sense)

Upvotes: 1

Views: 4782

Answers (1)

Carlos Afonso
Carlos Afonso

Reputation: 1957

The problem is that you stored now() in a variable, hence it won't be updated. Try changing

print("Awaiting Next 2H invertal. Current time is - " + TIME("%H:%M:%S"))

To:

print("Awaiting Next 2H invertal. Current time is - " + datetime.now().strftime("%H:%M:%S"))

Do that on any other place in the code you want the time to be updated.

Upvotes: 2

Related Questions