Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Fix for issue #6 hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
scaratozzolo committed Aug 16, 2020
1 parent e3d0570 commit c868d88
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 94 deletions.
2 changes: 1 addition & 1 deletion MouseTools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

name = "MouseTools"

__version__ = "2.0.0"
__version__ = "2.0.1"

__all__ = ["Destination", "Park", "EntertainmentVenue", "Attraction", "Entertainment", "Facility", "Character", "PointOfInterest", "DisneyDatabase"]
139 changes: 46 additions & 93 deletions MouseTools/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class DisneyDatabase:

def __init__(self, sync_on_init=True):
self.db_path = os.path.join(Path(os.path.abspath(__file__)).parent, "MouseTools.db")
conn = sqlite3.connect(self.db_path)
c = conn.cursor()
self.conn = sqlite3.connect(self.db_path)
self.c = self.conn.cursor()

self.create_last_sequence_table()
self.create_sync_table()
self.create_facilities_table()
self.create_calendar_table()

last_sequence = c.execute("""SELECT COUNT(value) FROM lastSequence""").fetchone()[0]
last_sequence = self.c.execute("""SELECT COUNT(value) FROM lastSequence""").fetchone()[0]
if last_sequence != 0:
if sync_on_init:
self.sync_database()
Expand All @@ -30,8 +30,6 @@ def __init__(self, sync_on_init=True):
self.create_facilities_channel('dlr.facilities.1_0.en_us')


conn.close()

def sync_database(self):

self.sync_facilities_channel()
Expand All @@ -42,64 +40,45 @@ def sync_database(self):

def create_last_sequence_table(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS lastSequence (channel TEXT PRIMARY KEY, value TEXT, channel_type TEXT)""")
self.c.execute("""CREATE TABLE IF NOT EXISTS lastSequence (channel TEXT PRIMARY KEY, value TEXT, channel_type TEXT)""")

self.conn.commit()

conn.commit()
conn.close()


def create_facilities_table(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()
# subType
c.execute("""CREATE TABLE IF NOT EXISTS facilities (id TEXT PRIMARY KEY, name TEXT, entityType TEXT, subType TEXT, doc_id TEXT, destination_code TEXT, park_id TEXT, resort_id TEXT, land_id TEXT, resort_area_id TEXT, entertainment_venue_id TEXT)""")
self.c.execute("""CREATE TABLE IF NOT EXISTS facilities (id TEXT PRIMARY KEY, name TEXT, entityType TEXT, subType TEXT, doc_id TEXT, destination_code TEXT, park_id TEXT, resort_id TEXT, land_id TEXT, resort_area_id TEXT, entertainment_venue_id TEXT)""")

conn.commit()
conn.close()
self.conn.commit()

def create_calendar_table(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()
# subType
c.execute("""CREATE TABLE IF NOT EXISTS calendar (id TEXT PRIMARY KEY, date TEXT, destination_code TEXT, body TEXT)""")
self.c.execute("""CREATE TABLE IF NOT EXISTS calendar (id TEXT PRIMARY KEY, date TEXT, destination_code TEXT, body TEXT)""")

conn.commit()
conn.close()
self.conn.commit()

def create_sync_table(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS sync (id TEXT PRIMARY KEY, rev TEXT, body TEXT, channel TEXT)""")

conn.commit()
conn.close()
self.c.execute("""CREATE TABLE IF NOT EXISTS sync (id TEXT PRIMARY KEY, rev TEXT, body TEXT, channel TEXT)""")

self.conn.commit()

def channel_exists(self, channel):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()
facility_channel_exists = self.c.execute("""SELECT COUNT(value) FROM lastSequence WHERE channel = '{}'""".format(channel)).fetchone()[0]
self.conn.commit()

facility_channel_exists = c.execute("""SELECT COUNT(value) FROM lastSequence WHERE channel = '{}'""".format(channel)).fetchone()[0]
conn.commit()
conn.close()

return facility_channel_exists != 0



def create_facilities_channel(self, channel):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

payload = {
"channels": channel,
"style": 'all_docs',
Expand All @@ -110,7 +89,7 @@ def create_facilities_channel(self, channel):
r = requests.post("https://realtime-sync-gw.wdprapps.disney.com/park-platform-pub/_changes?feed=normal&heartbeat=30000&style=all_docs&filter=sync_gateway%2Fbychannel", data=json.dumps(payload), headers=couchbaseHeaders())
s = json.loads(r.text)

c.execute("INSERT INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilities')", (channel, s['last_seq'],))
self.c.execute("INSERT INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilities')", (channel, s['last_seq'],))

# search for deleted: i['deleted'] or i['removed']
docs = []
Expand Down Expand Up @@ -143,7 +122,7 @@ def create_facilities_channel(self, channel):
if x != '' and x[0] != '-':
try:
x = json.loads(x)
c.execute("INSERT INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), channel,))
self.c.execute("INSERT INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), channel,))

split_id = x['_id'].split(':')
this['id'] = split_id[-1].split(';')[0].split('.')[-1]
Expand Down Expand Up @@ -188,22 +167,18 @@ def create_facilities_channel(self, channel):
except:
this['ev_id'] = None

c.execute("INSERT INTO facilities (id, name, entityType, subType, doc_id, destination_code, park_id, land_id, resort_id, resort_area_id, entertainment_venue_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (this['id'], this['name'], this['entityType'], this['sub'], this['cb_id'], this['dest_code'], this['park_id'], this['land_id'], this['resort_id'], this['ra_id'], this['ev_id'],))
self.c.execute("INSERT INTO facilities (id, name, entityType, subType, doc_id, destination_code, park_id, land_id, resort_id, resort_area_id, entertainment_venue_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (this['id'], this['name'], this['entityType'], this['sub'], this['cb_id'], this['dest_code'], this['park_id'], this['land_id'], this['resort_id'], this['ra_id'], this['ev_id'],))
except Exception as e:
# print(x)
# print(e)
continue

conn.commit()
conn.close()
self.conn.commit()

def sync_facilities_channel(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

docs = []
for row in c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'facilities'""").fetchall():
for row in self.c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'facilities'""").fetchall():
payload = {
"channels": row[0],
"style": 'all_docs',
Expand All @@ -214,17 +189,17 @@ def sync_facilities_channel(self):
r = requests.post("https://realtime-sync-gw.wdprapps.disney.com/park-platform-pub/_changes?feed=normal&heartbeat=30000&style=all_docs&since={}&filter=sync_gateway%2Fbychannel".format(row[1]), data=json.dumps(payload), headers=couchbaseHeaders())
s = json.loads(r.text)

c.execute("REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilities')", (row[0], s['last_seq'],))
self.c.execute("REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilities')", (row[0], s['last_seq'],))

# search for deleted: i['deleted'] or i['removed']

for i in s['results']:
try:
test = i['deleted']
c.execute("DELETE FROM facilities WHERE doc_id = ?", (i['id'],))
c.execute("DELETE FROM sync WHERE id = ?", (i['id'],))
self.c.execute("DELETE FROM facilities WHERE doc_id = ?", (i['id'],))
self.c.execute("DELETE FROM sync WHERE id = ?", (i['id'],))
num_id = i['id'].split(".")[-1].split(";")[0]
c.execute("DELETE FROM sync WHERE id LIKE '%facilitystatus.1_0.{}%'".format(num_id))
self.c.execute("DELETE FROM sync WHERE id LIKE '%facilitystatus.1_0.{}%'".format(num_id))
except:
this = {}
this['id'] = i['id']
Expand All @@ -250,7 +225,7 @@ def sync_facilities_channel(self):
if x != '':
try:
x = json.loads(x)
c.execute("INSERT OR REPLACE INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), row[0],))
self.c.execute("INSERT OR REPLACE INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), row[0],))

split_id = x['_id'].split(':')
this['id'] = split_id[-1].split(';')[0].split('.')[-1]
Expand Down Expand Up @@ -292,14 +267,14 @@ def sync_facilities_channel(self):
except:
this['ev_id'] = None

c.execute("INSERT OR REPLACE INTO facilities (id, name, entityType, subType, doc_id, destination_code, park_id, land_id, resort_id, resort_area_id, entertainment_venue_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (this['id'], this['name'], this['entityType'], this['sub'], this['cb_id'], this['dest_code'], this['park_id'], this['land_id'], this['resort_id'], this['ra_id'], this['ev_id'],))
self.c.execute("INSERT OR REPLACE INTO facilities (id, name, entityType, subType, doc_id, destination_code, park_id, land_id, resort_id, resort_area_id, entertainment_venue_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (this['id'], this['name'], this['entityType'], this['sub'], this['cb_id'], this['dest_code'], this['park_id'], this['land_id'], this['resort_id'], this['ra_id'], this['ev_id'],))
except Exception as e:
# print(e)
continue


conn.commit()
conn.close()
self.conn.commit()




Expand All @@ -316,10 +291,7 @@ def create_facilitystatus_channel(self, channel):
r = requests.post("https://realtime-sync-gw.wdprapps.disney.com/park-platform-pub/_changes?feed=normal&heartbeat=30000&style=all_docs&filter=sync_gateway%2Fbychannel", data=json.dumps(payload), headers=couchbaseHeaders())
s = json.loads(r.text)

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

c.execute("INSERT OR REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilitystatus')", (channel, s['last_seq'],))
self.c.execute("INSERT OR REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilitystatus')", (channel, s['last_seq'],))

# search for deleted: i['deleted'] or i['removed']
docs = []
Expand Down Expand Up @@ -352,23 +324,19 @@ def create_facilitystatus_channel(self, channel):
if x != '' and x[0] != '-':
try:
x = json.loads(x)
c.execute("INSERT INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), channel,))
self.c.execute("INSERT INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), channel,))
except Exception as e:
# print(e)
# print(x)
continue

conn.commit()
conn.close()
self.conn.commit()


def sync_facilitystatus_channel(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

docs = []
for row in c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'facilitystatus'""").fetchall():
for row in self.c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'facilitystatus'""").fetchall():
payload = {
"channels": row[0],
"style": 'all_docs',
Expand All @@ -381,7 +349,7 @@ def sync_facilitystatus_channel(self):



c.execute("REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilitystatus')", (row[0], s['last_seq'],))
self.c.execute("REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'facilitystatus')", (row[0], s['last_seq'],))

# search for deleted: i['deleted'] or i['removed']

Expand Down Expand Up @@ -414,12 +382,11 @@ def sync_facilitystatus_channel(self):
if x != '' and x[0] != '-':
try:
x = json.loads(x)
c.execute("INSERT OR REPLACE INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), row[0],))
self.c.execute("INSERT OR REPLACE INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), row[0],))
except:
continue

conn.commit()
conn.close()
self.conn.commit()



Expand All @@ -436,10 +403,7 @@ def create_today_channel(self, channel):
r = requests.post("https://realtime-sync-gw.wdprapps.disney.com/park-platform-pub/_changes?feed=normal&heartbeat=30000&style=all_docs&filter=sync_gateway%2Fbychannel", data=json.dumps(payload), headers=couchbaseHeaders())
s = json.loads(r.text)

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

c.execute("INSERT OR REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'today')", (channel, s['last_seq'],))
self.c.execute("INSERT OR REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'today')", (channel, s['last_seq'],))

docs = []
for i in s['results']:
Expand Down Expand Up @@ -469,21 +433,17 @@ def create_today_channel(self, channel):
if x != '' and x[0] != '-':
try:
x = json.loads(x)
c.execute("INSERT INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), channel,))
self.c.execute("INSERT INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), channel,))
except:
continue

conn.commit()
conn.close()
self.conn.commit()


def sync_today_channel(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

docs = []
for row in c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'today'""").fetchall():
for row in self.c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'today'""").fetchall():
payload = {
"channels": row[0],
"style": 'all_docs',
Expand All @@ -496,7 +456,7 @@ def sync_today_channel(self):



c.execute("REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'today')", (row[0], s['last_seq'],))
self.c.execute("REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'today')", (row[0], s['last_seq'],))

# search for deleted: i['deleted'] or i['removed']

Expand Down Expand Up @@ -529,25 +489,21 @@ def sync_today_channel(self):
if x != '' and x[0] != '-':
try:
x = json.loads(x)
c.execute("INSERT OR REPLACE INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), row[0],))
self.c.execute("INSERT OR REPLACE INTO sync (id, rev, body, channel) VALUES (?, ?, ?, ?)", (x['_id'], x['_rev'], json.dumps(x), row[0],))
except:
continue

conn.commit()
conn.close()
self.conn.commit()



def create_calendar_channel(self, channel):

today = datetime.datetime.today()

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

dest_code = channel.split('.')[0]

c.execute("DELETE FROM calendar WHERE destination_code = ?", (dest_code,))
self.c.execute("DELETE FROM calendar WHERE destination_code = ?", (dest_code,))

payload = {
"channels": channel,
Expand All @@ -559,7 +515,7 @@ def create_calendar_channel(self, channel):
r = requests.post("https://realtime-sync-gw.wdprapps.disney.com/park-platform-pub/_changes?feed=normal&heartbeat=30000&style=all_docs&filter=sync_gateway%2Fbychannel", data=json.dumps(payload), headers=couchbaseHeaders())
s = json.loads(r.text)

c.execute("INSERT OR REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'calendar')", (channel, s['last_seq'],))
self.c.execute("INSERT OR REPLACE INTO lastSequence (channel, value, channel_type) VALUES (?, ?, 'calendar')", (channel, s['last_seq'],))

docs = []
for i in s['results']:
Expand Down Expand Up @@ -604,20 +560,17 @@ def create_calendar_channel(self, channel):

dest_code = x['_id'].split('.')[0]

c.execute("INSERT INTO calendar (id, date, destination_code, body) VALUES (?, ?, ?, ?)", (x['_id'], date, dest_code, json.dumps(x),))
self.c.execute("INSERT INTO calendar (id, date, destination_code, body) VALUES (?, ?, ?, ?)", (x['_id'], date, dest_code, json.dumps(x),))

except Exception as e:
# print(e)
continue

conn.commit()
conn.close()
self.conn.commit()


def sync_calendar_channel(self):

conn = sqlite3.connect(self.db_path)
c = conn.cursor()

for row in c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'calendar'""").fetchall():
for row in self.c.execute("""SELECT * FROM lastSequence WHERE channel_type = 'calendar'""").fetchall():
self.create_calendar_channel(row[0])

0 comments on commit c868d88

Please sign in to comment.