Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass proper headers to return contributions after github update broke api #8

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Config } from 'jest';

const config: Config = {
silent: true,
};

export default config;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"postinstall": "npm run build",
"prestart": "npm run build",
"start": "node build/server.js",
"test": "jest --silent",
"test": "jest",
"typecheck": "tsc"
},
"repository": {
Expand Down
47 changes: 28 additions & 19 deletions src/scrape.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import cheerio from 'cheerio';

import { ParsedQuery } from '.';
import TagElement = cheerio.TagElement;
import Cheerio = cheerio.Cheerio;
import TagElement = cheerio.TagElement;

type Level = 0 | 1 | 2 | 3 | 4;
type Year = number | 'lastYear';
Expand Down Expand Up @@ -40,7 +40,15 @@ export interface NestedResponse {
* @throws UserNotFoundError
*/
async function scrapeYearLinks(username: string, query: ParsedQuery) {
const page = await fetch(`https://github.com/${username}`);
const page = await fetch(
`https://github.com/${username}?action=show&controller=profiles&tab=contributions&user_id=${username}`,
{
headers: {
referer: `https://github.com/${username}`,
'x-requested-with': 'XMLHttpRequest',
},
},
);
const $ = cheerio.load(await page.text());

const yearLinks = $('.js-year-link').get();
Expand All @@ -50,19 +58,9 @@ async function scrapeYearLinks(username: string, query: ParsedQuery) {
}

return yearLinks
.map((a) => {
const $a = $(a);
const href = $a.attr('href');

if (!href) {
throw Error('Unable to parse year link.');
}

return {
year: parseInt($a.text().trim()),
href,
};
})
.map((a) => ({
year: parseInt($(a).text().trim()),
}))
.filter((link) =>
query.fetchAll ? true : query.years.includes(link.year),
);
Expand All @@ -73,10 +71,21 @@ async function scrapeYearLinks(username: string, query: ParsedQuery) {
*/
async function scrapeContributionsForYear(
year: Year,
url: string,
username: string,
format?: 'nested',
): Promise<Response | NestedResponse> {
const page = await fetch(`https://github.com${url}`);
const url = `https://github.com/users/${username}/contributions`;
const page = await fetch(
year === 'lastYear'
? url
: url.concat(`?tab=overview&from=${year}-12-01&to=${year}-12-31`),
{
headers: {
referer: `https://github.com/${username}`,
'x-requested-with': 'XMLHttpRequest',
},
},
);

const $ = cheerio.load(await page.text());
const $days = $('.js-calendar-graph-table .ContributionCalendar-day');
Expand Down Expand Up @@ -198,12 +207,12 @@ export async function scrapeGitHubContributions(
): Promise<Response | NestedResponse> {
const yearLinks = await scrapeYearLinks(username, query);
const contributionsForYear = yearLinks.map((link) =>
scrapeContributionsForYear(link.year, link.href, query.format),
scrapeContributionsForYear(link.year, username, query.format),
);

if (query.lastYear) {
contributionsForYear.push(
scrapeContributionsForYear('lastYear', `/${username}`, query.format),
scrapeContributionsForYear('lastYear', username, query.format),
);
}

Expand Down
Loading
Loading