⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lj_procs.py

📁 pyLJclient是一个跨平台的livejournal客户端
💻 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

import httplib, urllib, time, sys
from lj.lj_exceptions import *
from lj import config

LIVEJOURNAL = "www.livejournal.com:80"
CGI_LOCATION = "/cgi-bin/log.cgi"
LINEENDINGS = 'pc'

def _process_request(params, url=LIVEJOURNAL, script_loc=CGI_LOCATION):
    #following only necessary for mac
    #params = params + '&' + urllib.urlencode({'lineendings':LINEENDINGS})
    try:
	h = httplib.HTTP(url)
	h.putrequest("POST", script_loc)
	h.putheader("Content-type", "application/x-www-form-urlencoded")
	h.putheader("Content-length", "%d" % len(params))
	h.endheaders()
	h.send(params)
    except:
	raise NetworkError, "Host Not Found or Host Not Responding"
    else:
	try:
	    reply, msg, hdrs = h.getreply()
	except:
	    raise "Host not replying..."
	if int(reply) == 200:
	    try:
		data = h.getfile().read()
	    except:
		raise "Network Busy..."
	    return urllib.unquote_plus(data)
	else:
	    return reply

def _pre_populate(**kwargs):
    pre_populate_args = {}
    pre_populate_args['user']=kwargs['user']
    if kwargs.has_key('hpassword') and kwargs['hpassword']:
        pre_populate_args['hpassword'] = kwargs['hpassword']
	print "using hpassword..."
    elif kwargs.has_key('password') and kwargs['password']:
	pre_populate_args['password'] = kwargs['password']
    else:
	raise "Must Provide Function" # LJMissingParameterError('No Password Provided')
    if kwargs.has_key('fastserver') and int(kwargs['fastserver']) == 1:
	pre_populate_args['Cookie: ljfastserver'] = 1
	print "fast cookie enabled..."
    #print "pre_populate_args = -->%s"%str(pre_populate_args)
    return pre_populate_args
    
def checkfriends(user, password='', hpassword='', lastupdate='', mask='', fastserver=0):
    """Mode that clients can use to poll the server to see if their friends
    list has been updated. This request is extremely quick, and is the
    preferred way for users to see when their friends list is updated, rather
    than pounding on reload in their browser, which is stressful on the
    serves.
    More Info: http://www.livejournal.com/developer/modeinfo.bml?checkfriends
    """
    print "Now running checkfriends..."
    vars=_pre_populate(user=user, password=password, hpassword=hpassword,
	    mask=mask, fastserver=fastserver)
    vars.update({'mode':'checkfriends', 'lastupdate':lastupdate})
    if mask:
	vars['mask'] = mask
	print "Mask enabled..."
    params = urllib.urlencode(vars)
    return _process_request(params)   

def editevent(user, password='', hpassword='', itemid='', event='', subject='', security='',
	    allowmask='', year='', mon='',day='',hour='',min='', usejournal='', fastserver=0):
    "Edit or delete a user's past journal entry"
    # if the event is to be deleted, send only the following information
    print "Running editevent..."
    vars = _pre_populate(user=user, password=password, hpassword=hpassword, fastserver=fastserver)
    vars.update({'mode':'editevent',
            'itemid':itemid,
	    'subject':subject})
    if not event:
	vars['event'] = ''
        params = urllib.urlencode(vars)
    #event is being modified
    else:
	vars['event'] = event
	vars['year'] = time.localtime()[0]
	vars['mon'] = time.localtime()[1]
	vars['day'] = time.localtime()[2]
	vars['hour'] = time.localtime()[3]
	vars['min'] = time.localtime()[4]	
        params = urllib.urlencode(vars)
    return _process_request(params)
                               
def editfriendgroups(user, password='', hpassword=''):
    "Edit the user's defined groups of friends."
    raise "Unimplemented function"

def editfriends(user, password='', hpassword=''):
    "Add, edit, or delete friends from the user's friends list."
    raise "Unimplemented function"

def friendof(user, password='', hpassword='', fastserver=0):
    """Returns a list of which other LiveJournal users list this user as their
    friend."""
    print "Running friendof..."
    vars = _pre_populate(user=user, password=password, hpassword=hpassword, fastserver=fastserver)
    vars.update({'mode':'friendof'})
    params = urllib.urlencode(vars)
    response = _process_request(params)
    return response

def getdaycounts(user, password, hpassword=''):
    """This mode retrieves the number of journal entries per day.
    Useful for populating calendar widgets in GUI clients."""
    raise "Unimplemented function"

def getevents(user, password='',hpassword='', truncate='', prefersubject='', noprops='',
	    selecttype='', lastsync='', year='', mon='', day='', howmany='', beforedate='',
	    itemid=-1, lineendings='pc', usejournal='', fastserver=0):
    """Download parts of the user's journal. 
    "lastsync format: yyyy-mm-dd hh:mm:ss"""
    vars=_pre_populate(user=user, password=password, hpassword=hpassword, fastserver=fastserver)
    vars.update({'mode':'getevents'})
    if selecttype == 'one':
	vars['itemid']=itemid
        return getevent(vars)
    elif selecttype == 'day':
	vars.update({'year':year,
		 'mon':mon,
		 'day':day})
        return geteventsbyday(vars)
    elif selecttype == 'syncitems':
	vars.update({'prefersubject':0,
                     'noprops':0,
                     'selecttype':'syncitems',
                     'lastsync':lastsync})
        #lastsync format: yyyy-mm-dd hh:mm:ss
        params = urllib.urlencode(vars)
        return _process_request(params)

def getfriendgroups(user, password='', hpassword='', fastserver=0):
    "Retrieves a list of the user's defined groups of friends."
    vars = _pre_populate(user=user, password=password, hpassword=hpassword, fastserver=fastserver)
    vars.update({'mode':'getfriendgroups'})
    params = urllib.urlencode(vars)
    return _process_request(params)

def getfriends(user, password='', hpassword='', includefriendof='', includegroups='',
		friendlimit='', fastserver=0):
    "Returns a list of which other LiveJournal users this user lists as their friend."
    vars = _pre_populate(user=user, password=password, hpassword=hpassword, fastserver=fastserver)
    vars.update({'mode':'getfriends'})
    params = urllib.urlencode(vars)
    # puts return string into key value pairs and returns the dict
    return _process_request(params)

def login(user, password='', hpassword='', mood_index=0, getmenus=0, getpickws=1, getpickwurls=1):
    print "logging in..."
    vars = _pre_populate(user=user, password=password, hpassword=hpassword)
    vars.update({'mode':'login',
		 'getmoods':mood_index,
	         'clientversion':config.getClientName()+'/'+config.getClientVersion(),
		 'getmenus':getmenus,
		 'getpickws':getpickws,
		 'getpickwurls':getpickwurls})
    print "Anouncing client version:   %s"%(config.getClientName()+'/'+config.getClientVersion())
    params = urllib.urlencode(vars)
    parsed_output = _process_request(params)
    #print "------server login response-------------\n", parsed_output, '\n---------------------------\n'
    return parsed_output

def postevent(user, password='', hpassword='', event='', subject='', security='', 
		allowmask='',journal='', year='',mon='', day='',hour='', min='', fastserver=0, 
		prop_items={}):
    print "Running postevent..."
    print "security=%s, allowmask=%s"%(security, allowmask)
    vars = _pre_populate(user=user, password=password, hpassword=hpassword, journal=journal, 
			    fastserver=fastserver)
    vars.update({'mode':'postevent',
		'event':event,
		'subject':subject,
		'security':security,
		'year':year,
		'mon':mon,
		'day':day,
		'hour':hour,
		'min':min})
    if security == 'usemask':
	vars.update({'allowmask':allowmask})
    vars.update(prop_items)
    if journal:
	vars['usejournal'] = journal
	print "using journal: %s"%journal
    print "passing vars: ", vars
    params = urllib.urlencode(vars)
    response = _process_request(params)
    # print response
    return response

def syncitems(user, password='', hpassword=''):
    """
    Returns a list (or part of a list) of all the items (journal entries, to-do
    items, comments) that have been created or updated on LiveJournal since you
    last downloaded them. Note that the items themselves are not returned
    --- only the item type and the item number. After you get this you have to
    go fetch the items using another protocol mode. For journal entries (type "L"),
    use the getevents mode with a selecttype of "syncitems". 
    """
    raise "Unimplemented function"

#-----------------------helper functions (NOT PART OF OFFICIAL MODE LIST)---------------------

def getevent(vars):
    print "Running getevent..."
    vars.update({'prefersubject':0,
            'noprops':0,
            'selecttype':'one'})
    params = urllib.urlencode(vars)
    return _process_request(params)

def geteventsbyday(vars):
    print "Running geteventsbyday..."
    vars.update({'prefersubject':0,
            'noprops':0,
            'selecttype':'day'})
    params = urllib.urlencode(vars)
    return _process_request(params)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -