Commits

hizel  committed 66414d1

строим облачки для отдельного топика по url

  • Participants
  • Parent commits 07503c5

Comments (0)

Files changed (2)

File lorparser.py

 from StringIO import StringIO
 
 from logging import debug, info, error, warning
+from os.path import exists
 
 import shelve
 import codecs
     comment_sign = bs_comment.find('div', {'class': 'sign'})
     comment_sign_a = comment_sign.find('a')
     # remove datatime string
-    comment_sign_a.extract() 
+    if comment_sign_a:
+        comment_sign_a.extract() 
     comment_title = bs_comment.find('h2')
     # remove title string
     if comment_title:
             cache[newkey] = cache[k]
             del cache[k]
 
+def drawonetopic(url, imgname, size=(1024,900), count=200, crop=False):
+    m = re_topicurl.match(url)
+    if not m:
+        error('invalid topic url')
+        return
+
+    topic_id = int(m.group(1))
+    db_name = 'topic-%d.db' % topic_id
+    if not exists(db_name):
+        error('for create image need fetch topic')
+
+    db = shelve.open(db_name)
+
+    f_comments = StringIO()
+
+    for k in db.keys():
+        if db[k]:
+            content = db[k]
+            content_text, time = commentparse(content)
+            print >>f_comments, content_text
+
+    content_comments = re_tag.sub('', f_comments.getvalue())
+
+    try_comments = True
+
+    counts_comments = get_tag_counts(content_comments)
+
+    f_comments.close()
+
+    if len(counts_comments) < 5:
+        warning('zero comments')
+        try_comments = False
+
+    if try_comments:
+        info('#top 30 comments#')
+        tags_comments = make_tags(counts_comments)
+        for tag in tags_comments[:30]:
+            info('%s:%s' % ( tag['tag'].encode('utf8'), tag['size']))
+        create_tag_image(
+                tags_comments[:count], 
+                '%s-topic-%d.png' % (imgname,topic_id),
+                fontname='Ubuntu',
+                background=(0, 0, 0, 255),
+                size=size,
+                crop=crop
+                )
+
 def fetchonetopic(url):
     m = re_topicurl.match(url)
     if not m:

File lorwordcloud.py

 from os.path import exists
 from re import compile as rcompile
 
-from lorparser import parse_comment_links,topicsfetch,create_image,fixoldcache  
+from lorparser import parse_comment_links,topicsfetch,create_image,fixoldcache,\
+        fetchonetopic,drawonetopic
+
+import logging
 
 re_opt_size = rcompile(r'^(\d+)x(\d+)$')
 
 parser = OptionParser(
         usage='%prog COMMAND OPTIONS',
-        description = 'Commands: fetch - fetch all comment for nick, draw - draw image for nick'
+        description = 'Commands: fetch,draw,fetchtopic,drawtopic'
         )
 
-group = OptionGroup(parser, 'for all commands')
+parser.add_option(
+        '-v', 
+        action='store_true', 
+        dest='verbose',
+        help = 'verbosity output'
+        )
+
+group = OptionGroup(parser, 'for fetch,draw')
 
 group.add_option(
         '-n', '--nick',
 
 parser.add_option_group(group)
 
-group = OptionGroup(parser, 'for draw - fetch all comment')
+group = OptionGroup(parser, 'for draw,drawtopic')
 
 group.add_option(
         '-i', '--image',
         '-s', '--size',
         dest='imgsize',
         help='size image(default=1024x900))', 
-        default=200
+        default='1024x900'
         )
 group.add_option(
         '--crop',
 
 parser.add_option_group(group)
 
+group = OptionGroup(parser, 'for fetchtopic,drawtopic')
+group.add_option(
+        '-u','--url',
+        dest='url',
+        help='url like http://www.linux.org.ru/forum/talks/6056608'
+        )
+parser.add_option_group(group)
+
 if __name__ == '__main__':
     (options, args) = parser.parse_args()
     fetch = False
     draw = False
+    fetchtopic = False
+    drawtopic = False
     if len(args) < 1:
         parser.error('need one command')
     if 'fetch' in args:
         else:
             isize = (int(m_opt_size.group(1)), int(m_opt_size.group(2)))
         draw = True
+    if options.verbose:
+        logging.basicConfig(level=logging.INFO)
+    if 'fetchtopic' in args:
+        if not options.url:
+            parser.error('for fetchtopic need url -u')
+        fetchtopic = True
+    if 'drawtopic' in args:
+        if not options.url:
+            parser.error('for drawtopic need url -u')
+        if not options.image:
+            parser.error('for draw topic need image option')
+        m_opt_size = re_opt_size.match(options.imgsize)
+        if not m_opt_size:
+            parser.error('invalid format size options need like "100x100" ')
+        else:
+            isize = (int(m_opt_size.group(1)), int(m_opt_size.group(2)))
+        drawtopic = True
 
-    cachename = '%s.cache2' % options.nick
-    if not exists(cachename) and draw and not fetch:
-        parser.error('not exists %s, need fetch?' % cachename)
+    if fetch or draw:
+        cachename = '%s.cache2' % options.nick
+        if not exists(cachename) and draw and not fetch:
+            parser.error('not exists %s, need fetch?' % cachename)
 
-    cache = shopen(cachename)
-    fixoldcache(cache)
+        cache = shopen(cachename)
+        fixoldcache(cache)
 
     if fetch:
         parse_comment_links(options.nick, cache)
         create_image(cache, options.image, size=isize, count=options.tagcount,
                 crop = options.crop)
 
-    cache.close()
+    if fetchtopic:
+        fetchonetopic(options.url)
 
+    if drawtopic:
+        drawonetopic(options.url, options.image, size=isize, count=options.tagcount,
+                crop = options.crop)
+
+    if fetch or draw:
+        cache.close()
+