├── .SmartGitAuto ├── status.csv ├── Procfile ├── requirements.txt └── main.py /.SmartGitAuto: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /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 | 65 | Qname = Qname.replace('\\"', '"') 66 | Qname = Qname.replace("“", "'") 67 | 68 | Qdesc = Qdesc.replace('\\"', '"') 69 | Qdesc = Qdesc.replace("“", "'") 70 | 71 | Code = Code.replace('\\"', '"') 72 | Code = Code.replace("“", "'") 73 | 74 | Qno = Qno[1:-1] 75 | Qname = Qname[1:-1] 76 | Qdesc = Qdesc[1:-1] 77 | Session = Session[1:-1] 78 | Code = Code[1:-1] 79 | 80 | 81 | 82 | 83 | parseAns = "#" + Qno + '\n\n' \ 84 | "#" + Qname + "(" + Session + ")" + '\n\n' + "#" + Qdesc + "\n\n\n" + Code 85 | return parseAns 86 | 87 | 88 | def ConnectGB(Code, Name): 89 | g = Github(ACCESS_TOKEN) 90 | 91 | repo = g.get_repo("Abhijith14/PythonElab") 92 | 93 | contents = repo.get_contents("") 94 | 95 | i = 0 96 | Name = Name[1:-1] 97 | Name = Name.replace(" ", "") 98 | destination = Name + ".py" 99 | Names = [] 100 | 101 | while contents: 102 | file_content = contents.pop(0) 103 | Names.append(file_content.name) 104 | 105 | restart = True 106 | 107 | while restart: 108 | for name_val in range(len(Names)): 109 | restart = False 110 | if Names[name_val] == destination: 111 | i = i + 1 112 | destination = Name + "(" + str(i) + ").py" 113 | restart = True 114 | break 115 | 116 | repo.create_file(destination, "Commit", Code, branch="main") 117 | 118 | 119 | def ConnectDB(): 120 | 121 | showStatDB() 122 | 123 | mydb = psycopg2.connect( 124 | host="HOST", 125 | database="DATABASE", 126 | user="USER", 127 | password="PASSWORD" 128 | ) 129 | 130 | mycursor = mydb.cursor() 131 | 132 | print("CONNECTED TO DB") 133 | print("----------------------------------------------------------") 134 | 135 | t = tuple(ID_Stat) 136 | 137 | SQL = "SELECT * FROM elabdata" 138 | #SQL = "SELECT * FROM elabdata WHERE id NOT IN {} ORDER BY ID ASC".format(t) 139 | 140 | mycursor.execute(SQL) 141 | 142 | ans = mycursor.fetchall() 143 | count = ID_Stat[-1] 144 | 145 | for row in ans: 146 | 147 | today = datetime.now(IST) # date.today() 148 | today = today.strftime('%Y-%m-%d') 149 | 150 | print() 151 | print("******************************") 152 | print(str(today)) 153 | print(str(Calendar)) 154 | print("******************************") 155 | print() 156 | 157 | while str(today) in str(Calendar): 158 | today = datetime.now(IST) # date.today() 159 | today = today.strftime('%Y-%m-%d') 160 | print() 161 | print("Waiting for Next Day ...") 162 | print("Date Stored : ") 163 | print(Calendar) 164 | print(str(count) + " Questions out of " + str(len(ans)) + " (" + str(len(ans) - count) + " remaining).") 165 | print() 166 | sleep(5) 167 | 168 | Calendar.append(str(today)) 169 | 170 | count = count + 1 171 | ID = row[0] 172 | Session = row[1] 173 | QNo = row[2] 174 | Qname = row[3] 175 | QDesc = row[4] 176 | Code = row[5] 177 | 178 | parsed_Code = mergeCode(QNo, Qname, QDesc, Session, Code) 179 | 180 | print("Typing Question - " + str(Qname.replace(" ", "")) + " --------- #" + str(ID) + " -------> " + str( 181 | count) + "/" + str(len(ans)) + " (" + str(len(ans) - count) + " remaining).") 182 | 183 | ConnectGB(parsed_Code, Qname) 184 | #Create_local_File(parsed_Code, Qname) 185 | 186 | # new_row = [{'ID': ID, 'Date': str(today), 'Name': Qname.replace(" ", "")}] 187 | # new_data = df1.append(new_row) 188 | # new_data.to_csv("status.csv", index=False) 189 | 190 | saveStatDB(ID, str(today), Qname.replace(" ", "")) 191 | 192 | 193 | print("Finished!!!") 194 | sleep(5) 195 | 196 | 197 | def Create_local_File(Data, Name): 198 | i = 0 199 | Name = Name.replace(" ", "") 200 | Name = Name[1:-1] 201 | destination = "Python/" + Name + ".py" 202 | 203 | while Path(destination).is_file(): 204 | i = i + 1 205 | destination = "Python/" + Name + "(" + str(i) + ").py" 206 | 207 | f = open(destination, "w+") 208 | f.write(Data) 209 | f.close() 210 | 211 | 212 | ConnectDB() 213 | --------------------------------------------------------------------------------