📄 lj_journal.py
字号:
# pyLJclient - a python based live journal client with a wxPython gui
# Copyright (C) 2002 Sameer Chowdhury
# refer to "about this software.txt" for info on licensing
from lj import lj_procs, friend, posted_event, friend_groups
from lj.lj_exceptions import *
from lj.moods import Moods, Mood
import string, re, time
# OO layer that sits on top of lj_procs. Not really optimized, so all
# re expressions compiled on the fly for now.
class LJ_Journal:
def __init__(self, username='', password='', name='', hpassword=''):
self.username = username
if hpassword:
self.hpassword=hpassword
self.password = ''
elif password:
self.password = password
self.hpassword=''
self.name = name
self.journal_access_list = []
self.pic_url_association = {}
self.ljfastserver = 0
self.moods = Moods()
if self.username and (self.hpassword or self.password):
self.login()
def getUser(self):
return self.username
# logs in to the online journal
# sets the ljfastserver for subsequent responses, if it is found
# sets the journal_access_list, a list of journals that can be posted to
# updates the child object moods with the current moods
def login(self):
if self.hpassword:
raw = lj_procs.login(self.username, hpassword = self.hpassword,
mood_index=self.moods.getmoodcount())
else:
raw = lj_procs.login(self.username, self.password, mood_index=self.moods.getmoodcount())
login_response = self._process_login_string(raw)
if login_response['success'] != 'OK':
raise UnknownUser("Invalid username or password")
self.name = login_response['name']
try:
self.ljfastserver = login_response['fastserver']
except:
self.ljfastserver = 0
self._populate_access_journals(login_response)
if login_response.has_key('mood_count') and (int(login_response['mood_count']) > 0):
self._extract_moods(login_response)
if login_response.has_key('pickw_count') and (int(login_response['pickw_count']) > 0):
self._populate_pics_with_urls(login_response)
self._extract_friend_groups(login_response)
def getmoods(self):
return self.moods
def get_pic_url_association(self):
return self.pic_url_association
# returns a list of Friend objects
def get_friends(self):
if self.hpassword:
raw_result = lj_procs.friendof(self.username, hpassword=self.hpassword)
else:
raw_result = lj_procs.friendof(self.username, self.password)
friends_list = self._process_friends_list(raw_result)
return friends_list
def getWriteJournals(self):
return self.journal_access_list
def add_mood_args(self, mood):
return {'prop_current_mood':mood}
def add_mood_pic(self, mood_pic):
return {'prop_picture_keyword': mood_pic}
def add_mood_id(self, mood_id):
return {'prop_current_moodid':mood_id}
def add_current_music(self, music):
return {'prop_current_music':music}
# returns an Event object
def post_event(self, new_event):
evt = new_event.getBodyText()
subject = new_event.getSubjectText()
security_mask = new_event.get_security_group().get_mask()
if security_mask:
security='usemask'
else:
security = new_event.get_security_group().get_name()
journal = new_event.getPostJournal()
time_stamp = new_event.getTimeStamp()
year = time_stamp.getYear()
mon = time_stamp.getMonth()
day = time_stamp.getDay()
hour = time_stamp.getHour()
min = time_stamp.getMinute()
mood = new_event.getMood()
mood_id = new_event.getMoodID()
mood_pic = new_event.getMoodPic()
current_music = new_event.getMusic()
print "security name: %s"%security
print "security mask: %s"%security_mask
prop_items = {}
if mood:
prop_items.update(self.add_mood_args(mood))
if mood_pic:
prop_items.update(self.add_mood_pic(mood_pic))
if mood_id:
prop_items.update(self.add_mood_id(mood_id))
if current_music:
prop_items.update(self.add_current_music(current_music))
if self.hpassword:
raw_result = lj_procs.postevent(self.username, hpassword=self.hpassword, event = evt,
subject=subject, security=security, allowmask=security_mask, journal=journal,
year=year, mon=mon, day=day, hour=hour, min=min, fastserver=self.ljfastserver,
prop_items=prop_items)
else:
raw_result = lj_procs.postevent(self.username, self.password, event = evt,
subject = subject, security = security, allowmask = security_mask, journal = journal,
year=year, mon=mon, day=day, hour=hour, min=min, fastserver=self.ljfastserver,
prop_items = prop_items)
# print "lj user post_event result:\n%s"%raw_result
self._process_event_string(raw_result)
return 1
def get_events_by_day(self, day, month=time.localtime()[1],
year=time.localtime()[0]):
raw = lj_procs.getevents(self.username, self.password, 'day',
year,month,day)
return self._process_event(raw)
def friendof(self):
raw = lj_procs.friendof(self.username, self.password)
friends_list = self._process_friends_list(raw)
return friends_list
# not properly implemented, just returns the raw result
# no processing performed on string returned
# this is because i don't see it used anywhere
def getfriendgroups(self):
raw = lj_procs.getfriendgroups(self.username, self.password)
return raw
def delete_event(self, itemid):
if self.hpassword:
raw = lj_procs.editevent(self.username, hpassword = self.hpassword, itemid= itemid, event='')
else:
raw = lj_procs.editevent(self.username, self.password, itemid= itemid, event='')
response_dict = self._dictify_return_string(raw)
self._perform_basic_error_checking(response_dict)
return 1
def edit_event(self, itemid, evt, subject=''):
if self.hpassword:
raw = lj_procs.editevent(self.username, hpassword= self.hpassword, itemid=itemid,
event=evt, subject=subject)
else:
raw = lj_procs.editevent(self.username, self.password, itemid=itemid, event=evt,
subject=subject)
response_dict = self._dictify_return_string(raw)
self._perform_basic_error_checking(response_dict)
return 1
def checkfriends(self):
if self.hpassword:
raw = lj_procs.checkfriends(self.username, hpassword = self.hpassword)
else:
raw = lj_procs.checkfriends(self.username, self.password)
response_dict = self._dictify_return_string(raw)
self._perform_basic_error_checking(response_dict)
if response_dict['new'] == '1':
return 1
else:
return 0
def get_friend_groups(self):
return self.groups
#------------ functions for internal use------------------------------------------
def _extract_friend_groups(self, login_response):
print "--------- extracting friend groups --------"
temp_group_list = friend_groups.Groups()
temp_group_list.initialize_defaults()
for key, val in login_response.items():
pat = "^frgrp_(?P<id>[\d]+)_name$"
result = re.search(pat, key)
if result:
group_name = val
group_number = result.group('id')
group_sort_order = login_response['frgrp_'+result.group('id')+'_sortorder']
new_group = friend_groups.User_Defined_Group(group_name, group_number, group_sort_order)
temp_group_list.add_group(new_group)
print "temp group list ----->"
print temp_group_list
self.groups = temp_group_list
def _extract_moods(self, login_response):
print "--------------- In extract_moods ---------------"
temp_moods_list = []
for key, val in login_response.items():
pat = "^mood_(?P<id>[\d]+)_id$"
result = re.search(pat, key)
if result:
mood_id = val
mood_val = login_response['mood_'+result.group('id')+'_name']
temp_moods_list.append(Mood(mood_id, mood_val))
# print mood_id, '------', mood_val
self.moods.update(temp_moods_list)
def _populate_pics_with_urls(self, login_response):
print "---------------- Now building association of pic names with urls --------------------"
temp_pic_association = {}
for key, val in login_response.items():
pat = "^pickw_(?P<id>[\d]+)$"
result = re.search(pat, key)
if result:
print "pic keyword name: (%s) found"%val
pick_name = val
pick_url = login_response['pickwurl_'+result.group('id')]
print "associate url (%s) found"%pick_url
temp_pic_association[pick_name] = pick_url
print temp_pic_association
self.pic_url_association.update(temp_pic_association)
#helper string processing function for get_friends function
def _process_friends_list(self, start_string):
basic_tokens = start_string.split('\n')
friends_list = []
#------------state machine below----------------------
#after friend_name has been found the machine goes into the state of
#waiting for the friend_user_name to come along. Once a friend_user
#name has been found the machine is set to start state again, which
#is to look for friend_name. I wish i could just include a link
#from here to a jpg or better yet, interactive image of this state
#machine alas my ide is not so advanced yet.
friend_name_found = 0
for token in basic_tokens:
if friend_name_found == 1:
if re.compile("^friendof_[\d]+_user$").search(token):
friend_user = basic_tokens[basic_tokens.index(token)+1]
a_friend = friend.Friend(friend_name, friend_user)
friends_list.append(a_friend)
friend_name_found = 0
else:
if re.compile("^friendof_[\d]+_name$").search(token):
friend_name = basic_tokens[basic_tokens.index(token)+1]
friend_name_found = 1
return friends_list
# helper string processing function for post_event function
def _process_event_string(self, raw):
response_dict = self._dictify_return_string(raw)
self._perform_basic_error_checking(response_dict)
#return event.Event(response_dict['itemid'])
def _perform_basic_error_checking(self, response_dict):
if response_dict['success'] == 'FAIL':
raise "EVENT UNSUCCESSFUL, %s"%response_dict['errmsg']
def _process_event(self, raw_event):
tokens = string.split(raw_event,'\n')
evt, evt_time, itemid, security, subject = '', '', '', '', ''
events = []
# start feeding the state machine with tokens
# i is the current position of the input tape
i = 0
while (i < len(tokens)):
# search for event start marker
if re.compile("^events_[\d]+_event$").search(tokens[i]):
# jump to state 2, keep feeding data into event string
# as long as eventtime marker has not been found
i = i + 1
while(1):
if re.compile("^events_[\d]+_eventtime$").search(tokens[i]):
break
evt = evt + tokens[i]
i = i + 1
# move marker to the evt_time value
i = i + 1
# now marker is on time, read it into the time variable
evt_time = tokens[i]
# skip over itemid label and move directly to its value
i= i + 2
# store itemid variable
itemid = tokens[i]
# advance marker to next token
i = i + 1
# now detect if there is an optional security item
if re.compile("^events_[\d]+_security$").search(tokens[i]):
i = i + 1
security = tokens[i]
i = i + 1
# now detect if there is a subject
# if there is, advance marker and read its value into variable
if re.compile("^events_[\d]+_subject$").search(tokens[i]):
i = i + 1
subject = tokens[i]
event_obj = posted_event.Event(itemid, evt, evt_time,
subject, security)
events.append(event_obj)
evt, evt_time, itemid, security, subject = '', '', '', '', ''
i = i + 1
return events
# extract name value pairs from raw string returned by livejournal
def _dictify_return_string(self, raw):
response_properties = {}
# split into words
tokens = string.split(raw, '\n')
# split function leaves a dangling item at the end of the list
# so truncate it
tokens = tokens[:-1]
#iterate through each name, and add name-value to response_properties
for i in xrange(0, len(tokens), 2):
response_properties[tokens[i]]=tokens[i+1]
return response_properties
def _process_login_string(self, login_string):
result = self._dictify_return_string(login_string)
return result
def _populate_access_journals(self, dictified_response):
for key, val in dictified_response.items():
if re.compile("^access_[\d]+$").search(key):
self.journal_access_list.append(val)
self.journal_access_list.insert(0, self.username)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -