pump_model.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. '''
  2. Goblinoid: Experience all of MediaGoblin on an Android Device
  3. Copyright (C) 2015 Dylan Jeffers
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. '''
  15. # models for app root
  16. from __future__ import print_function
  17. from os.path import join
  18. from kivy.app import App
  19. from kivy.properties import ObjectProperty, DictProperty, ListProperty
  20. from kivy.uix.widget import Widget
  21. from pypump.models.note import Note as PumpNote
  22. from pypump.models.image import Image as PumpImage
  23. from pypump import JSONStore
  24. from pypump.exception import PyPumpException
  25. import requests
  26. # store for user's credentials
  27. class UserStore(JSONStore):
  28. @classmethod
  29. def get_filename(cls):
  30. return join(App.get_running_app().user_data_dir,
  31. 'MediaGoblin/credentials.json')
  32. class PumpModel(Widget):
  33. feed_content = ObjectProperty(None)
  34. pump = ObjectProperty(None)
  35. screen_dict = DictProperty()
  36. inbox = ListProperty()
  37. feed_data = ListProperty()
  38. def __init__(self, **kwargs):
  39. super(PumpModel, self).__init__(**kwargs)
  40. self.init_screen_dict()
  41. self.data_dir = join(App.get_running_app().user_data_dir,
  42. 'MediaGoblin')
  43. self.init_screen_dict()
  44. def get_data_dir(self):
  45. return self.data_dir
  46. def init_screen_dict(self):
  47. import loginview
  48. import actionview
  49. import screenviews
  50. self.screen_dict = dict([
  51. ('login', loginview.Login),
  52. ('webfinger', loginview.WebFinger),
  53. ('verify', loginview.Verify),
  54. ('enter', loginview.EnterApp),
  55. ('action', actionview.ActionLayout),
  56. ('settings', actionview.SettingsScreen),
  57. ('feed', screenviews.FeedView), ('media', screenviews.MediaView),
  58. ('profile', screenviews.ProfileView)
  59. ])
  60. def on_screen_dict(self, instance, value):
  61. print ('updated screen dict')
  62. # TODO function much too complicated, clean up
  63. def on_pump(self, instance, pump):
  64. print ('on_pump commencing')
  65. inbox = []
  66. data = []
  67. index = 0
  68. # TODO may want to skip bad data rather than breaking out from for loop
  69. try:
  70. for activity in pump.me.inbox.major[:5]:
  71. inbox.append(activity)
  72. data_dict = self.build_data_dict(activity, index)
  73. data.append(data_dict)
  74. data_dict = {}
  75. index += 1
  76. self.inbox = inbox
  77. self.feed_data = data
  78. except KeyError, e:
  79. print('KeyError: {0}'.format(e))
  80. self.inbox = inbox
  81. self.feed_data = data
  82. def build_data_dict(self, activity, index):
  83. data_dict = dict([
  84. ('index', index),
  85. ('actor', str(activity.obj.author)),
  86. ('post_time', [str(activity.published.hour),
  87. str(activity.published.minute)]),
  88. ('likes', self.display_likes(activity)),
  89. ('comments', self.display_comments(activity)),
  90. ('description', ''),
  91. ('image_content', ''),
  92. ('note_content', ''),
  93. ('else_content', '')
  94. ])
  95. print (len(data_dict['comments']))
  96. print(data_dict)
  97. data_dict.update(self.parse_activity_types(activity, index))
  98. return data_dict
  99. def parse_activity_types(self, activity, photo_index):
  100. if isinstance(activity.obj, PumpImage):
  101. return self.is_image_activity(activity, photo_index)
  102. elif isinstance(activity.obj, PumpNote):
  103. try:
  104. note_text = activity.obj.content
  105. return {"note_content": note_text}
  106. except AttributeError, e:
  107. note_text = e
  108. return {'note_content': note_text}
  109. except Exception, e1:
  110. note_text = e1
  111. return {'note_content': note_text}
  112. else:
  113. else_text = 'cannot display media'
  114. return {("else_content", else_text),
  115. ("description", 'no description')}
  116. def is_image_activity(self, activity, photo_index):
  117. try:
  118. url = activity.obj.original.url
  119. image_dir = './downloaded_images/image{0}.png'.format(photo_index)
  120. fout = open(image_dir, 'wb')
  121. client = self.pump.setup_oauth_client()
  122. request = requests.get(url, auth=client)
  123. fout.write(request.content)
  124. print('got request')
  125. description = activity.obj.content
  126. if description is None:
  127. description = 'no description'
  128. return {("image_content", image_dir),
  129. ("description", description)}
  130. except AttributeError, e:
  131. return {("else_content", e),
  132. ("description", 'no description')}
  133. except Exception, e1:
  134. return {("else_content", e1),
  135. ("description", 'no description')}
  136. def is_note_activity(self, activity):
  137. pass
  138. def is_video_activity(self, activity):
  139. pass
  140. def display_likes(self, activity):
  141. try:
  142. total_likes = activity.obj.likes.total_items
  143. if total_likes > 0:
  144. return '{0} likes'.format(str(total_likes))
  145. else:
  146. return 'no likes yet'
  147. except AttributeError:
  148. return 'no likes yet'
  149. except KeyError:
  150. return 'mediagoblin doesn\'t support likes yet'
  151. def display_description(self, activity):
  152. try:
  153. return str(activity.obj.description)
  154. except AttributeError:
  155. return 'no description'
  156. # TODO function is much too complicated, clean up
  157. def display_comments(self, activity):
  158. comment_array = []
  159. def build_comment_dict(author, content):
  160. return dict([
  161. ("author", author),
  162. ("content", content)
  163. ])
  164. def comment_array_len(comment_array):
  165. pass
  166. # print ('len: {0}'.format(len(comment_array)))
  167. try:
  168. activity.obj.comments
  169. for comment in activity.obj.comments:
  170. try:
  171. author = str(comment.author)
  172. content = str(comment.content)
  173. except UnicodeEncodeError, e1:
  174. print('display_comments: {0}'.format(e1))
  175. author = 'Unicode Error'
  176. content = str(e1)
  177. except PyPumpException, e2:
  178. print('display_comments: {0}'.format(e2))
  179. author = 'PyPumpException'
  180. content = str(e2)
  181. comment_array.append(build_comment_dict(author, content))
  182. if len(comment_array) == 0:
  183. comment_array.append(build_comment_dict('', 'no comments yet'))
  184. comment_array_len(comment_array)
  185. return comment_array
  186. except AttributeError, e:
  187. print('display_comments: {0}'.format(e))
  188. comment_array.append(build_comment_dict('AttributeError', str(e)))
  189. comment_array_len(comment_array)
  190. return comment_array
  191. except Exception, e1:
  192. print('display_comments: {0}'.format(e1))
  193. comment_array.append(build_comment_dict('ExceptionError', str(e1)))
  194. comment_array_len(comment_array)
  195. return comment_array