-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from aapatre/develop
Release 0.3 PR
- Loading branch information
Showing
15 changed files
with
362 additions
and
320 deletions.
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 |
---|---|---|
|
@@ -224,4 +224,10 @@ Pipfile.lock | |
|
||
settings.yaml | ||
|
||
pyproject.toml | ||
poetry.lock | ||
|
||
# Editor specific | ||
.vscode | ||
|
||
# Cache files | ||
.course_cache |
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
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import datetime | ||
import json | ||
import os | ||
|
||
|
||
class CourseCache(object): | ||
""" | ||
Basic cache to keep details on courses already scraped | ||
""" | ||
|
||
def __init__(self): | ||
self._file_name = ".course_cache" | ||
self._cache = [] | ||
self._load_cache() | ||
|
||
def __contains__(self, url: str) -> bool: | ||
""" | ||
Simply check if the url is already in the cache | ||
:param str url: URL to check the cache for | ||
:return: | ||
""" | ||
return url in [c["url"] for c in self._cache] | ||
|
||
def _load_cache(self) -> None: | ||
""" | ||
Load the cache into memory when we initialize | ||
:return: | ||
""" | ||
file_mode = "r" if os.path.isfile(self._file_name) else "w+" | ||
with open(self._file_name, file_mode) as f: | ||
cached_data = f.read().splitlines() | ||
if cached_data: | ||
self._cache = list(map(json.loads, cached_data)) | ||
|
||
def _append_cache(self, data: str) -> None: | ||
""" | ||
Append the new data to the cache | ||
:param str data: Data to append to the cache | ||
:return: | ||
""" | ||
with open(self._file_name, "a") as f: | ||
f.write(f"{data}\n") | ||
|
||
def add(self, url: str, status: str) -> None: | ||
""" | ||
Add a result our cache | ||
:param str url: URL of the udemy course to cache | ||
:param str status: The status of the course determined by the script | ||
:return: | ||
""" | ||
_data_to_cache = { | ||
"url": url, | ||
"status": status, | ||
"date": datetime.datetime.utcnow().isoformat(), | ||
} | ||
self._cache.append(_data_to_cache) | ||
self._append_cache(json.dumps(_data_to_cache)) |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class RobotException(Exception): | ||
""" | ||
You have been identified as a robot on Udemy site | ||
""" | ||
|
||
pass |
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
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
Oops, something went wrong.