├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── requirements.txt ├── src └── main.py └── vercel.json /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | .venv 3 | .vscode 4 | .idea -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Setup environment 2 | FROM python:3.9-bullseye 3 | 4 | # Install requirements 5 | COPY requirements.txt requirements.txt 6 | RUN pip3 install -r requirements.txt 7 | 8 | # Copy files 9 | COPY src/* src/ 10 | 11 | # Set /src as working directory 12 | WORKDIR /src 13 | 14 | # Run the server 15 | CMD ["uvicorn", "main:app", "--host", "0.0.0.0"] 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Salman Shaikh 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 | # Unofficial Fiverr API 2 | 3 | ![Vercel](https://img.shields.io/static/v1?label=Vercel%20Build&labelColor=black&message=Success&color=ddd&logo=vercel) 4 | ![Python](https://img.shields.io/static/v1?label=Python&message=3.9.2&color=306998&logo=python&logoColor=white) 5 | ![Release](https://img.shields.io/static/v1?label=Release&message=v1.2&color=306998) 6 | 7 | This is an unofficial api to fetch Fiverr seller's data 8 | 9 | ## How to run? 10 | 11 | ### Manually 12 | 13 | > **Prerequisites:** 14 | > python >= 3.9 15 | > pip >= 20.3 16 | 17 | ```zsh 18 | git clone https://github.com/salmannotkhan/fiverr-api.git 19 | cd fiverr-api 20 | pip3 install -r requirements.txt 21 | uvicorn main:app 22 | ``` 23 | 24 | - This will start localserver at `localhost:8000` 25 | 26 | ### Docker 27 | 28 | > **Prerequirsites:** 29 | > Docker must be installed on system 30 | 31 | #### Creating docker image 32 | 33 | `docker build -t fiverr-api .` 34 | 35 | > -t: This option will assign `fiverr-api` tag to docker image 36 | 37 | #### Running docker container 38 | 39 | `docker run --rm -d -p 8000:8000 fiverr-api` 40 | 41 | > --rm: This option will delete container as soon it's stopped 42 | > -d: This will detach container and it will run in background 43 | > -p 8000:8000: This will expose container's port 8000 to host's port 8000 44 | 45 | - This will start server at `localhost:8000` 46 | - It'll also return `CONTAINER_ID` which can be used to stop container 47 | 48 | #### Stopping docker container 49 | 50 | `docker stop CONTAINER_ID` 51 | 52 | ## Usage 53 | 54 | Visit API Docs from [here](https://fiverr-api.vercel.app/docs) 55 | 56 | ## Contributing 57 | 58 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 59 | 60 | ## License 61 | 62 | [MIT](https://choosealicense.com/licenses/mit/) 63 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asgiref==3.4.1 2 | beautifulsoup4==4.9.3 3 | bs4==0.0.1 4 | certifi==2021.5.30 5 | charset-normalizer==2.0.4 6 | click==8.0.1 7 | cloudscraper==1.2.65 8 | fastapi==0.68.0 9 | h11==0.12.0 10 | idna==3.2 11 | lxml==4.6.3 12 | pydantic==1.8.2 13 | pyparsing==3.0.9 14 | requests==2.26.0 15 | requests-toolbelt==0.10.0 16 | soupsieve==2.2.1 17 | starlette==0.14.2 18 | typing-extensions==3.10.0.0 19 | urllib3==1.26.6 20 | uvicorn==0.14.0 21 | -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | """ 2 | Unofficial Fiverr API helps you to get: 3 | 4 | * Seller Details 5 | * Seller Gigs 6 | * Seller Orders 7 | * Seller Transactions 8 | * Seller Reviews 9 | * Group by buyers 10 | * Filter by Impression 11 | * Sort by time 12 | * Limit no. of reviews 13 | """ 14 | 15 | from typing import Union 16 | from enum import Enum 17 | import json 18 | from cloudscraper.exceptions import CloudflareChallengeError 19 | 20 | from fastapi.middleware.cors import CORSMiddleware 21 | from fastapi.security import HTTPBearer 22 | from fastapi import Depends, FastAPI, HTTPException, status 23 | from bs4 import BeautifulSoup 24 | import requests 25 | import cloudscraper 26 | 27 | DESCRIPTION = __doc__ or "" 28 | tags_metadata = [ 29 | {"name": "Home"}, 30 | {"name": "Seller Details", "description": "Get details about seller"}, 31 | ] 32 | 33 | URL = "https://www.fiverr.com" 34 | bearer_description = f""" 35 | ### Steps to obtain authorization token from Fiverr: 36 | - Login into your [Fiverr account]({URL}) 37 | - Open Network Tab of browser 38 | - Select any `HTML` or `XHR` request 39 | - Go to `Cookies` tab 40 | - Use `hodor_creds` as authorization token 41 | 42 | > I DO NOT store your credentials. You can verify that by viewing source code [here](https://www.github.com/salmannotkhan/fiverr-api) 43 | """ 44 | 45 | bearer = HTTPBearer(auto_error=True, description=bearer_description) 46 | 47 | app = FastAPI( 48 | title="Unofficial Fiverr API", 49 | description=DESCRIPTION, 50 | version="1.2", 51 | contact={ 52 | "name": "Salman Shaikh", 53 | "url": "https://salmannotkhan.github.io/", 54 | "email": "tony903212@gmail.com", 55 | }, 56 | openapi_tags=tags_metadata, 57 | ) 58 | 59 | app.add_middleware( 60 | CORSMiddleware, 61 | allow_origins=["*"], 62 | allow_credentials=True, 63 | allow_methods=["GET"], 64 | allow_headers=["*"], 65 | ) 66 | 67 | 68 | class FilterBy(str, Enum): 69 | """ 70 | Filter by Enum Class 71 | """ 72 | 73 | POSITIVE = "positive" 74 | NEGATIVE = "negative" 75 | 76 | 77 | class SortBy(str, Enum): 78 | """ 79 | Sort by Enum Class 80 | """ 81 | 82 | RECENT = "recent" 83 | RELEVANT = "relevant" 84 | 85 | 86 | common_headers = { 87 | "User-Agent": "Mozilla Firefox", 88 | "Accept": "application/json", 89 | "Accept-Language": "en-US,en;q=0.9", 90 | "Accept-Encoding": "gzip, deflate", 91 | "Sec-Fetch-Dest": "empty", 92 | "Sec-Fetch-Mode": "cors", 93 | "Sec-Fetch-Site": "same-origin", 94 | "X-Requested-With": "XMLHttpRequest", 95 | } 96 | 97 | def get_user_data(username: str): 98 | """ 99 | Get basic seller details and CSRF Token 100 | """ 101 | scraper = cloudscraper.create_scraper() 102 | seller_url = f"{URL}/{username}" 103 | data = scraper.get(seller_url) 104 | soup = BeautifulSoup(data.text, "lxml") 105 | seller_data = json.loads( 106 | soup.find("script", id="perseus-initial-props").string or "null" 107 | ) 108 | seller_data["csrfToken"] = soup.find("meta", {"property": "csrfToken"}).get( 109 | "content" 110 | ) 111 | return scraper.cookies, seller_data 112 | 113 | 114 | @app.get("/", tags=["Home"]) 115 | async def index(): 116 | """ 117 | Home Path for API 118 | """ 119 | return { 120 | "Welcome to": "Unofficial Fiverr API", 121 | "For docs": "Visit /docs", 122 | "For redocs": "Visit /redoc", 123 | } 124 | 125 | 126 | @app.get("/transaction", tags=["Seller Details"]) 127 | async def get_transactions(after: Union[str, None] = None, token=Depends(bearer)): 128 | """ 129 | Use `endCursor` as `after` for pagination 130 | """ 131 | scraper = cloudscraper.create_scraper() 132 | url = f"{URL}/perseus/financial-dashboard/api/earnings/transactions" 133 | cookies = {"hodor_creds": token.credentials} 134 | while True: 135 | try: 136 | res = scraper.get( 137 | url, 138 | headers=common_headers, 139 | cookies=cookies, 140 | allow_redirects=False, 141 | params={"after": after}, 142 | ) 143 | break 144 | except CloudflareChallengeError: 145 | pass 146 | data = res.json() 147 | data["data"]["transactions"] = list( 148 | map( 149 | lambda x: {**x, "amount": (x["amount"] / 100)}, data["data"]["transactions"] 150 | ) 151 | ) 152 | return data 153 | 154 | 155 | @app.get("/{username}", tags=["Seller Details"]) 156 | async def get_seller_details(username: str): 157 | """ 158 | Remove unnecessary details from seller card and returns it 159 | """ 160 | _, user_data = get_user_data(username) 161 | seller_card = user_data["userData"]["seller_card"] 162 | seller_profile = user_data["userData"]["seller_profile"] 163 | seller_card.update(seller_profile) 164 | return seller_card 165 | 166 | 167 | @app.get("/{username}/gigs", tags=["Seller Details"]) 168 | async def get_gigs(username: str): 169 | """ 170 | Get seller gigs 171 | """ 172 | _, user_data = get_user_data(username) 173 | return user_data["gigs"]["gigs"] 174 | 175 | 176 | @app.get("/{username}/reviews", tags=["Seller Details"]) 177 | async def get_reviews( 178 | username: str, 179 | filter_by: Union[FilterBy, None] = None, 180 | sort_by: Union[SortBy, None] = None, 181 | group_by_buyer: bool = False, 182 | limit: int = 9999, 183 | ): 184 | """ 185 | Get seller reviews 186 | """ 187 | session = requests.session() 188 | cookies, user_data = get_user_data(username) 189 | session.cookies = cookies 190 | url = f"{URL}/reviews/user_page/fetch_user_reviews/{user_data['userData']['user']['id']}" 191 | 192 | # Adding CSRF Token 193 | common_headers["X-CSRF-Token"] = user_data["csrfToken"] 194 | common_headers["Referer"] = f"https://www.fiverr.com/{username}" 195 | # Setting up payload 196 | payload: dict[str, str] = {} 197 | payload["user_id"] = user_data["userData"]["user"]["id"] 198 | if filter_by: 199 | payload["filter_by"] = filter_by.value 200 | if sort_by: 201 | payload["sort_by"] = sort_by.value 202 | reviews: list[dict[str, str]] = [] 203 | 204 | scraper = cloudscraper.create_scraper(sess=session, browser="chrome") 205 | while True: 206 | data = scraper.get(url, headers=common_headers, params=payload) 207 | data = data.json() 208 | reviews.extend(data["reviews"]) 209 | if not data["has_next"] or len(reviews) >= limit: 210 | break 211 | payload["last_star_rating_id"] = reviews[-1]["id"] 212 | payload["last_review_id"] = reviews[-1]["id"] 213 | payload["last_score"] = "0" 214 | reviews = reviews[:limit] 215 | if group_by_buyer: 216 | merged_reviews: dict[str, list[dict[str, str]]] = {} 217 | for review in reviews: 218 | if review["username"] in merged_reviews: 219 | merged_reviews[review["username"]].append(review) 220 | else: 221 | merged_reviews[review["username"]] = [review] 222 | return merged_reviews 223 | session.close() 224 | return reviews 225 | 226 | 227 | @app.get("/{username}/orders", tags=["Seller Details"]) 228 | async def get_orders(username: str, token=Depends(bearer)): 229 | url = f"{URL}/users/{username}/manage_orders/type/completed" 230 | cookies = {"hodor_creds": token.credentials} 231 | results = [] 232 | scraper = cloudscraper.create_scraper() 233 | while True: 234 | while True: 235 | try: 236 | res = scraper.get( 237 | url, headers=common_headers, cookies=cookies, allow_redirects=False 238 | ) 239 | break 240 | except CloudflareChallengeError: 241 | pass 242 | 243 | if res.status_code == 302: 244 | raise HTTPException( 245 | status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized profile" 246 | ) 247 | data = res.json() 248 | results.extend(data["results"]) 249 | if not data.get("load_more_url"): 250 | break 251 | url = "https://www.fiverr.com" + data["load_more_url"] 252 | return results 253 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "builds": [ 4 | { 5 | "src": "src/main.py", 6 | "use": "@vercel/python" 7 | } 8 | ], 9 | "routes": [ 10 | { 11 | "src": "(.*)", 12 | "dest": "src/main.py" 13 | } 14 | ] 15 | } 16 | --------------------------------------------------------------------------------