-
Notifications
You must be signed in to change notification settings - Fork 7
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
lang: TR Language added #1
Open
factorycreacom
wants to merge
15
commits into
mandaputtra:master
Choose a base branch
from
factorycreacom:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9c1d4e4
lang: TR Language added
factorycreacom cda03bf
add: Get detail
factorycreacom 287a310
1 Gr Support
factorycreacom 4d655ea
Update vercel.json
factorycreacom 1fe2e2d
vercel.json
factorycreacom 7599c60
Merge branch 'master' of https://github.com/factorycreacom/fatsecret-…
factorycreacom 1afa18a
vercel.json
factorycreacom b98995a
Update vercel.json
factorycreacom ea148a6
abv vercel
factorycreacom 392c4a2
Update vercel.json
factorycreacom c1b486d
aqq vercel
factorycreacom 4f5da21
add(next-cors): Cors added
factorycreacom 1675a72
rewrites removed
factorycreacom 6e0f476
added(food-name): Food Name added
factorycreacom 4fee4fe
fix(fixed): Returned value fixed decimal
factorycreacom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,3 +109,5 @@ temp/ | |
|
||
# End of https://www.gitignore.io/api/node | ||
.vercel | ||
|
||
.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,144 @@ | ||
import { VercelRequest, VercelResponse } from '@vercel/node' | ||
import { getLang } from '../../../utils/lang' | ||
import { VercelRequest, VercelResponse } from "@vercel/node"; | ||
import { fetchHTML } from "../../../utils/fetch"; | ||
import { getLang } from "../../../utils/lang"; | ||
import cheerio from "cheerio"; | ||
import { parse, stringify } from "qs"; | ||
import NextCors from "nextjs-cors"; | ||
export default async ( | ||
request: VercelRequest, | ||
response: VercelResponse | ||
): Promise<void> => { | ||
await NextCors(request, response, { | ||
// Options | ||
methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"], | ||
origin: "*", | ||
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204 | ||
}); | ||
|
||
const langConfig = getLang(String(request.query.lang)); | ||
const detailUrl = request.query.url; | ||
const url = request.headers["x-forwarded-host"]; | ||
const proto = request.headers["x-forwarded-proto"]; | ||
const query: any = request.query.query; | ||
const page: any = +request.query.page || 0; | ||
const portionamount = request.query.portionamount; | ||
const portionid = request.query.portionid; | ||
const isGr = request.query.isGram ? true : false; | ||
|
||
if (!langConfig) { | ||
response.json({ error: `${request.query.lang} are not supported` }); | ||
return; | ||
} | ||
|
||
export default async (request: VercelRequest, response: VercelResponse): Promise<void> => { | ||
const langConfig = getLang(String(request.query.lang)) | ||
const detailUrl = request.query.url | ||
if (!detailUrl) { | ||
response.json({ error: 'please provide detailLink on search' }) | ||
return | ||
response.json({ error: "please provide detailLink on search" }); | ||
return; | ||
} | ||
if (!langConfig) { | ||
response.json({ error: `${request.query.lang} are not supported` }) | ||
return | ||
response.json({ error: `${request.query.lang} are not supported` }); | ||
return; | ||
} | ||
|
||
let _url = langConfig.baseUrl + detailUrl; | ||
const html = await fetchHTML(_url, { | ||
portionamount, | ||
portionid, | ||
pg: page, | ||
}); | ||
|
||
const $ = cheerio.load(html); | ||
let items: any = {}; | ||
|
||
$("div.summarypanelcontent").each((_, elem: any) => { | ||
const element = $(elem); | ||
const x = element.find("h1").text(); | ||
if (x) { | ||
items["food"] = x; | ||
} | ||
}); | ||
|
||
$("div.nutrient.left").each((_, elem: any) => { | ||
const element = $(elem); | ||
const normalizeText = element.text().replace(/(\r\n|\n|\r\t|\t|\r)/gm, ""); | ||
let item; | ||
const nextELem = element.next().text(); | ||
|
||
switch (normalizeText) { | ||
case "Enerji": | ||
items["kj"] = parseItem(nextELem, "kj"); | ||
break; | ||
|
||
case "": | ||
if (nextELem.indexOf("kcal") !== -1) { | ||
items["kcal"] = parseItem(nextELem, "kcal"); | ||
} | ||
break; | ||
|
||
case "Protein": | ||
items["protein"] = parseItem(nextELem, "protein"); | ||
break; | ||
|
||
case "Yağ": | ||
items["fat"] = parseItem(nextELem, "fat"); | ||
break; | ||
|
||
case "Karbonhidratlar": | ||
items["carb"] = parseItem(nextELem, "carb"); | ||
break; | ||
|
||
case "Doymuş Yağ": | ||
items["saturated_fat"] = parseItem(nextELem, "saturated_fat"); | ||
break; | ||
|
||
case "Kolesterol": | ||
items["colestrol"] = parseItem(nextELem, "colestrol"); | ||
break; | ||
case "Şeker": | ||
items["sugar"] = parseItem(nextELem, "sugar"); | ||
break; | ||
|
||
case "Fiber": | ||
items["lif"] = parseItem(nextELem, "lif"); | ||
break; | ||
|
||
case "Sodyum": | ||
items["sodyum"] = parseItem(nextELem, "sodyum"); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
}); | ||
|
||
response.json(items); | ||
|
||
function parseItem( | ||
item: string, | ||
type: | ||
| "kj" | ||
| "kcal" | ||
| "protein" | ||
| "fat" | ||
| "carb" | ||
| "saturated_fat" | ||
| "colestrol" | ||
| "sugar" | ||
| "lif" | ||
| "sodyum" | ||
) { | ||
const vb: any = | ||
item.replace(langConfig.detailRegex[type], "").replace(",", ".").trim() || | ||
0; | ||
const x = Number(vb / 100); | ||
|
||
return Math.round(x * 100) / 100; | ||
|
||
//return vb; | ||
} | ||
response.json({ | ||
status: 'work in progress', | ||
debug: `${langConfig.baseUrl}${detailUrl}` | ||
}) | ||
} | ||
|
||
/* $("div.nutrient.black.right.tRight").each((_, elem: any) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this too |
||
const element = $(elem); | ||
const normalizeText = element.text().replace(/(\r\n|\n|\r\t|\t|\r)/gm, ""); | ||
console.log("ELEM", normalizeText); | ||
}); */ | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this detail is too specific with one language. Can you make it more language agnostic? Maybe add some parameters on
utils/lang.ts
. Just maybe add this types and update accordingly.