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

Fixes access error using file cache #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions ycast/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import logging
import sys
import ycast.generic as generic

from ycast import __version__
from ycast import server
Expand All @@ -23,6 +24,8 @@ def launch_server():
logging.debug("Debug logging enabled")
else:
logging.getLogger('werkzeug').setLevel(logging.WARNING)
# TODO Should cleaning cache be optional?
generic.clear_cache()
server.run(arguments.config, arguments.address, arguments.port)


Expand Down
64 changes: 63 additions & 1 deletion ycast/generic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
import os
import shutil
import hashlib

USER_AGENT = 'YCast'
VAR_PATH = os.path.expanduser("~") + '/.ycast'
CACHE_PATH = VAR_PATH + '/cache'
CACHE_NAME = 'shortid'


class Directory:
Expand All @@ -23,6 +26,11 @@ def generate_stationid_with_prefix(uid, prefix):
if not uid:
logging.error("Missing station id for full station id generation")
return None
if prefix != 'MY':
uid = write_uuid(str(uid))
if not uid:
logging.error("Unable to store uuid. See previous errors")
return None
return str(prefix) + '_' + str(uid)


Expand All @@ -37,7 +45,10 @@ def get_stationid_without_prefix(uid):
if len(uid) < 4:
logging.error("Could not extract stationid (Invalid station id length)")
return None
return uid[3:]
wopid = uid[3:]
if get_stationid_prefix(uid) != 'MY':
return read_uuid(wopid)
return wopid


def get_cache_path(cache_name):
Expand All @@ -50,3 +61,54 @@ def get_cache_path(cache_name):
logging.error("Could not create cache folders (%s) because of access permissions", cache_path)
return None
return cache_path


def get_checksum(feed, charlimit=12):
hash_feed = feed.encode()
hash_object = hashlib.md5(hash_feed)
digest = hash_object.digest()
xor_fold = bytearray(digest[:8])
for i, b in enumerate(digest[8:]):
xor_fold[i] ^= b
digest_xor_fold = ''.join(format(x, '02x') for x in bytes(xor_fold))
return digest_xor_fold[:charlimit]


def write_uuid(uid):
cache_path = get_cache_path(CACHE_NAME)
if not cache_path:
return None
shortid = get_checksum(uid)
id_file = cache_path + '/' + shortid
try:
with open(id_file, 'w') as file:
file.write(uid)
except PermissionError:
logging.error("Could not access station id file in cache (%s) because of access permissions",
id_file)
return None
return shortid


def read_uuid(shortid):
cache_path = get_cache_path(CACHE_NAME)
if not cache_path:
return None
id_file = cache_path + '/' + shortid
try:
with open(id_file, 'r') as file:
uid = file.read()
except PermissionError:
logging.error("Could not access station id file in cache (%s) because of access permissions",
id_file)
return None
return uid


def clear_cache():
try:
for root, dirs, files in os.walk(CACHE_PATH):
for d in dirs:
shutil.rmtree(os.path.join(root, d))
except OSError:
logging.error("Could not clean cache)")
14 changes: 1 addition & 13 deletions ycast/my_stations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import hashlib

import yaml

Expand Down Expand Up @@ -71,17 +70,6 @@ def get_stations_by_category(category):
if my_stations_yaml and category in my_stations_yaml:
for station_name in my_stations_yaml[category]:
station_url = my_stations_yaml[category][station_name]
station_id = str(get_checksum(station_name + station_url)).upper()
station_id = str(generic.get_checksum(station_name + station_url)).upper()
stations.append(Station(station_id, station_name, station_url, category))
return stations


def get_checksum(feed, charlimit=12):
hash_feed = feed.encode()
hash_object = hashlib.md5(hash_feed)
digest = hash_object.digest()
xor_fold = bytearray(digest[:8])
for i, b in enumerate(digest[8:]):
xor_fold[i] ^= b
digest_xor_fold = ''.join(format(x, '02x') for x in bytes(xor_fold))
return digest_xor_fold[:charlimit]