Skip to content

Commit

Permalink
initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
YashNuhash committed Feb 4, 2024
1 parent ad6b7eb commit 0983f09
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env_Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CF_HANDLE=your_codeforces_handle
CF_PASSWORD=your_codeforces_password
LC_USERNAME=your_leetcode_username
LC_PASSWORD=your_leetcode_password
AC_USERNAME=your_atcoder_username
AC_PASSWORD=your_atcoder_password
GH_TOKEN=your_github_token
30 changes: 30 additions & 0 deletions .github/workflows/upload_solution.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CodeVault

on:
push:
branches:
- main
pull_request:

jobs:
upload_solution:
runs-on: ubuntu-latest

env:
$(grep -v '^#' .env | xargs -0)

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install Dependencies
run: |
pip install requests
- name: Run Upload Script
run: python .github/scripts/upload_solution.py
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# .gitignore
.env
config.json
config.py
113 changes: 113 additions & 0 deletions scripts/upload_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import os
from src.codeforces.api import get_accepted_submissions as cf_get_accepted_submissions, submit_solution as cf_submit_solution
# from leetcode.api import get_accepted_submissions as lc_get_accepted_submissions, submit_solution as lc_submit_solution
# from atcoder.api import get_accepted_submissions as ac_get_accepted_submissions, submit_solution as ac_submit_solution

def upload_codeforces_solutions(handle, password, github_token):
submissions = cf_get_accepted_submissions(handle, password)

for submission in submissions:
problem_name = submission['problem']['name']

# Print the entire submission for debugging
print(f"Submission for problem {problem_name}: {submission}")

# Try to get 'sourceCode' or 'source', if not found, print the submission for further inspection
solution_code = submission.get('sourceCode') or submission.get('source')

if not solution_code:
print(f"Error: Could not find source code for problem {problem_name}")
print(f"Full submission details: {submission}")
continue # Skip to the next submission if source code is not found

upload_to_github(problem_name, solution_code, github_token)





# def upload_leetcode_solutions(username, password, github_token):
# submissions = lc_get_accepted_submissions(username, password)

# for submission in submissions:
# problem_name = submission['problem']['title']
# solution_code = submission['code']

# # Modify this line to match the actual function in your GitHub API module
# upload_to_github(problem_name, solution_code, github_token)

# def upload_atcoder_solutions(username, password, github_token):
# submissions = ac_get_accepted_submissions(username, password)

# for submission in submissions:
# problem_name = submission['problem']['name']
# solution_code = submission['source_code']

# # Modify this line to match the actual function in your GitHub API module
# upload_to_github(problem_name, solution_code, github_token)

def upload_to_github(problem_name, solution_code, github_token):
# GitHub repository information
owner = 'YashNuhash'
repo = 'Codes-PlayGround-GitHub-Web-Development-Bootcamp'
branch = 'main' # or your desired branch

# File details
file_path = f'{problem_name}.cpp' # adjust the file path and extension as needed

# GitHub API endpoint for creating or updating a file
api_url = f'https://api.github.com/repos/{owner}/{repo}/contents/{file_path}'

# Prepare the request headers
headers = {
'Authorization': f'Bearer {github_token}',
'Accept': 'application/vnd.github.v3+json'
}

# Check if the file already exists in the repository
existing_file_response = requests.get(api_url, headers=headers)
existing_file_data = existing_file_response.json()

if 'sha' in existing_file_data:
# File exists, update the content
content = solution_code.encode('base64').decode('utf-8')
payload = {
'message': f'Update {problem_name} solution',
'content': content,
'sha': existing_file_data['sha'],
'branch': branch
}
response = requests.put(api_url, headers=headers, json=payload)
else:
# File doesn't exist, create a new one
content = solution_code.encode('base64').decode('utf-8')
payload = {
'message': f'Added {problem_name} solution',
'content': content,
'branch': branch
}
response = requests.put(api_url, headers=headers, json=payload)

# Check the response status
if response.status_code == 200:
print(f'Successfully uploaded {problem_name} solution to GitHub.')
else:
print(f'Failed to upload {problem_name} solution to GitHub. Status code: {response.status_code}, Message: {response.text}')



def main():
cf_handle = os.environ.get('CF_HANDLE')
cf_password = os.environ.get('CF_PASSWORD')
# lc_username = os.environ.get('LC_USERNAME')
# lc_password = os.environ.get('LC_PASSWORD')
# ac_username = os.environ.get('AC_USERNAME')
# ac_password = os.environ.get('AC_PASSWORD')
gh_token = os.environ.get('GH_TOKEN')

upload_codeforces_solutions(cf_handle, cf_password, gh_token)
# upload_leetcode_solutions(lc_username, lc_password, gh_token)
# upload_atcoder_solutions(ac_username, ac_password, gh_token)

if __name__ == "__main__":
main()
Binary file added src/__pycache__/config.cpython-310.pyc
Binary file not shown.
Binary file added src/codeforces/__pycache__/api.cpython-310.pyc
Binary file not shown.
52 changes: 52 additions & 0 deletions src/codeforces/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import requests
from src.config import CF_API_KEY, CF_API_SECRET

def get_accepted_submissions(handle, password):
# Codeforces API endpoint for fetching user submissions
api_url = f'https://codeforces.com/api/user.status?handle={handle}&from=1&count=10'

# Use requests library to make the API request
response = requests.get(api_url)

# Check if the request was successful (status code 200)
if response.status_code == 200:
# Extract and return accepted submissions
submissions = [submission for submission in response.json()['result'] if submission['verdict'] == 'OK']
return submissions
else:
# Handle API request failure
print(f'Error fetching submissions. Status code: {response.status_code}')
return []

def submit_solution(handle, password, problem_id, code):
# Codeforces API endpoint for submitting a solution
api_url = 'https://codeforces.com/api/problem.submit'

# Set up the payload with necessary data
payload = {
'contestId': problem_id[0],
'index': problem_id[1],
'verdict': 'OK', # Assuming successful submission for simplicity
'programmingLanguage': 'cpp17', # Replace with the actual programming language
'source': code
}

# Set up the headers with authentication information
headers = {
'apiKey': CF_API_KEY,
'secret': CF_API_SECRET
}

# Use requests library to make the API request
response = requests.post(api_url, data=payload, headers=headers)

# Check if the request was successful (status code 200)
if response.status_code == 200:
# Extract and return submission result
return response.json()['result']
else:
# Handle API request failure
print(f'Error submitting solution. Status code: {response.status_code}')
return {'verdict': 'FAILED'} # Placeholder for failure

# Add other Codeforces-related functions as needed

0 comments on commit 0983f09

Please sign in to comment.