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.

Alan Tingey
  • 835
  • 11
  • 39
  • 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](https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/) – spectras Oct 30 '16 at 17:22
  • Understood sir, will go have a read of that. – Alan Tingey Oct 30 '16 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. – Alan Tingey Oct 30 '16 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! – spectras Oct 30 '16 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 – Alan Tingey Oct 30 '16 at 18:44
  • Don't run your command with **bash filename.py**, but **python filename.py** – Antti Haapala -- Слава Україні Oct 30 '16 at 19:27
  • Many thanks for the help Antti, I will try this tomorrow after work. – Alan Tingey Oct 31 '16 at 22:04

1 Answers1

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.

Anton
  • 504
  • 2
  • 6