Repository: auino/cookidump Branch: master Commit: ebfc98214d1a Files: 5 Total size: 18.1 KB Directory structure: gitextract_q0xes30l/ ├── LICENSE ├── README.md ├── cookidump.py ├── deploy.sh └── requirements.txt ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2022 Enrico Cambiaso Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # cookidump Easily dump cookidoo recipes from the official website ### Description ### This program allows you to dump all recipes on [Cookidoo](https://cookidoo.co.uk) websites (available for different countries) for offline and posticipate reading. Those recipes are valid in particular for [Thermomix/Bimby](https://en.wikipedia.org/wiki/Thermomix) devices. In order to dump the recipes, a valid subscription is needed. The initial concept of this program was based on [jakubszalaty/cookidoo-parser](https://github.com/jakubszalaty/cookidoo-parser). ### Mentioning ### If you intend to scientifically investigate or extend cookidump, please consider citing the following paper. ``` @article{cambiaso2022cookidump, title = {Web security and data dumping: The Cookidump case}, journal = {Software Impacts}, volume = {14}, pages = {100426}, year = {2022}, issn = {2665-9638}, doi = {https://doi.org/10.1016/j.simpa.2022.100426}, url = {https://www.sciencedirect.com/science/article/pii/S2665963822001105}, author = {Enrico Cambiaso and Maurizio Aiello}, keywords = {Cyber-security, Data dump, Database security, Browser automation}, abstract = {In the web security field, data dumping activities are often related to a malicious exploitation. In this paper, we focus on data dumping activities executed legitimately by scraping/storing data shown on the browser. We evaluate such operation by proposing Cookidump, a tool able to dump all recipes available on the Cookidoo© website portal. While such scenario is not relevant, in terms of security and privacy, we discuss the impact of such kind of activity for other scenarios including web applications hosting sensitive information.} } ``` Further information can be found at [https://www.sciencedirect.com/science/article/pii/S2665963822001105](https://www.sciencedirect.com/science/article/pii/S2665963822001105). ### Features ### * Easy to run * Easy to open HTML output * Output including a surfable list of dumped recipes * Customizable searches ### Installation ### #### nix #### ``` nix run github:auino/cookidump -- [--separate-json] ``` Nix provisions `google-chrome` together with `chromedriver`. Only `` and `[--separate-json]` arguments are expected. #### manual #### 1. Clone the repository: ``` git clone https://github.com/auino/cookidump.git ``` 2. `cd` into the download folder 3. Install [Python](https://www.python.org) requirements: ``` pip install -r requirements.txt ``` 4. Install the [Google Chrome](https://chrome.google.com) browser, if not already installed 5. Download the [Chrome WebDriver](https://sites.google.com/chromium.org/driver/) and save it on the `cookidump` folder 6. You are ready to dump your recipes ### Usage ### Simply run the following command to start the program. The program is interactive to simplify it's usage. ``` python cookidump.py [--separate-json] ``` where: * `webdriverfile` identifies the path to the downloaded [Chrome WebDriver](https://sites.google.com/chromium.org/driver/) (for instance, `chromedriver.exe` for Windows hosts, `./chromedriver` for Linux and macOS hosts) * `outputdir` identifies the path of the output directory (will be created, if not already existent) * `--separate-json` allows to generate a separate JSON file for each recipe, instead of one aggregate file including all recipes The program will open a [Google Chrome](https://chrome.google.com) window and wait until you are logged in into your [Cookidoo](https://cookidoo.co.uk) account (different countries are supported). After that, follow intructions provided by the script itself to proceed with the dump. #### Considerations #### By following script instructions, it is also possible to apply custom filters to export selected recipes (for instance, in base of the dish, title and ingredients, Thermomix/Bimby version, etc.). Output is represented by an `index.html` file, included in `outputdir`, plus a set of recipes inside of structured folders. By opening the generated `index.html` file on your browser, it is possible to have a list of recipes downloaded and surf to the desired recipe. The number of exported recipes is limited to around `1000` for each execution. Hence, use of filters may help in this case to reduce the number of recipes exported. ### Other approaches ### A different approach, previously adopted, is based on the retrieval of structured data on recipes. More information can be found on the [datastructure branch](https://github.com/auino/cookidump/tree/datastructure). Output is represented in this case in a different (structured) format, hence, it has to be interpreted. Such interpretation is not implemented in the linked previous commit. Another community-driven approach, supported by some AI, has been released in the [community branch](https://github.com/auino/cookidump/tree/community). ### TODO ### * Bypass the limited number of exported recipes * Parse downloaded recipes to store them on a database, or to generate a unique linked PDF * Make Chrome run headless for better speeds * Set up a dedicated container for the program ### Supporters ### * [@vikramsoni2](https://github.com/vikramsoni2), regarding JSON saves plus minor enhancements * [@mrwogu](https://github.com/mrwogu), regarding additional information to be extracted on the generated JSON file, plus suggestions on the possibility to save recipes on dedicated JSON files * [@nilskrause](https://github.com/NilsKrause), regarding argument parsing and updates on the link to download the Chrome WebDriver * [@NightProgramming](https://github.com/NightProgramming), regarding the use of selenium version 3 * [@morela](https://github.com/morela), regarding the update of the tool to support a newer version of Selenium * [@ndjc](https://github.com/ndjc), fixing some deprecation warnings * [@paoloaq](https://github.com/paoloaq), fixing page scrolling ### Extensions ### * [GAC27/ReceitasDaAvoGenerator](https://github.com/GAC27/ReceitasDaAvoGenerator) implements a tool generating a PDF "book" file from the output provided by [cookidump](https://github.com/auino/cookidump) ### Disclaimer ### The authors of this program are not responsible of the usage of it. This program is released only for research and dissemination purposes. Also, the program provides users the ability to locally and temporarily store recipes accessible through a legit subscription. Before using this program, check Cookidoo subscription terms of service, according to the country related to the exploited subscription. Sharing of the obtained recipes is not a legit activity and the authors of this program are not responsible of any illecit and sharing activity accomplished by the users. ### Contacts ### You can find me on Twitter as [@auino](https://twitter.com/auino). ================================================ FILE: cookidump.py ================================================ #!/usr/bin/python3 # cookidump # Original GitHub project: # https://github.com/auino/cookidump import os import io import re import time import json import pathlib import argparse import platform from bs4 import BeautifulSoup from selenium import webdriver from urllib.parse import urlparse from urllib.request import urlretrieve from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.action_chains import ActionChains PAGELOAD_TO = 3 SCROLL_TO = 1 MAX_SCROLL_RETRIES = 5 def startBrowser(chrome_driver_path): """Starts browser with predefined parameters""" chrome_options = Options() if "GOOGLE_CHROME_PATH" in os.environ: chrome_options.binary_location = os.getenv('GOOGLE_CHROME_PATH') #chrome_options.add_argument('--headless') chrome_service = Service(chrome_driver_path) driver = webdriver.Chrome(service=chrome_service, options=chrome_options) return driver def listToFile(browser, baseDir): """Gets html from search list and saves in html file""" filename = '{}index.html'.format(baseDir) # creating directories, if needed path = pathlib.Path(filename) path.parent.mkdir(parents=True, exist_ok=True) # getting web page source #html = browser.page_source html = browser.execute_script("return document.documentElement.outerHTML") # saving the page with io.open(filename, 'w', encoding='utf-8') as f: f.write(html) def imgToFile(outputdir, recipeID, img_url): img_path = '{}images/{}.jpg'.format(outputdir, recipeID) path = pathlib.Path(img_path) path.parent.mkdir(parents=True, exist_ok=True) urlretrieve(img_url, img_path) return '../images/{}.jpg'.format(recipeID) def recipeToFile(browser, filename): """Gets html of the recipe and saves in html file""" # creating directories, if needed path = pathlib.Path(filename) path.parent.mkdir(parents=True, exist_ok=True) # getting web page source html = browser.page_source # saving the page with io.open(filename, 'w', encoding='utf-8') as f: f.write(html) def recipeToJSON(browser, recipeID): html = browser.page_source soup = BeautifulSoup(html, 'html.parser') recipe = {} recipe['id'] = recipeID recipe['language'] = soup.select_one('html').attrs['lang'] recipe['title'] = soup.select_one(".recipe-card__title").text recipe['rating_count'] = re.sub(r'\D', '', soup.select_one(".core-rating__label").text, flags=re.IGNORECASE) recipe['rating_score'] = soup.select_one(".core-rating__counter").text recipe['tm-versions'] = [v.text.replace('\n','').strip().lower() for v in soup.select(".recipe-card__tm-version core-badge")] recipe.update({ l.text : l.next_sibling.strip() for l in soup.select("core-feature-icons label span") }) recipe['ingredients'] = [re.sub(' +', ' ', li.text).replace('\n','').strip() for li in soup.select("#ingredients li")] recipe['nutritions'] = {} for item in list(zip(soup.select(".nutritions dl")[0].find_all("dt"), soup.select(".nutritions dl")[0].find_all("dd"))): dt, dl = item recipe['nutritions'].update({ dt.string.replace('\n','').strip().lower(): re.sub(r'\s{2,}', ' ', dl.string.replace('\n','').strip().lower()) }) recipe['steps'] = [re.sub(' +', ' ', li.text).replace('\n','').strip() for li in soup.select("#preparation-steps li")] recipe['tags'] = [a.text.replace('#','').replace('\n','').strip().lower() for a in soup.select(".core-tags-wrapper__tags-container a")] return recipe def run(webdriverfile, outputdir, separate_json): """Scraps all recipes and stores them in html""" print('[CD] Welcome to cookidump, starting things off...') # fixing the outputdir parameter, if needed if outputdir[-1:][0] != '/': outputdir += '/' locale = str(input('[CD] Complete the website domain: https://cookidoo.')) baseURL = 'https://cookidoo.{}/'.format(locale) brw = startBrowser(webdriverfile) # opening the home page brw.get(baseURL) time.sleep(PAGELOAD_TO) reply = input('[CD] Please login to your account and then enter y to continue: ') # recipes base url rbURL = 'https://cookidoo.{}/search/'.format(locale) brw.get(rbURL) time.sleep(PAGELOAD_TO) # possible filters done here reply = input('[CD] Set your filters, if any, and then enter y to continue: ') # asking for additional details for output organization custom_output_dir = input("[CD] enter the directory name to store the results (ex. vegeratian): ") if custom_output_dir : outputdir += '{}/'.format(custom_output_dir) # proceeding print('[CD] Proceeding with scraping') # removing the name brw.execute_script("var element = arguments[0];element.parentNode.removeChild(element);", brw.find_element(By.TAG_NAME, 'core-user-profile')) # clicking on cookie accept try: brw.find_element(By.CLASS_NAME, 'accept-cookie-container').click() except: pass # showing all recipes elementsToBeFound = int(brw.find_element(By.CLASS_NAME, 'items-start').text.split('\n')[-1].split(' ')[0]) previousElements = 0 while True: # checking if ended or not currentElements = len(brw.find_elements(By.CLASS_NAME, 'link--alt')) if currentElements >= elementsToBeFound: break # scrolling to the end brw.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(SCROLL_TO) # clicking on the "load more recipes" button try: brw.find_element(By.XPATH, "//button[@data-cy='load-more-button']").click() time.sleep(PAGELOAD_TO) except: pass print('Scrolling [{}/{}]'.format(currentElements, elementsToBeFound)) # checking if I can't load more elements count = count + 1 if previousElements == currentElements else 0 if count >= MAX_SCROLL_RETRIES: break previousElements = currentElements print('Scrolling [{}/{}]'.format(currentElements, elementsToBeFound)) # saving all recipes urls els = brw.find_elements(By.CLASS_NAME, 'link--alt') recipesURLs = [] for el in els: recipeURL = el.get_attribute('href') recipesURLs.append(recipeURL) recipeID = recipeURL.split('/')[-1:][0] brw.execute_script("arguments[0].setAttribute(arguments[1], arguments[2]);", el, 'href', './recipes/{}.html'.format(recipeID)) # removing search bar try: brw.execute_script("var element = arguments[0];element.parentNode.removeChild(element);", brw.find_element(By.TAG_NAME, 'core-search-bar')) except: pass # removing scripts for s in brw.find_elements(By.TAG_NAME, 'script'): try: brw.execute_script("var element = arguments[0];element.parentNode.removeChild(element);", s) except: pass # saving the list to file listToFile(brw, outputdir) # filter recipe Url list because it contains terms-of-use, privacy, disclaimer links too recipesURLs = [l for l in recipesURLs if 'recipe' in l] # getting all recipes print("Getting all recipes...") c = 0 recipeData = [] for recipeURL in recipesURLs: try: # building urls u = str(urlparse(recipeURL).path) if u[0] == '/': u = '.'+u recipeID = u.split('/')[-1:][0] # opening recipe url brw.get(recipeURL) time.sleep(PAGELOAD_TO) # removing the base href header try: brw.execute_script("var element = arguments[0];element.parentNode.removeChild(element);", brw.find_element(By.TAG_NAME, 'base')) except: pass # removing the name brw.execute_script("var element = arguments[0];element.parentNode.removeChild(element);", brw.find_element(By.TAG_NAME, 'core-user-profile')) # changing the top url brw.execute_script("arguments[0].setAttribute(arguments[1], arguments[2]);", brw.find_element(By.CLASS_NAME, 'page-header__home'), 'href', '../../index.html') # saving recipe image img_url = brw.find_element(By.ID, 'recipe-card__image-loader').find_element(By.TAG_NAME, 'img').get_attribute('src') local_img_path = imgToFile(outputdir, recipeID, img_url) # change the image url to local brw.execute_script("arguments[0].setAttribute(arguments[1], arguments[2]);", brw.find_element(By.CLASS_NAME, 'core-tile__image'), 'srcset', '') brw.execute_script("arguments[0].setAttribute(arguments[1], arguments[2]);", brw.find_element(By.CLASS_NAME, 'core-tile__image'), 'src', local_img_path) # saving the file recipeToFile(brw, '{}recipes/{}.html'.format(outputdir, recipeID)) # extracting JSON info recipe = recipeToJSON(brw, recipeID) # saving JSON file, if needed if separate_json: print('[CD] Writing recipe to JSON file') with open('{}recipes/{}.json'.format(outputdir, recipeID), 'w') as outfile: json.dump(recipe, outfile) else: recipeData.append(recipe) # printing information c += 1 if c % 10 == 0: print('Dumped recipes: {}/{}'.format(c, len(recipesURLs))) except: pass # save JSON file, if needed if not separate_json: print('[CD] Writing recipes to JSON file') with open('{}data.json'.format(outputdir), 'w') as outfile: json.dump(recipeData, outfile) # logging out logoutURL = 'https://cookidoo.{}/profile/logout'.format(locale) brw.get(logoutURL) time.sleep(PAGELOAD_TO) # closing session print('[CD] Closing session\n[CD] Goodbye!') brw.close() if __name__ =='__main__': parser = argparse.ArgumentParser(description='Dump Cookidoo recipes from a valid account') parser.add_argument('webdriverfile', type=str, help='the path to the Chrome WebDriver file') parser.add_argument('outputdir', type=str, help='the output directory') parser.add_argument('-s', '--separate-json', action='store_true', help='Create a separate JSON file for each recipe; otherwise, a single data file will be generated') args = parser.parse_args() run(args.webdriverfile, args.outputdir, args.separate_json) ================================================ FILE: deploy.sh ================================================ #!/bin/bash NAME="cookidump" if [ "$#" != "1" ]; then echo "Usage: $0 " exit 0 fi git add . git commit -m "$1" git push -f $NAME master ================================================ FILE: requirements.txt ================================================ beautifulsoup4 selenium>=4.8.0