├── status.csv ├── Procfile ├── requirements.txt └── main.py /status.csv: -------------------------------------------------------------------------------- 1 | ID,Date,Name -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | worker: python main.py -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2020.12.5 2 | chardet==4.0.0 3 | Deprecated==1.2.11 4 | idna==2.10 5 | numpy==1.19.5 6 | pandas==1.1.5 7 | psycopg2==2.8.6 8 | PyGithub==1.54.1 9 | PyJWT==1.7.1 10 | python-dateutil==2.8.1 11 | pytz==2020.5 12 | requests==2.25.1 13 | six==1.15.0 14 | urllib3==1.26.3 15 | wrapt==1.12.1 16 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import psycopg2 2 | from github import Github 3 | from pathlib import Path 4 | from datetime import datetime 5 | from time import sleep 6 | import pandas as pd 7 | import pytz 8 | 9 | IST = pytz.timezone('Asia/Kolkata') 10 | 11 | # df1 = pd.read_csv("status.csv", delimiter=',') 12 | ID_Stat = []#df1['ID'].values.tolist() 13 | Calendar = []#df1['Date'].values.tolist() 14 | # Name_Stat = df1['Name'].values.tolist() 15 | 16 | 17 | 18 | 19 | 20 | def showStatDB(): 21 | global ID_Stat, Calendar 22 | 23 | mydb2 = psycopg2.connect( 24 | host="HOST", 25 | database="DATABASE", 26 | user="USER", 27 | password="PASSWORD" 28 | ) 29 | 30 | mycursor2 = mydb2.cursor() 31 | 32 | SQL = "SELECT * FROM status ORDER BY ID ASC" 33 | mycursor2.execute(SQL) 34 | out = mycursor2.fetchall() 35 | for row in out: 36 | ID_Stat.append(row[0]) 37 | Calendar.append(row[1]) 38 | 39 | ID_Stat = (-1, 0) + tuple(ID_Stat) 40 | print(ID_Stat) 41 | 42 | 43 | def saveStatDB(ID, DATE_STAT, QNAME): 44 | 45 | mydb2 = psycopg2.connect( 46 | host="HOST", 47 | database="DATABASE", 48 | user="USER", 49 | password="PASSWORD" 50 | ) 51 | 52 | mycursor2 = mydb2.cursor() 53 | 54 | sql2 = "INSERT INTO status(ID, DATE_STAT, NAME) VALUES (" + str(ID) + ", '" + str(DATE_STAT) + "', '" + str(QNAME) + "');" 55 | print(sql2) 56 | mycursor2.execute(sql2) 57 | mydb2.commit() 58 | 59 | 60 | def mergeCode(Qno, Qname, Qdesc, Session, Code): 61 | Qname = Qname.replace(" ", "") 62 | Session = Session.replace(" ", "") 63 | Qdesc = Qdesc.replace("
", "\n") 64 | parseAns = "//" + Qno + '\n' \ 65 | "//" + Qname + "(" + Session + ")" + '\n'\ 66 | "/*\n" + Qdesc + "\n*/\n\n" + Code 67 | return parseAns 68 | 69 | 70 | def ConnectGB(Code, Name): 71 | g = Github(ACCESS_TOKEN) 72 | 73 | repo = g.get_repo("Abhijith14/JavaElab") 74 | 75 | contents = repo.get_contents("") 76 | 77 | i = 0 78 | Name = Name.replace(" ", "") 79 | destination = Name + ".java" 80 | Names = [] 81 | 82 | while contents: 83 | file_content = contents.pop(0) 84 | Names.append(file_content.name) 85 | 86 | restart = True 87 | 88 | while restart: 89 | for name_val in range(len(Names)): 90 | restart = False 91 | if Names[name_val] == destination: 92 | i = i + 1 93 | destination = Name + "(" + str(i) + ").java" 94 | restart = True 95 | break 96 | 97 | repo.create_file(destination, "Commit", Code, branch="main") 98 | 99 | 100 | def ConnectDB(): 101 | 102 | showStatDB() 103 | 104 | mydb = psycopg2.connect( 105 | host="HOST", 106 | database="DATABASE", 107 | user="USER", 108 | password="PASSWORD" 109 | ) 110 | 111 | mycursor = mydb.cursor() 112 | 113 | print("CONNECTED TO DB") 114 | print("----------------------------------------------------------") 115 | 116 | t = tuple(ID_Stat) 117 | 118 | SQL = "SELECT * FROM elabdata WHERE ID NOT IN {} ORDER BY ID ASC".format(t) 119 | 120 | mycursor.execute(SQL) 121 | 122 | ans = mycursor.fetchall() 123 | count = ID_Stat[-1] 124 | 125 | for row in ans: 126 | 127 | today = datetime.now(IST)#date.today() 128 | today = today.strftime('%Y-%m-%d') 129 | 130 | print() 131 | print("******************************") 132 | print(str(today)) 133 | print(str(Calendar)) 134 | print("******************************") 135 | print() 136 | 137 | while str(today) in str(Calendar): 138 | today = datetime.now(IST) # date.today() 139 | today = today.strftime('%Y-%m-%d') 140 | print() 141 | print("Waiting for Next Day ...") 142 | print("Date Stored : ") 143 | print(Calendar) 144 | print(str(count) + " Questions out of " + str(len(ans)) + " (" + str(len(ans) - count) + " remaining).") 145 | print() 146 | sleep(5) 147 | 148 | Calendar.append(str(today)) 149 | 150 | count = count + 1 151 | Code = row[0] 152 | QDesc = row[1] 153 | Qname = row[2] 154 | QNo = row[3] 155 | Session = row[4] 156 | ID = row[5] 157 | 158 | parsed_Code = mergeCode(QNo, Qname, QDesc, Session, Code) 159 | 160 | print("Typing Question - " + str(Qname.replace(" ", "")) + " --------- #" + str(ID) + " -------> " + str( 161 | count) + "/" + str(len(ans)) + " (" + str(len(ans) - count) + " remaining).") 162 | 163 | ConnectGB(parsed_Code, Qname) 164 | #Create_local_File(parsed_Code, Qname) 165 | 166 | # new_row = [{'ID': ID, 'Date': str(today), 'Name': Qname.replace(" ", "")}] 167 | # new_data = df1.append(new_row) 168 | # new_data.to_csv("status.csv", index=False) 169 | 170 | saveStatDB(ID, str(today), Qname.replace(" ", "")) 171 | 172 | print("Finished!!!") 173 | sleep(5) 174 | 175 | 176 | def Create_local_File(Data, Name): 177 | i = 0 178 | Name = Name.replace(" ", "") 179 | destination = "Java/" + Name + ".java" 180 | 181 | while Path(destination).is_file(): 182 | i = i + 1 183 | destination = "Java/" + Name + "(" + str(i) + ").java" 184 | 185 | f = open(destination, "w+") 186 | f.write(Data) 187 | f.close() 188 | 189 | 190 | ConnectDB() 191 | --------------------------------------------------------------------------------