-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdatabase.py
More file actions
73 lines (49 loc) · 1.86 KB
/
Copy pathdatabase.py
File metadata and controls
73 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sketchify
import sketchy_data
import random
from google.cloud import datastore
class URLStoreModel():
def __init__(self):
datastore_client = datastore.Client(
project="verylegitlink")
self.db = datastore_client
# self.initial_setup()
def initial_setup(self):
if not self.get_sketchy_url(random.choice(sketchy_data.SAMPLE_LONG_URLS)):
for sample_long_url in sketchy_data.SAMPLE_LONG_URLS:
self.add(sample_long_url)
def add(self, long_url):
sketchy_url = sketchify.generate_sketchy_url()
self.set_url(long_url, sketchy_url)
def set_url(self, long_url, sketchy_url):
with self.db.transaction():
incomplete_key = self.db.key("URL")
url = datastore.Entity(key=incomplete_key)
url.update({
"long_url": long_url,
"sketchy_url": sketchy_url
})
self.db.put(url)
print("Setting " + long_url + " -> " + sketchy_url)
def get_long_url(self, sketchy_url):
query = self.db.query(kind="URL")
query.add_filter("sketchy_url", "=", sketchy_url)
result = list(query.fetch(limit=1))
# None if we don't have this URL already
if not result:
print("Getting: " + sketchy_url + " -> " + str(result))
return None
long_url = result[0]["long_url"]
return long_url
def get_sketchy_url(self, long_url):
query = self.db.query(kind="URL")
query.add_filter("long_url", "=", long_url)
result = list(query.fetch(limit=1))
print(result)
if not result:
print("Getting: " + long_url + " -> None")
return None
sketchy_url = result[0]["sketchy_url"]
return sketchy_url
if __name__ == "__main__":
URLStoreModel().initial_setup()