Browse Source

Python 3 compatibility

Nikola Kotur 3 years ago
parent
commit
27d0ea4567

+ 1 - 1
reddrip/__init__.py

@@ -3,5 +3,5 @@ import sys
 __VERSION__ = "1.2.3"
 
 if __name__ == "__main__":
-    print "Hi."
+    print ("Hi.")
     sys.exit(0)

+ 1 - 1
reddrip/commands/run.py

@@ -35,7 +35,7 @@ class RunCommand(Command):
             db=conf.glob["redis.db"],
         )
 
-        rip = Ripper(redis_conn, conf.glob["outdir"])
+        rip = Ripper(redis_conn, conf.glob["outdir"], conf.glob["client_id"], conf.glob["client_secret"])
 
         # Main runner.
         while True:

+ 5 - 4
reddrip/commands/stat.py

@@ -36,10 +36,10 @@ class StatCommand(Command):
 
         subreddits = redis_conn.smembers("subreddits")
 
-        print "Total of %d subreddits in database, following %s." % (
+        print("Total of %d subreddits in database, following %d." % (
             len(subreddits), len(conf)
-        )
-        print
+        ))
+        print()
 
         table = Texttable(max_width=0)
         table.set_deco(Texttable.HEADER)
@@ -56,6 +56,7 @@ class StatCommand(Command):
 
         size = processed = saved = existing = 0
         for subreddit in subreddits:
+            subreddit = subreddit.decode("utf-8")
             try:
                 dl_type = conf.subreddit(subreddit)["type"]
             except KeyError:
@@ -103,7 +104,7 @@ class StatCommand(Command):
             self._human_readable(size)
         ])
 
-        print table.draw()
+        print(table.draw())
 
     def _human_readable(self, size):
         size = float(size)

+ 10 - 6
reddrip/ripper/__init__.py

@@ -2,7 +2,7 @@ import os
 import logging
 import time
 import re
-import urlparse
+import urllib.parse
 import string
 import random
 
@@ -20,11 +20,15 @@ class Ripper(object):
     submission_limit = 50
     supported_exts = [ "jpg", "jpeg", "png", "gif" ]
 
-    def __init__(self, redis_conn, output_dir):
+    def __init__(self, redis_conn, output_dir, client_id, client_secret):
         self.redis_conn = redis_conn
         self.output_dir = os.path.abspath(output_dir)
 
-        self.reddit = praw.Reddit(user_agent=self.agent)
+        self.reddit = praw.Reddit(
+            client_id=client_id,
+            client_secret=client_secret,
+            user_agent=self.agent,
+        )
 
     def _random_name(self, size=10, chars=string.ascii_lowercase +
                                           string.digits):
@@ -37,7 +41,7 @@ class Ripper(object):
         return title
 
     def _get_ext(self, url):
-        url_info = urlparse.urlparse(url)
+        url_info = urllib.parse.urlparse(url)
         return url_info.path.split(".")[-1].lower()
 
     def _seen(self, sub_id, subreddit):
@@ -102,11 +106,11 @@ class Ripper(object):
             self.redis_conn.sadd("subreddits", sub["name"])
 
         if sub["type"] == "hot":
-            submissions = self.reddit.get_subreddit(sub["name"]).get_hot(
+            submissions = self.reddit.subreddit(sub["name"]).hot(
                 limit=self.submission_limit
             )
         elif sub["type"] == "new":
-            submissions = self.reddit.get_subreddit(sub["name"]).get_new(
+            submissions = self.reddit.subreddit(sub["name"]).new(
                 limit=self.submission_limit
             )
         else:

+ 3 - 3
reddrip/util/config.py

@@ -1,4 +1,4 @@
-import ConfigParser
+import configparser
 
 
 class Configuration(object):
@@ -19,7 +19,7 @@ class Configuration(object):
     def read(self):
         self.glob = {}
         self.subs = {}
-        config = ConfigParser.ConfigParser()
+        config = configparser.ConfigParser()
         config.read(self.config_file)
 
         for section in config.sections():
@@ -39,7 +39,7 @@ class Configuration(object):
         return len(self.subs)
 
     def subreddits(self):
-        for subreddit in self.subs.itervalues():
+        for subreddit in self.subs.values():
             yield subreddit
 
     def subreddit(self, name):

+ 1 - 1
reddrip/util/parse.py

@@ -4,7 +4,7 @@
 def setup_parser(parser, commands):
     subparsers = parser.add_subparsers()
 
-    for name, command in commands.iteritems():
+    for name, command in commands.items():
         sub_parser = subparsers.add_parser(name, help=command.help)
         command.setup_parser(sub_parser)
         sub_parser.set_defaults(