0

I have the following view in django which works as expected:

from xmlsoccer import XmlSoccer
from straightred.models import StraightredFixtureLive
    @csrf_exempt
    def updatefixtureslive(request, soccerseason, league):

        if request.user.is_authenticated():

            xmlsoccer = XmlSoccer(api_key='XYZ123ABC789', use_demo=False)
            fixtureslive = xmlsoccer.call_api(method='GetLiveScoreByLeague',
                                       league='English Premier League')

        count = 0

            for fixturelive in fixtureslive:



                if 'Id' in fixturelive.keys():

                count = count + 1       
                    fixtureLiveUpdate = StraightredFixtureLive(fixtureid_id=fixturelive['Id'],
                                                           away_team_id = fixturelive['AwayTeam_Id'],
                                                           home_team_id = fixturelive['HomeTeam_Id'],
                                                           fixturedate = fixturelive['Date'],
                                                           fixturestatus = fixturelive['Time'],
                                                           fixturematchday_id = fixturelive['Round'],
                                                           spectators = fixturelive['Spectators'],
                                                           hometeamscore = fixturelive['HomeGoals'],
                                                           awayteamscore = fixturelive['AwayGoals'],
                                                           homegoaldetails = fixturelive['HomeGoalDetails'],
                                                           awaygoaldetails = fixturelive['AwayGoalDetails'],
                                                           hometeamyellowcarddetails = fixturelive['HomeTeamYellowCardDetails'],
                                                           awayteamyellowcarddetails = fixturelive['AwayTeamYellowCardDetails'],
                                                           hometeamredcarddetails = fixturelive['HomeTeamRedCardDetails'],
                                                           awayteamredcarddetails = fixturelive['AwayTeamRedCardDetails']
                                )
                    fixtureLiveUpdate.save()

            return HttpResponse("Live games have been updated." + str(count))
        else:
            return HttpResponse("You must be logged in to update teams.")

I have removed all the parts I thought were just django specific and ended up with the following:

from xmlsoccer import XmlSoccer
from straightred.models import StraightredFixtureLive

xmlsoccer = XmlSoccer(api_key='XYZ123ABC789', use_demo=False)
fixtureslive = xmlsoccer.call_api(method='GetLiveScoreByLeague',
                                   league='English Premier League')

count = 0

for fixturelive in fixtureslive:



if 'Id' in fixturelive.keys():

    count = count + 1
    fixtureLiveUpdate = StraightredFixtureLive(fixtureid_id=fixturelive['Id'],
                                                       away_team_id = fixturelive['AwayTeam_Id'],
                                                       home_team_id = fixturelive['HomeTeam_Id'],
                                                       fixturedate = fixturelive['Date'],
                                                       fixturestatus = fixturelive['Time'],
                                                       fixturematchday_id = fixturelive['Round'],
                                                       spectators = fixturelive['Spectators'],
                                                       hometeamscore = fixturelive['HomeGoals'],
                                                       awayteamscore = fixturelive['AwayGoals'],
                                                       homegoaldetails = fixturelive['HomeGoalDetails'],
                                                       awaygoaldetails = fixturelive['AwayGoalDetails'],
                                                       hometeamyellowcarddetails = fixturelive['HomeTeamYellowCardDetails'],
                                                       awayteamyellowcarddetails = fixturelive['AwayTeamYellowCardDetails'],
                                                       hometeamredcarddetails = fixturelive['HomeTeamRedCardDetails'],
                                                       awayteamredcarddetails = fixturelive['AwayTeamRedCardDetails'])
    fixtureLiveUpdate.save()

However, I just get the following error:

bash: update_live.py: line 6: syntax error near unexpected token `('

What I am after is using looking to create the python file and then use crontab to run it at predetermined times.

If anyone can offer any advice on this it would be appreciated.

Many thanks, Alan.

7
  • 1
    You're doing it wrong. You cannot take a django component, strip Django and expect it to work (by the way, in your example StraightredFixtureLive is a Django model so you should have removed it by that account). For launching Django-based code from cron, you should make it a management command Commented Oct 30, 2016 at 17:22
  • Understood sir, will go have a read of that. Commented Oct 30, 2016 at 17:30
  • Great points all round and understand what to do. Could you put it as an answer so I can accept the advice. Many thanks, Alan. Commented Oct 30, 2016 at 17:36
  • Explaining with a full answer is more time than I am willing to spend, especially if you understood what to do, and so already got the value from the advice. Good implementing! Commented Oct 30, 2016 at 17:56
  • All I meant was just copy and paste what you put. It made complete sense and may help someone in the future. They will goto the green tick and read rather than read the comments lol Commented Oct 30, 2016 at 18:44

1 Answer 1

1

If you want create script use your django project start script by:

import os
import sys
import django

#Don't forget '/' at end in path.
PATH_PROJECT = os.path.dirname('/path/to/your/project/')
sys.path.append(PATH_PROJECT) 

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
django.setup()

from straightred.models import StraightredFixtureLive

## many operations.

After add cronjob for this script.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Anton, will look to try this after work tomorrow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.