Repository: pymedusa/Medusa

Issue:
service beated up
### Before submitting your issue:

Enable debug logging in Medusa settings, reproduce the error (be sure to disable after the bug is fixed)

**Branch/Commit: `master`
**OS: `FreeBSD 11.1-RELEASE (iocage jail)`
**What you did: `fresh install -> add series -> saw errors about KeyError -> debug mode -> sleep -> reboot host`
**What happened: `service failed`
**What you expected: `it works :)`
**Logs:

```
root@medusa:/ # service medusa start
Starting medusa.
Traceback (most recent call last):
  File "/usr/local/medusa/start.py", line 9, in <module>
    main()
  File "/usr/local/medusa/medusa/__main__.py", line 2157, in main
    application.start(sys.argv[1:])
  File "/usr/local/medusa/medusa/__main__.py", line 339, in start
    self.initialize(console_logging=self.console_logging)
  File "/usr/local/medusa/medusa/__main__.py", line 1093, in initialize
    app_logger.rebuild_censored_list()
  File "/usr/local/medusa/medusa/logger/__init__.py", line 90, in rebuild_censored_list
    results |= {quote(item) for item in results}
  File "/usr/local/medusa/medusa/logger/__init__.py", line 90, in <setcomp>
    results |= {quote(item) for item in results}
  File "/usr/local/lib/python2.7/urllib.py", line 1298, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u20ac'
/usr/local/etc/rc.d/medusa: WARNING: failed to start medusa

```
I was on SickRage and I saw your awesome medusa. It perfectly worked before I add my series.
This is my installation script : 
```
pkg update
pkg install git python py27-sqlite3 unrar ffmpeg
pw groupadd multimedia -g 1001
pw useradd clement -u 1000 -g 1001
cd /usr/local
git clone git://github.com/pymedusa/Medusa.git
cp /usr/local/Medusa/runscripts/init.freebsd /usr/local/etc/rc.d/medusa
echo 'medusa_enable="YES"' >> /etc/rc.conf
echo 'medusa_user="clement"' >> /etc/rc.conf
echo 'medusa_group="multimedia"' >> /etc/rc.conf
echo 'medusa_datadir="/var/db/medusa"' >> /etc/rc.conf
mkdir /var/db/medusa
chown -R clement:multimedia /var/db/medusa /usr/local/Medusa
cd ..
mv Medusa/ medusa/
```
service beated up
### Before submitting your issue:

Enable debug logging in Medusa settings, reproduce the error (be sure to disable after the bug is fixed)

**Branch/Commit: `master`
**OS: `FreeBSD 11.1-RELEASE (iocage jail)`
**What you did: `fresh install -> add series -> saw errors about KeyError -> debug mode -> sleep -> reboot host`
**What happened: `service failed`
**What you expected: `it works :)`
**Logs:

```
root@medusa:/ # service medusa start
Starting medusa.
Traceback (most recent call last):
  File "/usr/local/medusa/start.py", line 9, in <module>
    main()
  File "/usr/local/medusa/medusa/__main__.py", line 2157, in main
    application.start(sys.argv[1:])
  File "/usr/local/medusa/medusa/__main__.py", line 339, in start
    self.initialize(console_logging=self.console_logging)
  File "/usr/local/medusa/medusa/__main__.py", line 1093, in initialize
    app_logger.rebuild_censored_list()
  File "/usr/local/medusa/medusa/logger/__init__.py", line 90, in rebuild_censored_list
    results |= {quote(item) for item in results}
  File "/usr/local/medusa/medusa/logger/__init__.py", line 90, in <setcomp>
    results |= {quote(item) for item in results}
  File "/usr/local/lib/python2.7/urllib.py", line 1298, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u20ac'
/usr/local/etc/rc.d/medusa: WARNING: failed to start medusa

```
I was on SickRage and I saw your awesome medusa. It perfectly worked before I add my series.
This is my installation script : 
```
pkg update
pkg install git python py27-sqlite3 unrar ffmpeg
pw groupadd multimedia -g 1001
pw useradd clement -u 1000 -g 1001
cd /usr/local
git clone git://github.com/pymedusa/Medusa.git
cp /usr/local/Medusa/runscripts/init.freebsd /usr/local/etc/rc.d/medusa
echo 'medusa_enable="YES"' >> /etc/rc.conf
echo 'medusa_user="clement"' >> /etc/rc.conf
echo 'medusa_group="multimedia"' >> /etc/rc.conf
echo 'medusa_datadir="/var/db/medusa"' >> /etc/rc.conf
mkdir /var/db/medusa
chown -R clement:multimedia /var/db/medusa /usr/local/Medusa
cd ..
mv Medusa/ medusa/
```


Discussion:
Guessing here;
Did you copy over the `config.ini` file from SickRage?
Does one of your passwords contain the Euro sign?
No copy config.ini, only clean install
Indeed ! In git password, not logon
Alright, for now set the git credentials to `""` in the config file, and medusa should run.
I'll look into fixing this. It needs to be tweaked to work with unicode characters.

For anyone interested, `urllib.quote` doesn't handle unicode strings, it needs to be a utf-8 encoded byte string.
work like a charm
EDIT : many thanks
Guessing here;
Did you copy over the `config.ini` file from SickRage?
Does one of your passwords contain the Euro sign?
No copy config.ini, only clean install
Indeed ! In git password, not logon
Alright, for now set the git credentials to `""` in the config file, and medusa should run.
I'll look into fixing this. It needs to be tweaked to work with unicode characters.

For anyone interested, `urllib.quote` doesn't handle unicode strings, it needs to be a utf-8 encoded byte string.
work like a charm
EDIT : many thanks

Resolving patch:
diff --git a/medusa/logger/__init__.py b/medusa/logger/__init__.py
--- a/medusa/logger/__init__.py
+++ b/medusa/logger/__init__.py
@@ -45,9 +45,8 @@
 from medusa import app
 from medusa.init.logconfig import standard_logger
 
-from requests.compat import quote
-
 from six import itervalues, string_types, text_type, viewitems
+from six.moves.urllib.parse import quote
 
 import subliminal
 
@@ -86,8 +85,14 @@ def rebuild_censored_list():
         elif value and value != '0':
             results.add(value)
 
+    def quote_unicode(value):
+        """Quote a unicode value by encoding it to bytes first."""
+        if isinstance(value, text_type):
+            return quote(value.encode(default_encoding, 'replace'))
+        return quote(value)
+
     # set of censored items and urlencoded counterparts
-    results |= {quote(item) for item in results}
+    results |= {quote_unicode(item) for item in results}
     # convert set items to unicode and typecast to list
     results = list({item.decode(default_encoding, 'replace')
                     if not isinstance(item, text_type) else item for item in results})


Test patch: