Преглед изворни кода

Get the starting date correct (in stead of default 1970-01-01)

Foppe Hemminga пре 6 година
родитељ
комит
a67077f489
2 измењених фајлова са 25 додато и 7 уклоњено
  1. 8 6
      _bs.py
  2. 17 1
      model.py

+ 8 - 6
_bs.py

@@ -7,7 +7,6 @@ import collections
 # import pytz
 from pprint import pprint
 
-
 """
 This module contains custom methods based on bs4.beautifulsoup to analyze data
 """
@@ -66,8 +65,9 @@ def get_today_row(this_text, this_row):
                         # Create the Scratchings URL by substitution
                         scratchings_url = re.sub(r"/(.*)\.aspx", 'Scratchings.aspx', this_url)
                         scratchings_url = base_url + scratchings_url
+                        calculated_date = model.convert_to_date(date_string)
                         this_race_day = RaceDayShort(this_venue.state, this_venue.name, date_string,
-                                                     '1970-01-01', scratchings_url)
+                                                     calculated_date.strftime('%Y-%m-%d'), scratchings_url)
                         all_race_days.append(this_race_day)
             i += 1
     return all_race_days
@@ -83,6 +83,7 @@ def get_meta_data(this_data, this_venue):
     :param this_venue:
     :return:
     """
+    global print_it
     this_soup = BeautifulSoup(this_data, 'html.parser')
     early = this_soup.select('div.large')
     # if early:
@@ -114,10 +115,11 @@ def get_meta_data(this_data, this_venue):
             # print(times[1])
         match = close_regex.search(this_meta_data)
         utctime = datetime.datetime.utcnow()
-        if (utctime.hour in [15,16,17,18] and utctime.minute > 49) or \
-            (utctime.hour in [16,17,18,19] and utctime.minute < 11):
+        if (utctime.hour in (15, 16, 17, 18) and utctime.minute > 49) or \
+                (utctime.hour in (16, 17, 18, 19) and utctime.minute < 11):
             print_it = True
             print('utctime: {}'.format(utctime))
+
         if match:
             print_it = False
             if '-19 ' not in match.group(1):
@@ -139,14 +141,14 @@ def get_meta_data(this_data, this_venue):
             if print_it or times[3] < 1e6:
                 print_it = True
                 print('datetime.date.fromtimestamp(times[3]+12*60*60): {}'.format(
-                    datetime.date.fromtimestamp(times[3]+12*60*60)))
+                    datetime.date.fromtimestamp(times[3] + 12 * 60 * 60)))
         elif print_it:
             print('this_meta_data: {}'.format(this_meta_data))
             print('No match for regex: {}'.format(close_regex))
             pprint(times)
     # The RaceDAy namedTuple is created and filled
     race_day = RaceDay(this_venue.state, this_venue.name, this_venue.date_string,
-                       datetime.date.fromtimestamp(times[3]+12*60*60),
+                       datetime.date.fromtimestamp(times[3] + 12 * 60 * 60),
                        this_venue.scratchings_url, times[0], times[1], times[2], times[3])
     return race_day
 

+ 17 - 1
model.py

@@ -1,9 +1,11 @@
+import re
+
 import _html
 import _bs
 import pytz
 import datetime
 # import time
-import psycopg2.extras
+# import psycopg2.extras
 
 
 """
@@ -63,3 +65,17 @@ def convert_to_unixtime(dt_object):
     epoch = datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
     ts = int((d - epoch).total_seconds())
     return ts
+
+
+def convert_to_date(weird_string):
+    """
+    Converts a string like 'MONDAY 15 JUL' to a python datetime object
+    :param weird_string:
+    :return datetime object:
+    """
+    weird_string = re.sub(r' (\d) ', ' 0\1 ', weird_string)
+    local_timezone = pytz.timezone('Australia/Sydney')
+    now = datetime.datetime.now(local_timezone)
+    calculated_date = datetime.datetime.strptime(str(now.year) + ' ' + weird_string, "%Y %A %d %b").date()
+    print(calculated_date)
+    return calculated_date