├── .eslintrc.json ├── .github └── workflows │ └── docs_publish.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── docs ├── acknowledgments.md ├── customization.md ├── images │ ├── favicon.ico │ └── logo.png ├── index.md └── themes.md ├── jest.config.js ├── jsconfig.json ├── mkdocs.yml ├── next.config.js ├── package-lock.json ├── package.json ├── public ├── android-chrome-192x192.png ├── android-chrome-512x512.png ├── apple-touch-icon.png ├── bg.png ├── favicon-16x16.png ├── favicon-32x32.png ├── favicon.ico ├── google873d1dd2194f40f4.html ├── robots.txt ├── site.webmanifest └── sitemap.xml ├── src ├── common.js ├── fetcher-utils.js ├── fetcher.js ├── hooks │ └── option.js ├── images │ ├── error.svg │ └── logo.png ├── pages │ ├── _app.js │ ├── _document.js │ ├── api │ │ ├── badge.js │ │ └── card.js │ ├── index.js │ └── styles.css ├── templates │ ├── badge.svg │ └── card.svg └── themes.js ├── test ├── badge.test.js ├── card.test.js ├── common.test.js └── test-data.js └── vercel.json /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /.github/workflows/docs_publish.yml: -------------------------------------------------------------------------------- 1 | name: docs_publish 2 | on: 3 | push: 4 | branches: 5 | - master 6 | - main 7 | permissions: 8 | contents: write 9 | jobs: 10 | deploy: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | - uses: actions/setup-python@v4 15 | with: 16 | python-version: 3.x 17 | - uses: actions/cache@v2 18 | with: 19 | key: ${{ github.ref }} 20 | path: .cache 21 | - run: pip install mkdocs-material 22 | - run: mkdocs gh-deploy --force -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | .pnpm-debug.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | .vscode -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to [Codeforces readme stats](https://github.com/RedHeadphone/codeforces-readme-stats) 2 | 3 | We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's: 4 | 5 | - Reporting an issue 6 | - Discussing the current state of the code 7 | - Submitting a fix 8 | - Proposing new features 9 | 10 | ## All Changes Happen Through Pull Requests 11 | 12 | Pull requests are the best way to propose changes. We actively welcome your pull requests: 13 | 14 | 1. Fork the repo and create your branch from `master`. 15 | 2. If you've added code that should be tested, add some tests. 16 | 3. If you've changed APIs, update the documentation. 17 | 4. Issue that pull request! 18 | 19 | ## Local Development 20 | 21 | To run & test codeforces-readme-stats, you need to follow a few simple steps:- 22 | 23 | 1. Fork the repository and clone the code to your local machine. 24 | 2. Run `npm install` in the repository root. 25 | 3. Run the command `npm run dev` to start the development server at . 26 | 4. To run the development server for docs use command `mkdocs serve` (setup it using command `pip install mkdocs mkdocs-material`) 27 | 28 | ## Themes Contribution 29 | 30 | Codeforces Readme Stats supports custom theming, and you can also contribute new themes! 31 | 32 | All you need to do is edit the [src/themes.js](./src/themes.js) file and add your theme at the end of the file. 33 | 34 | While creating the Pull request to add a new theme **don't forget to add a screenshot of how your theme looks**, you can also test how it looks using custom URL parameters like `title_color`, `icon_color`, `bg_color`, `text_color`, `border_color` 35 | 36 | ## Any contributions you make will be under the MIT Software License 37 | 38 | In short, when you submit changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern. 39 | 40 | ## Report issues/bugs using GitHub's [issues](https://github.com/RedHeadphone/codeforces-readme-stats/issues) 41 | 42 | We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/RedHeadphone/codeforces-readme-stats/issues/new/choose); it's that easy! 43 | 44 | ### Bug Reports 45 | 46 | **Great Bug Reports** tend to have: 47 | 48 | - A quick summary and/or background 49 | - Steps to reproduce 50 | - Be specific! 51 | - Share the snapshot, if possible. 52 | - What actually happens 53 | - What you expected would happen 54 | - Notes (possibly including why you think this might be happening or stuff you tried that didn't work) 55 | 56 | People _love_ thorough bug reports. I'm not even kidding. 57 | 58 | ### Feature Request 59 | 60 | **Great Feature Requests** tend to have: 61 | 62 | - A quick idea summary 63 | - What & why do you want to add the specific feature 64 | - Additional context like images, links to resources to implement the feature, etc. 65 | 66 | ## License 67 | 68 | By contributing, you agree that your contributions will be licensed under its [MIT License](./LICENSE). 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Huzaifa Khilawala 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Codeforces Readme Stats 2 | 3 | An API that generates beautiful statistics of your Codeforces profile as an SVG image, perfect for showcasing your competitive programming skills and achievements on Github. 4 | 5 | Check the [docs](https://redheadphone.github.io/codeforces-readme-stats/) for information on how to use the API. 6 | 7 | ## Features 8 | 9 | - 📈 Clean and simple Codeforces stats 10 | - 🎨 Multiple themes - [Theme](docs/themes.md) 11 | - ⚙️ Fully customizable - [Customization](docs/customization.md) 12 | - 🍀 Open source - [MIT License](https://github.com/RedHeadphone/codeforces-readme-stats/blob/master/LICENSE) 13 | 14 | Want to contribute? Feel free to open a pull request! 15 | 16 | ## Usage 17 | 18 | ### Stats card 19 | 20 | Simply copy the code below, paste it into your `README.md`, and change the username query parameter to your Codeforces username (case-insensitive). 21 | 22 | ```md 23 | ![Codeforces Stats](https://codeforces-readme-stats.vercel.app/api/card?username=redheadphone) 24 | ``` 25 | 26 | Preview: 27 | 28 |

29 | Codeforces Stats 30 |

31 | 32 | Want a hyperlink? Try this: 33 | 34 | ```md 35 | [![Codeforces Stats](https://codeforces-readme-stats.vercel.app/api/card?username=redheadphone)](https://codeforces.com/profile/redheadphone) 36 | ``` 37 | 38 | ### Rating badge 39 | 40 | Rating badge can also be added to your `README.md`, by coping and pasting the code below 41 | 42 | ```md 43 | ![Codeforces Badge](https://codeforces-readme-stats.vercel.app/api/badge?username=redheadphone) 44 | ``` 45 | 46 | Preview: 47 | 48 |

49 | Codeforces Badge 50 |

51 | 52 | ## Contributing 53 | 54 | Please kindly follow [CONTRIBUTING.md](CONTRIBUTING.md) to get started. 55 | 56 | ## Acknowledgements 57 | 58 | ### Inspirations 59 | 60 | - [sudiptob2/cf-stats](https://github.com/sudiptob2/cf-stats): SVG design 61 | - [anuraghazra/github-readme-stats](https://github.com/anuraghazra/github-readme-stats): API design and themes 62 | - [JacobLinCool/LeetCode-Stats-Card](https://github.com/JacobLinCool/LeetCode-Stats-Card): Miscellaneous -------------------------------------------------------------------------------- /docs/acknowledgments.md: -------------------------------------------------------------------------------- 1 | 2 | ### Inspirations 3 | 4 | - [sudiptob2/cf-stats](https://github.com/sudiptob2/cf-stats): SVG design 5 | - [anuraghazra/github-readme-stats](https://github.com/anuraghazra/github-readme-stats): API design and themes 6 | - [JacobLinCool/LeetCode-Stats-Card](https://github.com/JacobLinCool/LeetCode-Stats-Card): Miscellaneous -------------------------------------------------------------------------------- /docs/customization.md: -------------------------------------------------------------------------------- 1 | You can customize the appearance of your `Stats Card` however you wish with URL parameters. 2 | 3 | #### Common Options 4 | 5 | - `title_color` - Card's title color _(hex color)_. Default: `2f80ed`. 6 | - `text_color` - Body text color _(hex color)_. Default: `434d58`. 7 | - `icon_color` - Icons color if available _(hex color)_. Default: `4c71f2`. 8 | - `border_color` - Card's border color _(hex color)_. Default: `e4e2e2` 9 | - `bg_color` - Card's background color _(hex color)_. Default: `fffefe` 10 | - `theme` - Name of the theme, choose from [all available themes](./themes.md). Default: `default` theme. 11 | - `force_username` - Forces the username to be shown in the card _(boolean)_. Default: `false`. 12 | - `disable_animations` - Disables all animations in the card _(boolean)_. Default: `false`. 13 | - `show_icons` - Shows icons in the card _(boolean)_. Default: `true`. 14 | - `cache_seconds` - Set the cache header manually _(min: 14400, max: 86400)_. Default: `14400 seconds (4 hours)`. 15 | -------------------------------------------------------------------------------- /docs/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/docs/images/favicon.ico -------------------------------------------------------------------------------- /docs/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/docs/images/logo.png -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | 2 | # Codeforces Readme Stats 3 | 4 | An API that generates beautiful statistics of your Codeforces profile as an SVG image, perfect for showcasing your competitive programming skills and achievements on Github. 5 | 6 | ## Features 7 | 8 | - 📈 Clean and simple Codeforces stats 9 | - 🎨 Multiple themes - [Theme](./themes.md) 10 | - ⚙️ Fully customizable - [Customization](./customization.md) 11 | - 🍀 Open source - [MIT License](https://github.com/RedHeadphone/codeforces-readme-stats/blob/master/LICENSE) 12 | 13 | Want to contribute? Feel free to open a pull request! 14 | 15 | ## Usage 16 | 17 | ### Stats card 18 | 19 | Simply copy the code below, paste it into your `README.md`, and change the username query parameter to your Codeforces username (case-insensitive). 20 | 21 | ```md 22 | ![Codeforces Stats](https://codeforces-readme-stats.vercel.app/api/card?username=redheadphone) 23 | ``` 24 | 25 | Preview: 26 | 27 |

28 | Codeforces Stats 29 |

30 | 31 | Want a hyperlink? Try this: 32 | 33 | ```md 34 | [![Codeforces Stats](https://codeforces-readme-stats.vercel.app/api/card?username=redheadphone)](https://codeforces.com/profile/redheadphone) 35 | ``` 36 | 37 | ### Rating badge 38 | 39 | Rating badge can also be added to your `README.md`, by coping and pasting the code below 40 | 41 | ```md 42 | ![Codeforces Badge](https://codeforces-readme-stats.vercel.app/api/badge?username=redheadphone) 43 | ``` 44 | 45 | Preview: 46 | 47 |

48 | Codeforces Badge 49 |

50 | -------------------------------------------------------------------------------- /docs/themes.md: -------------------------------------------------------------------------------- 1 | 2 | With inbuilt themes, you can customize the look of the card without doing any [manual customization](./customization.md). 3 | 4 | Use `&theme=THEME_NAME` parameter like so : 5 | 6 | ```md 7 | ![Codeforces Stats](https://codeforces-readme-stats.vercel.app/api/card?username=redheadphone&theme=radical) 8 | ``` 9 | 10 | ### All inbuilt themes 11 | 12 | Codeforces readme stats comes with several built-in themes (e.g. `dark`, `radical`, `merko`, `gruvbox`, `tokyonight`, `onedark`, `cobalt`, `synthwave`, `highcontrast`, `dracula`). 13 | 14 |

15 | 16 | 17 | 18 | 19 |

20 | 21 | You can look at all available themes from the [theme config file](https://github.com/RedHeadphone/codeforces-readme-stats/blob/master/src/themes.js) & **you can also contribute new themes** if you like :D -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | testEnvironment: "jsdom", 3 | transform: {}, 4 | testPathIgnorePatterns: ["/node_modules/"], 5 | modulePathIgnorePatterns: ["/node_modules/"], 6 | coveragePathIgnorePatterns: ["/node_modules/"], 7 | moduleNameMapper: { 8 | "^@/(.*)$": "/src/$1", 9 | }, 10 | }; 11 | -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "paths": { 5 | "@/*": ["./src/*"] 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: Codeforces Readme Stats 2 | site_description: Dynamically generated Codeforces stats for your Github profiles or your website 3 | site_url: https://redheadphone.github.io/codeforces-readme-stats/ 4 | site_author: RedHeadphone 5 | repo_name: codeforces-readme-stats 6 | repo_url: https://github.com/RedHeadphone/codeforces-readme-stats/ 7 | theme: 8 | name: material 9 | language: en 10 | palette: 11 | - scheme: default 12 | primary: black 13 | accent: green 14 | toggle: 15 | icon: material/toggle-switch-off-outline 16 | name: Switch to light mode 17 | 18 | - scheme: slate 19 | primary: default 20 | accent: green 21 | toggle: 22 | icon: material/toggle-switch 23 | name: Switch to dark mode 24 | 25 | logo: images/logo.png 26 | favicon: images/favicon.ico 27 | features: 28 | - content.code.annotate 29 | - content.code.copy 30 | 31 | extra: 32 | social: 33 | - icon: fontawesome/brands/github 34 | link: https://github.com/RedHeadphone 35 | name: RedHeadphone on Github 36 | - icon: fontawesome/brands/twitter 37 | link: https://twitter.com/huzaifa_Khila 38 | name: Huzaifa Khilawala on Twitter 39 | - icon: fontawesome/brands/linkedin 40 | link: https://www.linkedin.com/in/huzaifa-khilawala/ 41 | name: Huzaifa Khilawala on Linkedin 42 | 43 | 44 | copyright: Copyright © 2023 Huzaifa Khilawala 45 | 46 | markdown_extensions: 47 | - admonition 48 | - codehilite 49 | - attr_list 50 | 51 | plugins: 52 | - search 53 | 54 | nav: 55 | - About: index.md 56 | - Themes: themes.md 57 | - Customization: customization.md 58 | - Acknowledgments: acknowledgments.md -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | reactStrictMode: true, 4 | images: { 5 | dangerouslyAllowSVG: true, 6 | domains: ['codeforces-readme-stats.vercel.app','localhost'], 7 | }, 8 | } 9 | 10 | export default nextConfig 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codeforces-readme-stats", 3 | "version": "1.0.0", 4 | "type": "module", 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --silent", 10 | "lint": "next lint" 11 | }, 12 | "dependencies": { 13 | "@next/font": "13.1.6", 14 | "@vercel/kv": "^2.0.0", 15 | "antd": "^5.3.1", 16 | "axios": "^1.0.0", 17 | "axios-cache-interceptor": "^1.5.3", 18 | "eslint": "8.33.0", 19 | "eslint-config-next": "13.1.6", 20 | "fast-querystring": "^1.1.1", 21 | "next": "13.1.6", 22 | "nunjucks": "^3.2.3", 23 | "react": "18.2.0", 24 | "react-dom": "18.2.0", 25 | "use-local-storage-state": "^19.4.0" 26 | }, 27 | "devDependencies": { 28 | "@types/node": "18.11.18", 29 | "@types/react": "18.0.27", 30 | "jest": "^29.7.0", 31 | "jest-environment-jsdom": "^29.4.3" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /public/android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/android-chrome-192x192.png -------------------------------------------------------------------------------- /public/android-chrome-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/android-chrome-512x512.png -------------------------------------------------------------------------------- /public/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/apple-touch-icon.png -------------------------------------------------------------------------------- /public/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/bg.png -------------------------------------------------------------------------------- /public/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/favicon-16x16.png -------------------------------------------------------------------------------- /public/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/favicon-32x32.png -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/public/favicon.ico -------------------------------------------------------------------------------- /public/google873d1dd2194f40f4.html: -------------------------------------------------------------------------------- 1 | google-site-verification: google873d1dd2194f40f4.html -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * -------------------------------------------------------------------------------- /public/site.webmanifest: -------------------------------------------------------------------------------- 1 | { 2 | "name": "CodeForces Readme Stats", 3 | "short_name": "CodeForces Readme Stats", 4 | "icons": [ 5 | { 6 | "src": "/android-chrome-192x192.png", 7 | "sizes": "192x192", 8 | "type": "image/png" 9 | }, 10 | { 11 | "src": "/android-chrome-512x512.png", 12 | "sizes": "512x512", 13 | "type": "image/png" 14 | } 15 | ], 16 | "theme_color": "#ffffff", 17 | "background_color": "#ffffff", 18 | "display": "standalone" 19 | } 20 | -------------------------------------------------------------------------------- /public/sitemap.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | https://codeforces-readme-stats.vercel.app/ 6 | 1.0 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/common.js: -------------------------------------------------------------------------------- 1 | import nunjucks from "nunjucks"; 2 | import path from "path"; 3 | 4 | export function renderTemplate(template, data) { 5 | nunjucks.configure(path.join(process.cwd(), "src/templates"), { 6 | autoescape: true, 7 | }); 8 | return nunjucks.render(template, data); 9 | } 10 | 11 | export const COLORS = { 12 | NEWBIE: "#8b898b", 13 | PUPIL: "#0fdb0f", 14 | SPECIALIST: "#55d3ab", 15 | EXPERT: "#2e2eff", 16 | CANDIDATE_MASTER: "#fc3dff", 17 | MASTER: "#ffbd66", 18 | INTERNATIONAL_MASTER: "#ffaf38", 19 | GRANDMASTER: "#fe5858", 20 | INTERNATIONAL_GRANDMASTER: "#ff0000", 21 | }; 22 | 23 | export function get_color_from_rating(rank) { 24 | switch (true) { 25 | case rank < 1200: 26 | return COLORS.NEWBIE; 27 | case rank < 1400: 28 | return COLORS.PUPIL; 29 | case rank < 1600: 30 | return COLORS.SPECIALIST; 31 | case rank < 1900: 32 | return COLORS.EXPERT; 33 | case rank < 2100: 34 | return COLORS.CANDIDATE_MASTER; 35 | case rank < 2300: 36 | return COLORS.MASTER; 37 | case rank < 2400: 38 | return COLORS.INTERNATIONAL_MASTER; 39 | case rank < 2600: 40 | return COLORS.GRANDMASTER; 41 | default: 42 | return COLORS.INTERNATIONAL_GRANDMASTER; 43 | } 44 | } 45 | 46 | export const CONSTANTS = { 47 | THIRTY_MINUTES: 1800, 48 | TWO_HOURS: 7200, 49 | FOUR_HOURS: 14400, 50 | ONE_DAY: 86400, 51 | }; 52 | 53 | export const clamp_value = (number, min, max) => { 54 | if (Number.isNaN(parseInt(number))) return min; 55 | return Math.max(min, Math.min(number, max)); 56 | }; 57 | 58 | export const capitalize = (str) => { 59 | const words = str.split(" "); 60 | const capitalized_words = words.map( 61 | (word) => word.charAt(0).toUpperCase() + word.slice(1) 62 | ); 63 | return capitalized_words.join(" "); 64 | }; 65 | -------------------------------------------------------------------------------- /src/fetcher-utils.js: -------------------------------------------------------------------------------- 1 | import Axios from "axios"; 2 | import { setupCache } from "axios-cache-interceptor"; 3 | import { kv } from "@vercel/kv"; 4 | 5 | const instance = Axios.create({ 6 | baseURL: "https://codeforces.com/api", 7 | headers: { 8 | "User-Agent": "Codeforces Readme Stats", 9 | }, 10 | }); 11 | 12 | export const api = setupCache(instance); 13 | 14 | class KVCache { 15 | constructor(type) { 16 | this.type = type; 17 | } 18 | 19 | get(key) { 20 | return kv.get(key+"/"+this.type); 21 | } 22 | 23 | set(key, value) { 24 | kv.set(key+"/"+this.type, value); 25 | } 26 | } 27 | 28 | export const last_rating_cache = new KVCache("rating"); 29 | export const last_stats_cache = new KVCache("stats"); 30 | -------------------------------------------------------------------------------- /src/fetcher.js: -------------------------------------------------------------------------------- 1 | import { capitalize } from "@/common.js"; 2 | import { api, last_rating_cache, last_stats_cache } from "@/fetcher-utils.js"; 3 | 4 | function fetch_error_handler(fetch, username, last_cache) { 5 | return new Promise((resolve, reject) => { 6 | const timeoutID = setTimeout(function () { 7 | last_cache.get(username).then((res) => { 8 | if (res != null) { 9 | console.log("Using cached data for", username); 10 | resolve(res); 11 | } 12 | else reject({ status: 500, error: "Codeforces Server Error" }); 13 | }).catch((error) => { 14 | console.error(error); 15 | reject({ status: 500, error: "Codeforces Server Error" }); 16 | }); 17 | 18 | }, 3000); 19 | fetch() 20 | .then((result) => { 21 | clearTimeout(timeoutID); 22 | resolve(result); 23 | }) 24 | .catch((error) => { 25 | if (error.status === 400){ 26 | clearTimeout(timeoutID); 27 | reject(error); 28 | } 29 | }); 30 | }); 31 | } 32 | 33 | function count_submissions(submissions) { 34 | let alreadySolved = {}; 35 | let problemID; 36 | let count = 0; 37 | for (const submission of submissions) { 38 | problemID = submission.problem.contestId + "-" + submission.problem.index; 39 | if (submission.verdict == "OK" && !alreadySolved[problemID]) { 40 | count++; 41 | alreadySolved[problemID] = true; 42 | } 43 | } 44 | return count; 45 | } 46 | 47 | export function get_rating(username, cache_seconds) { 48 | return fetch_error_handler( 49 | () => 50 | new Promise((resolve, reject) => { 51 | api 52 | .get(`/user.info?handles=${username}`, { 53 | cache: { 54 | ttl: cache_seconds * 1000, 55 | }, 56 | }) 57 | .then((response) => { 58 | const res = response.data.result[0].rating || 0; 59 | try { 60 | last_rating_cache.set(username, res); 61 | } catch (error) { 62 | console.error(error); 63 | } 64 | resolve(res); 65 | }) 66 | .catch((error) => { 67 | console.error(error); 68 | if (error.response && error.response.status === 400) 69 | reject({ status: 400, error: "Codeforces Handle Not Found" }); 70 | else reject({ status: 500, error: "Codeforces Server Error" }); 71 | }); 72 | }), 73 | username, 74 | last_rating_cache 75 | ); 76 | } 77 | 78 | export function get_stats(username, cache_seconds) { 79 | const apiConfig = { 80 | cache: { 81 | ttl: cache_seconds * 1000, 82 | }, 83 | }; 84 | return fetch_error_handler( 85 | () => 86 | new Promise((resolve, reject) => { 87 | Promise.all([ 88 | api.get(`/user.info?handles=${username}`, apiConfig), 89 | api.get(`/user.rating?handle=${username}`, apiConfig), 90 | api.get(`/user.status?handle=${username}`, apiConfig), 91 | ]) 92 | .then((responses) => { 93 | let { 94 | firstName, 95 | lastName, 96 | rating, 97 | rank, 98 | maxRank, 99 | maxRating, 100 | friendOfCount, 101 | contribution, 102 | } = responses[0].data.result[0]; 103 | 104 | rating = rating ? rating : 0; 105 | maxRating = maxRating ? maxRating : 0; 106 | rank = rank ? capitalize(rank) : "Unrated"; 107 | maxRank = maxRank ? capitalize(maxRank) : "Unrated"; 108 | 109 | const fullName = `${firstName} ${lastName}` 110 | .replace("undefined", "") 111 | .replace("undefined", "") 112 | .trim(); 113 | const contestsCount = responses[1].data.result.length; 114 | const submissions = responses[2].data.result.length; 115 | const problemsSolved = count_submissions(responses[2].data.result); 116 | 117 | const res = { 118 | username, 119 | fullName, 120 | rating, 121 | maxRating, 122 | rank, 123 | maxRank, 124 | contestsCount, 125 | problemsSolved, 126 | submissions, 127 | friendOfCount, 128 | contribution, 129 | }; 130 | 131 | try { 132 | last_stats_cache.set(username, res); 133 | } catch (error) { 134 | console.error(error); 135 | } 136 | resolve(res); 137 | }) 138 | .catch((error) => { 139 | console.error(error); 140 | if (error.response && error.response.status === 400) 141 | reject({ status: 400, error: "Codeforces Handle Not Found" }); 142 | else reject({ status: 500, error: "Codeforces Server Error" }); 143 | }); 144 | }), 145 | username, 146 | last_stats_cache 147 | ); 148 | } 149 | -------------------------------------------------------------------------------- /src/hooks/option.js: -------------------------------------------------------------------------------- 1 | import useLocalStorageState from "use-local-storage-state"; 2 | import qs from "fast-querystring"; 3 | 4 | const defaultOption = { 5 | username: "redheadphone", 6 | theme: "github_dark", 7 | disable_animations: false, 8 | show_icons: true, 9 | force_username: true, 10 | }; 11 | 12 | const useOption = () => { 13 | const [options, setOptions] = useLocalStorageState("options", { 14 | defaultValue: defaultOption, 15 | }); 16 | const [imageUrl, setImageUrl] = useLocalStorageState("imageUrl", { 17 | defaultValue: qs.stringify(options), 18 | }); 19 | const [error, setError] = useLocalStorageState("error", { 20 | defaultValue: false, 21 | }); 22 | const [loading, setLoading] = useLocalStorageState("loading", { 23 | defaultValue: true, 24 | }); 25 | 26 | const updateImage = (newOptions) => { 27 | const newImageUrl = `/api/card?${qs.stringify(newOptions)}`; 28 | if (newImageUrl != imageUrl || error) setLoading(true); 29 | setError(false); 30 | setImageUrl(newImageUrl); 31 | }; 32 | 33 | const checkHandleNotFound = () => { 34 | return new Promise((resolve) => { 35 | fetch( 36 | `https://codeforces.com/api/user.info?handles=${options.username}` 37 | ).then((res) => { 38 | if (res.status === 400) { 39 | resolve(); 40 | } 41 | }); 42 | }); 43 | }; 44 | 45 | return { 46 | options, 47 | setOptions, 48 | imageUrl, 49 | updateImage, 50 | error, 51 | setError, 52 | loading, 53 | setLoading, 54 | checkHandleNotFound, 55 | }; 56 | }; 57 | 58 | export default useOption; 59 | -------------------------------------------------------------------------------- /src/images/error.svg: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | 25 | 26 | 27 | 28 | 30 | 32 | 33 | 34 | 36 | 38 | 40 | 41 | 42 | 43 | 47 | 48 | 49 | 51 | 53 | 55 | 57 | 59 | 61 | 63 | 65 | 67 | 69 | 71 | 72 | 73 | 75 | 77 | 79 | 81 | 83 | 85 | 87 | 88 | 92 | 96 | 97 | 98 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 114 | 116 | 118 | 121 | 122 | 127 | 128 | 130 | 132 | 134 | 135 | 136 | 138 | 139 | 140 | 146 | 147 | 149 | 151 | 153 | 154 | 155 | 157 | 159 | 160 | 162 | 164 | 165 | 166 | 168 | 170 | 172 | 174 | 176 | 179 | 181 | 183 | 185 | 187 | 189 | 190 | 192 | 194 | 196 | 198 | 199 | 200 | 203 | 204 | 205 | 206 | 207 | -------------------------------------------------------------------------------- /src/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RedHeadphone/codeforces-readme-stats/d86987a22e9b76f783dc278448a385036e0e8370/src/images/logo.png -------------------------------------------------------------------------------- /src/pages/_app.js: -------------------------------------------------------------------------------- 1 | import dynamic from "next/dynamic"; 2 | import Head from "next/head"; 3 | import "./styles.css"; 4 | 5 | function App({ Component, pageProps }) { 6 | return ( 7 | <> 8 | 9 | Codeforces Readme Stats 10 | 11 | 12 | 13 | 14 | ); 15 | } 16 | 17 | export default dynamic(() => Promise.resolve(App), { 18 | ssr: false, 19 | }); 20 | -------------------------------------------------------------------------------- /src/pages/_document.js: -------------------------------------------------------------------------------- 1 | import { Html, Head, Main, NextScript } from "next/document"; 2 | 3 | export default function Document() { 4 | return ( 5 | 6 | 7 | 8 | 12 | 16 | 17 | 18 | 22 | 26 | 27 | 28 | 29 | 30 | 34 | 35 | 39 | 43 | 44 | 45 | 46 | 50 | 54 | 55 | 59 | 63 | 64 | 65 | 66 | 67 | 68 |
69 | 70 | 71 | 72 | ); 73 | } 74 | -------------------------------------------------------------------------------- /src/pages/api/badge.js: -------------------------------------------------------------------------------- 1 | import { get_rating } from "@/fetcher.js"; 2 | import { 3 | renderTemplate, 4 | get_color_from_rating, 5 | CONSTANTS, 6 | clamp_value, 7 | } from "@/common.js"; 8 | 9 | export default async function handler(req, res) { 10 | return new Promise((resolve, reject) => { 11 | const { username, cache_seconds } = req.query; 12 | 13 | const cacheSeconds = clamp_value( 14 | parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10), 15 | CONSTANTS.FOUR_HOURS, 16 | CONSTANTS.ONE_DAY 17 | ); 18 | 19 | get_rating(username, cacheSeconds) 20 | .then((rating) => { 21 | res.setHeader("Content-Type", "image/svg+xml"); 22 | res.setHeader( 23 | "Cache-Control", 24 | `max-age=${ 25 | cacheSeconds / 2 26 | }, s-maxage=${cacheSeconds}, stale-while-revalidate=${ 27 | CONSTANTS.ONE_DAY 28 | }` 29 | ); 30 | 31 | res.send( 32 | renderTemplate("badge.svg", { 33 | rating, 34 | color: get_color_from_rating(rating), 35 | }) 36 | ); 37 | resolve(); 38 | }) 39 | .catch(({ status, error }) => { 40 | res.setHeader("Content-Type", "text/plain"); 41 | res.setHeader("Cache-Control", `no-cache, no-store, must-revalidate`); 42 | res.status(status).send(error); 43 | resolve(); 44 | }); 45 | }); 46 | } 47 | -------------------------------------------------------------------------------- /src/pages/api/card.js: -------------------------------------------------------------------------------- 1 | import { get_stats } from "@/fetcher.js"; 2 | import themes from "@/themes.js"; 3 | import { 4 | renderTemplate, 5 | get_color_from_rating, 6 | CONSTANTS, 7 | clamp_value, 8 | } from "@/common.js"; 9 | 10 | export default async function handler(req, res) { 11 | return new Promise((resolve, reject) => { 12 | let { 13 | username, 14 | force_username, 15 | theme = "default", 16 | title_color, 17 | text_color, 18 | icon_color, 19 | border_color, 20 | bg_color, 21 | cache_seconds, 22 | disable_animations, 23 | show_icons, 24 | } = req.query; 25 | 26 | if (themes[theme] == undefined) { 27 | res.setHeader("Cache-Control", `no-cache, no-store, must-revalidate`); 28 | res.status(404).send("Theme not found"); 29 | resolve(); 30 | return; 31 | } 32 | 33 | const cacheSeconds = clamp_value( 34 | parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10), 35 | CONSTANTS.FOUR_HOURS, 36 | CONSTANTS.ONE_DAY 37 | ); 38 | 39 | get_stats(username, cacheSeconds) 40 | .then( 41 | ({ 42 | username, 43 | fullName, 44 | rating, 45 | maxRating, 46 | rank, 47 | maxRank, 48 | contestsCount, 49 | problemsSolved, 50 | submissions, 51 | friendOfCount, 52 | contribution, 53 | }) => { 54 | res.setHeader("Content-Type", "image/svg+xml"); 55 | res.setHeader( 56 | "Cache-Control", 57 | `max-age=${ 58 | cacheSeconds / 2 59 | }, s-maxage=${cacheSeconds}, stale-while-revalidate=${ 60 | CONSTANTS.ONE_DAY 61 | }` 62 | ); 63 | 64 | show_icons = show_icons !== "false"; 65 | disable_animations = disable_animations === "true"; 66 | force_username = force_username === "true"; 67 | 68 | const name = force_username || fullName === "" ? username : fullName; 69 | 70 | const customColorScheme = { 71 | title_color, 72 | text_color, 73 | icon_color, 74 | border_color, 75 | bg_color, 76 | }; 77 | Object.keys(customColorScheme).forEach( 78 | (key) => 79 | customColorScheme[key] == undefined && 80 | delete customColorScheme[key] 81 | ); 82 | const colorScheme = { 83 | ...themes["default"], 84 | ...themes[theme], 85 | ...customColorScheme, 86 | }; 87 | const width = Math.max(380, 70 + name.length * 13); 88 | const breakBetweenRank = (rank.length + maxRank.length + 8)*10 + 34 > width; 89 | const height = 290 + (breakBetweenRank ? 22 : 0); 90 | 91 | res.send( 92 | renderTemplate("card.svg", { 93 | name, 94 | rating, 95 | maxRating, 96 | rank, 97 | maxRank, 98 | breakBetweenRank, 99 | height, 100 | width, 101 | contestsCount, 102 | problemsSolved, 103 | submissions, 104 | friendOfCount, 105 | contribution, 106 | rankColor: get_color_from_rating(rating), 107 | maxRankColor: get_color_from_rating(maxRating), 108 | theme: colorScheme, 109 | animation: !disable_animations, 110 | show_icons: show_icons, 111 | }) 112 | ); 113 | resolve(); 114 | } 115 | ) 116 | .catch(({ status, error }) => { 117 | res.setHeader("Content-Type", "text/plain"); 118 | res.setHeader("Cache-Control", `no-cache, no-store, must-revalidate`); 119 | res.status(status).send(error); 120 | resolve(); 121 | }); 122 | }); 123 | } 124 | -------------------------------------------------------------------------------- /src/pages/index.js: -------------------------------------------------------------------------------- 1 | import Image from "next/image"; 2 | import { 3 | GithubFilled, 4 | BookFilled, 5 | ExportOutlined, 6 | CopyOutlined, 7 | } from "@ant-design/icons"; 8 | import { 9 | Space, 10 | Card, 11 | Col, 12 | Form, 13 | Input, 14 | Select, 15 | Radio, 16 | Button, 17 | Divider, 18 | Row, 19 | Spin, 20 | notification, 21 | } from "antd"; 22 | 23 | import themes from "@/themes.js"; 24 | import Logo from "@/images/logo.png"; 25 | import Error from "@/images/error.svg"; 26 | import useOption from "@/hooks/option.js"; 27 | 28 | export default function Home() { 29 | const { 30 | options, 31 | setOptions, 32 | imageUrl, 33 | updateImage, 34 | error, 35 | setError, 36 | loading, 37 | setLoading, 38 | checkHandleNotFound, 39 | } = useOption(); 40 | const [api, contextHolder] = notification.useNotification(); 41 | 42 | const openNotification = (message, description) => { 43 | api.info({ 44 | message, 45 | description, 46 | placement: "topRight", 47 | duration: 3, 48 | }); 49 | }; 50 | 51 | const handleFieldsChange = async (changed_value) => { 52 | setOptions((prev) => { 53 | const newOptions = { 54 | ...prev, 55 | [changed_value[0].name]: changed_value[0].value, 56 | }; 57 | if (changed_value[0].name[0] !== "username") { 58 | updateImage(newOptions); 59 | } 60 | return newOptions; 61 | }); 62 | }; 63 | 64 | const handleCopyMarkdown = () => { 65 | navigator.clipboard.writeText( 66 | `[![Codeforces Stats](${ 67 | window.location.href.substring( 68 | 0, 69 | window.location.href.lastIndexOf("/") 70 | ) + imageUrl 71 | })](https://codeforces.com/profile/${options.username})` 72 | ); 73 | openNotification("Success", "Copied to clipboard!"); 74 | }; 75 | 76 | const handleOpenInNewTab = () => { 77 | window.open(imageUrl, "_blank"); 78 | }; 79 | 80 | const handleUsernameEnter = () => { 81 | checkHandleNotFound().then(() => { 82 | openNotification("Error", "Handle not found!"); 83 | }); 84 | updateImage(options); 85 | }; 86 | 87 | const handleError = () => { 88 | setError(true); 89 | }; 90 | 91 | const handleLoad = () => { 92 | setLoading(false); 93 | }; 94 | 95 | return ( 96 | <> 97 | {contextHolder} 98 | 99 | 100 | 101 |
102 | 103 | Logo 104 |

Codeforces Readme Stats

105 |
106 |

107 | ⚡ Dynamically generated Codeforces stats for your Github 108 | profile! 109 |

110 |
111 | 112 | 113 | 114 |
121 | 132 | 137 | 143 | 146 | 147 | 148 | 149 |