-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadsis.py
136 lines (122 loc) · 4.15 KB
/
threadsis.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
from django.db.utils import IntegrityError
from transferguideapp.models import InternalCourse
import requests
import queue
import threading
import time
url = "https://sisuva.admin.virginia.edu/psc/ihprd/UVSS/SA/s/WEBLIB_HCX_CM.H_CLASS_SEARCH.FieldFormula.IScript_ClassSearchOptions?institution=UVA01"
sr = requests.get(url, timeout=15).json()
subjects = []
for i in range(len(sr['subjects'])):
curr_subject = sr['subjects'][i]['subject']
if curr_subject not in subjects:
subjects.append(curr_subject)
url = 'https://sisuva.admin.virginia.edu/psc/ihprd/UVSS/SA/s/WEBLIB_HCX_CM.H_CLASS_SEARCH.FieldFormula.IScript_ClassSearch?institution=UVA01'
page = '&page='
subject = '&subject='
term = '&term='
terms = ['1238', '1232', '1228', '1222', '1218', '1212', '1208', '1202'] # Term format = 1 + year + [2 for spring 8 for fall]
def get_json(s):
i = 0
r = [{}]
for t in terms:
timeouts = 0
while r != []:
i += 1
print(url + subject + s + page + str(i) + term + t)
try:
r = requests.get(url + subject + s + page + str(i) + term + t, timeout=30).json()
except Exception as error:
print("timeout error. probably caused by network. small quantity of classes may not have been processed. Total timeouts: {timeouts}")
i -= 1
timeouts += 1
continue
#print(r)
queue_lock.acquire()
json_queue.put(r)
queue_lock.release()
if timeouts>=5:
print("Too many timeouts")
break
else:
i = 0
exitFlag = 0
def process():
while not exitFlag:
queue_lock.acquire()
if not json_queue.empty():
data = json_queue.get()
queue_lock.release()
for c in data:
id = c['crse_id']
mnemonic = c['subject']
course_number = c['catalog_nbr']
course_name = c['descr']
try:
InternalCourse.objects.update_or_create(id=id,mnemonic=mnemonic, course_number=course_number, course_name=course_name)
except IntegrityError:
continue
else:
queue_lock.release()
time.sleep(1)
def request_fact(threadName, q):
while not exitFlag:
course_lock.acquire()
if not q.empty():
data = q.get()
course_lock.release()
get_json(data)
else:
course_lock.release()
time.sleep(1)
class requestThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting " + self.name)
request_fact(self.name, self.q)
print ("Exiting " + self.name)
class dataThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting " + self.name)
process()
print ("Exiting " + self.name)
course_queue = queue.Queue(300)
for c in subjects:
course_queue.put(c)
course_lock = threading.Lock()
json_queue = queue.Queue()
queue_lock = threading.Lock()
json1 = requestThread(1, "request1", course_queue)
json2 = requestThread(2, "request2", course_queue)
json3 = requestThread(3, "request3", course_queue)
json1.start()
json2.start()
json3.start()
json4 = requestThread(4, "request4", course_queue)
json5 = requestThread(5, "request5", course_queue)
json6 = requestThread(6, "request6", course_queue)
json4.start()
json5.start()
json6.start()
json7 = requestThread(7, "request7", course_queue)
json8 = requestThread(8, "request8", course_queue)
json9 = requestThread(9, "request9", course_queue)
json7.start()
json8.start()
json9.start()
data = dataThread(0, "data", json_queue)
data.start()