Advertisement
Guest User

calendar-search.py

a guest
Aug 3rd, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # read/search entries in Jolla calendar db.
  5. # runs this script as root (with sudo) because the calendar db is located at a protected place.
  6. # https://together.jolla.com/question/53418/search-in-calendar-app/
  7.  
  8. # This code is placed into public domain.
  9.  
  10. # version 1.00 31.07.2015  rolfa  first version
  11. # version 1.01 31.07.2015  rolfa  ordered results
  12. # version 1.02 03.08.2015  rolfa  raw_input
  13.  
  14. '''
  15. EXAMPLES
  16. ========
  17.  
  18. Interactive search: sudo calendar-search.py
  19.  
  20. List all entries: sudo calendar-search.py "%"
  21.  
  22. List birthdays: sudo calendar-search.py "%" | grep "BIRTHDAY"
  23.  
  24. List entries containing "lunch" and "paul" (in that order): sudo calendar-search.py "lunch%paul"
  25. '''
  26.  
  27.  
  28. '''
  29. HOW TO ACTIVATE SUDO
  30. ====================
  31. as root:
  32.  
  33. usermod -a -G wheel nemo
  34.  
  35. pkcon install sudo
  36. pkcon install nano
  37. EDITOR="nano" visudo
  38.  
  39.  ## Uncomment to allow members of group wheel to execute any command
  40.  %wheel ALL=(ALL) ALL
  41.  
  42. reboot
  43.  
  44. Test as nemo:
  45. ls /home/nemo/.local/share/system/privileged
  46.  -> fails
  47. sudo ls /home/nemo/.local/share/system/privileged
  48.  -> works
  49. '''
  50.  
  51. import os, sys, sqlite3
  52.  
  53. if os.geteuid() != 0:
  54.     exit('This script needs root privileges, please try again using "sudo".')
  55.  
  56. cal_file = '/home/nemo/.local/share/system/privileged/Calendar/mkcal/db'
  57.  
  58. like_base = '''
  59. (summary like '%%%s%%' or location like '%%%s%%' or description like '%%%s%%')
  60. '''
  61.  
  62. sql_base = '''
  63. select date(datestartlocal, 'unixepoch') start, '' end, category, summary, location, description FROM components where %s and category = 'BIRTHDAY' and DateDeleted = 0
  64. union all
  65. select datetime(datestartlocal, 'unixepoch') start, datetime(dateendduelocal, 'unixepoch') end, category, summary, location, description FROM components where %s and category <> 'BIRTHDAY' and DateDeleted = 0
  66. order by category desc, start
  67. '''
  68.  
  69. try:
  70.     db = sqlite3.connect(cal_file)
  71. except Exception as e:
  72.     exit('could not open calendar DB: %s' % e)
  73.  
  74. while True:
  75.  
  76.     if len(sys.argv) > 1:
  77.         searchtext = sys.argv[1]
  78.         sys.argv[1] = ''
  79.     else:
  80.         searchtext = raw_input('Searchtext (empty to exit): ')
  81.    
  82.     if not searchtext: break
  83.  
  84.     like = like_base % (searchtext, searchtext, searchtext)
  85.     sql = sql_base % (like, like)
  86.  
  87.     try:
  88.         rows = db.execute(sql).fetchall()
  89.     except Exception as e:
  90.         exit('error while accessing calendar db: %s' % e)
  91.    
  92.     for row in rows:
  93.         for el in row:
  94.             print '%s\t' % el.encode('utf-8'),
  95.         print
  96.  
  97.     print 'hits: %s\n' % len(rows)
  98.  
  99. try:
  100.   db.close()
  101. except Exception as e:
  102.   pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement