рд╕рд╛рдЗрдЯ рдХреЗ рдЖрдзрд╛рд░ рдкрд░ рдореЗрд▓рд┐рдВрдЧ рд╕реВрдЪреА рдХрд░рдиреЗ рдХрд╛ рдПрдХ рд╕реБрд╡рд┐рдзрд╛рдЬрдирдХ рддрд░реАрдХрд╛ (Django рдХреЗ рд▓рд┐рдП рдПрдХ рдЙрджрд╛рд╣рд░рдг рдХреЗ рд╕рд╛рде)



рдореИрдВ рдЖрдкрдХреЛ рд╣рдорд╛рд░реА рдПрдХ рдкрд░рд┐рдпреЛрдЬрдирд╛ рдХреЗ рд╡рд┐рддрд░рдг рдХреЛ рд╡реНрдпрд╡рд╕реНрдерд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Your MailingList рдкреНрд░рджрд╛рддрд╛ (YMLP) рд╕реЗрд╡рд╛ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рдЕрдиреБрднрд╡ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдмрддрд╛рдКрдВрдЧрд╛ ред



рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ, рд╕рдорд╕реНрдпрд╛ рд╕рд░рд▓ рдФрд░ рдХрд╛рдлреА рд╕рд╛рдорд╛рдиреНрдп рд╣реИ - рд╕рд╛рдЗрдЯ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХрд╛ рдПрдХ рдбреЗрдЯрд╛рдмреЗрд╕ рд╣реИ, рдХрд╛рд░реНрдп рд╕рд╣реА рдврдВрдЧ рд╕реЗ рдФрд░ рдЖрд╕рд╛рдиреА рд╕реЗ рдЗрд╕ рдкрд░ рд╕рдорд╛рдЪрд╛рд░ рдкрддреНрд░ рд╡реНрдпрд╡рд╕реНрдерд┐рдд рдХрд░рдирд╛ рд╣реИред рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛рдУрдВ рдХреЛ рд╕рдорд╛рдЪрд╛рд░ рдкрддреНрд░ рд╕реЗ рд╕рджрд╕реНрдпрддрд╛ рд╕рдорд╛рдкреНрдд рдХрд░рдиреЗ рдореЗрдВ рд╕рдХреНрд╖рдо рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдП, рд╣рдореЗрдВ рд╕рдорд╛рдЪрд╛рд░ рдкрддреНрд░ рд╕реЗ рд╕рджрд╕реНрдпрддрд╛ рд╕рдорд╛рдкреНрдд рдХрд░рдиреЗ рдореЗрдВ рд╕рдХреНрд╖рдо рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдП, рдФрд░ рдпрд╣ рднреА (рдмрд╣реБрдд рд╡рд╛рдВрдЫрдиреАрдп) рдкреНрд░реЛрдлрд╝рд╛рдЗрд▓ рдлрд╝реАрд▓реНрдб рджреНрд╡рд╛рд░рд╛ рдЪрдпрди рдХрд░реЗрдВ (рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдХреЗрд╡рд▓ 30 рд╕реЗ рдЕрдзрд┐рдХ рдорд╣рд┐рд▓рд╛рдУрдВ рдХреЛ рднреЗрдЬрдиреЗ рдХреЗ рд▓рд┐рдП) рдФрд░ рдЖрдБрдХрдбрд╝реЛрдВ рдХреЛ рдЯреНрд░реИрдХ рдХрд░реЗрдВ (рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдХрд┐рддрдиреЗ рд▓реЛрдЧреЛрдВ рдиреЗ рдкрддреНрд░ рдЦреЛрд▓рд╛)ред



рдореИрдВ рдзреНрдпрд╛рди рджреЗрддрд╛ рд╣реВрдВ рдХрд┐ рдЗрд╕ рддрдереНрдп рдХреЗ рдорджреНрджреЗрдирдЬрд░ рдХрд┐ рд╣рдорд╛рд░реА рдкрд░рд┐рдпреЛрдЬрдирд╛ рдЕрдВрддрд░рд░рд╛рд╖реНрдЯреНрд░реАрдп рд╣реИ, рд╣рдорд╛рд░реА рджреГрд╖реНрдЯрд┐ рдХреЗ рдХреНрд╖реЗрддреНрд░ рдореЗрдВ рджреЛ рд╕реЗрд╡рд╛рдПрдВ рдереАрдВ рдЬреЛ рд╣рдореЗрдВ рдРрд╕реА рдЪреАрдЬреЗрдВ рдХрд░рдиреЗ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддреА рд╣реИрдВ - рдпрд╣ рдкрд╣рд▓реЗ рд╕реЗ рд╣реА рдЙрд▓реНрд▓рд┐рдЦрд┐рдд YMLP рдФрд░ NetAtlantic рд╣реИ ред рджреВрд╕рд░реА рд╕реЗрд╡рд╛ рдХрд╛рдлрд╝реА рдХрдо рд▓рдЪреАрд▓реА рдФрд░ рд╕реБрд╡рд┐рдзрд╛рдЬрдирдХ рд╣реИ, рдЗрд╕рд▓рд┐рдП рд╣рдо рдЗрд╕рдХреЗ рдореБрдЦреНрдп рдкреНрд░рддрд┐рдпреЛрдЧреА, YMLP (рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ, рдмрд╣реБрдд рдкрд╣рд▓реЗ рдирд╣реАрдВ, NetAtlantic рдХрд╛ рдПрдХ рдмрдбрд╝рд╛ рдкреНрд▓рд╕ рдерд╛ - рдПрдХ рдПрдкреАрдЖрдИ рдХреА рдЙрдкрд╕реНрдерд┐рддрд┐ рдкрд░ рдмрд╕реЗ рдереЗ, рд▓реЗрдХрд┐рди рдпрд╣ YPPP рдкрд░ рднреА рджрд┐рдЦрд╛рдИ рджрд┐рдпрд╛, рдЗрд╕рд▓рд┐рдП рд╕рдм рдХреБрдЫ рджрд┐рди рдХреЗ рд░реВрдк рдореЗрдВ рд╕реНрдкрд╖реНрдЯ рд╣реЛ рдЧрдпрд╛)ред



рддреЛ, YMLP рдЖрдкрдХреЛ рдЙрдкрд░реЛрдХреНрдд рд╕рднреА рдХреЛ рдХрд░рдиреЗ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддрд╛ рд╣реИ, рд╕рд╛рде рд╣реА рдмрд╣реБрдд рдЕрдзрд┐рдХред рдПрдХ рдореБрдлреНрдд рдЦрд╛рддрд╛ рдЖрдкрдХреЛ 1000 рд▓реЛрдЧреЛрдВ рддрдХ рдХреЗ рдбреЗрдЯрд╛рдмреЗрд╕ рдкрд░ рд╕рдорд╛рдЪрд╛рд░ рдкрддреНрд░ рднреЗрдЬрдиреЗ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддрд╛ рд╣реИред рдЖрдк рд╕рдореВрд╣ рдмрдирд╛ рд╕рдХрддреЗ рд╣реИрдВред рдЖрдк рдиреНрдпреВрдЬрд╝рд▓реЗрдЯрд░ рдЯреЗрдореНрдкрд▓реЗрдЯ рд╕реНрдЯреЛрд░ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред рдЖрдк рдордирдорд╛рдиреЗ рдХреНрд╖реЗрддреНрд░ рдЬреЛрдбрд╝ рд╕рдХрддреЗ рд╣реИрдВ рдФрд░ рдЙрди рдкрд░ рдЪрдпрди рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред рдЖрдк рдХрд╕реНрдЯрдо рдлреЙрд░реНрдо рдмрдирд╛ рд╕рдХрддреЗ рд╣реИрдВред PHP рдореЗрдВ рдПрдкреАрдЖрдИ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдПрдХ рдЖрдзрд┐рдХрд╛рд░рд┐рдХ рдХрд╛рд░реНрдпрд╛рдиреНрд╡рдпрди рд╣реИ, рд▓реЗрдХрд┐рди рдЗрд╕рдХреЗ рд╕рдВрдЪрд╛рд▓рди рдХрд╛ рддрдВрддреНрд░ рдЗрддрдирд╛ рд╕рд░рд▓ рд╣реИ рдХрд┐ рдХрд┐рд╕реА рдЕрдиреНрдп рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рднрд╛рд╖рд╛ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдирд╛ рдореБрд╢реНрдХрд┐рд▓ рдирд╣реАрдВ рд╣реИ, рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдкрд╛рдпрдердиред



рдПрдХ рдмрд╣реБрдд рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╡рд┐рд╢реЗрд╖рддрд╛ - рдЖрдк рдХрд┐рд╕реА рднреА рдЙрдк-рдЦрд╛рддреЗ рдХреЛ рдмрдирд╛ рд╕рдХрддреЗ рд╣реИрдВ рдФрд░ рдЙрдирдХреЗ рдЕрдзрд┐рдХрд╛рд░реЛрдВ рдХреЛ рдХреЙрдиреНрдлрд╝рд┐рдЧрд░ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред



рд╣рдо рдкрд┐рдЫрд▓реЗ рдирд╡рдВрдмрд░ рд╕реЗ рд╕реЗрд╡рд╛ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░ рд░рд╣реЗ рд╣реИрдВ, рдХреЛрдИ рд╢рд┐рдХрд╛рдпрдд рдирд╣реАрдВ рд╣реИред рдореИрдВ рдЗрд╕рдХреА рдЕрддреНрдпрдзрд┐рдХ рдЕрдиреБрд╢рдВрд╕рд╛ рдХрд░рддрд╛ рд╣реВрдВред



рддреЛ, Django рдХреЗ рд▓рд┐рдП рд╡рд╛рджрд╛ рдХрд┐рдпрд╛ рдЧрдпрд╛ рдЙрджрд╛рд╣рд░рдг YMLP рдХреЗ рд╕рд╛рде рдЗрд╕ рдврд╛рдВрдЪреЗ рдХреЗ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдЖрдзрд╛рд░ рдХреЛ рдПрдХреАрдХреГрдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдПрдХ рдХрд╛рд░реНрдпрд╢реАрд▓ рд╕реНрдХреНрд░рд┐рдкреНрдЯ рд╣реИред рдпрд╣ рдХреНрд░реЛрди рджреНрд╡рд╛рд░рд╛ рдмреБрд▓рд╛рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ, рд╡рд╛рдИрдПрдордПрд▓рдкреА рдбреЗрдЯрд╛рдмреЗрд╕ рдХреЗ рд╕рд╛рде рд╕рд╛рдЗрдЯ рдбреЗрдЯрд╛рдмреЗрд╕ рдХреЛ рд╕рд┐рдВрдХреНрд░рдирд╛рдЗрдЬрд╝ рдХрд░рддрд╛ рд╣реИ:



#!/usr/bin/env python2.6 <br/>

import os , sys , urllib <br/>

from random import randint<br/>

from datetime import datetime <br/>

<br/>

sys . path . append ( '/home/www-data/university' ) <br/>

os . environ [ 'DJANGO_SETTINGS_MODULE' ] = 'settings' <br/>

<br/>

API_URL = "www.ymlp.com/api/" <br/>

<br/>

try :<br/>

from django. utils import simplejson as json<br/>

except ImportError :<br/>

import simplejson as json<br/>

<br/>

from accounts. models import UserProfile<br/>

from settings import ROOT, YMLP_API_KEY, YMLP_USERNAME, YMLP_GROUP_NAME<br/>

<br/>

_groups = None <br/>

_fields = None <br/>

<br/>

class YMLPException ( Exception ) :<br/>

pass <br/>

<br/>

def _search_id ( dicts, search_key, search_value ) :<br/>

for d in dicts:<br/>

if d. get ( search_key, "" ) . lower ( ) == search_value. lower ( ) :<br/>

return d. get ( "ID" ) <br/>

<br/>

def get_fields_list ( ) :<br/>

global _fields<br/>

<br/>

if _fields is None :<br/>

params = { <br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Fields.GetList" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if type ( result ) is dict and result. has_key ( "Code" ) :<br/>

raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>

_fields = result<br/>

return _fields<br/>

<br/>

def get_groups_list ( ) :<br/>

global _groups<br/>

<br/>

if _groups is None :<br/>

params = { <br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Groups.GetList" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if type ( result ) is dict and result. has_key ( "Code" ) :<br/>

raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>

_groups = result<br/>

return _groups<br/>

<br/>

def add_contact ( email , group_name, fields, overrule_unsubscribed= False ) :<br/>

groups_list = get_groups_list ( ) <br/>

<br/>

group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>

if group_id is None :<br/>

raise YMLPException ( None , "Invalid group name" ) <br/>

<br/>

fields_list = get_fields_list ( ) <br/>

params = { <br/>

"Email" : email ,<br/>

"GroupID" : group_id,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

<br/>

for name,value in fields. items ( ) :<br/>

field_id = _search_id ( fields_list, "FieldName" , name ) or _search_id ( fields_list, "Alias" , name ) <br/>

if field_id is not None :<br/>

if type ( value ) is unicode :<br/>

value = value. encode ( "utf-8" ) <br/>

params [ "Field" +field_id ] = value<br/>

<br/>

params [ "OverruleUnsubscribedBounced" ] = "1" if overrule_unsubscribed else "0" <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Add" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

<br/>

def unsubscribe_contact ( email ) :<br/>

params = { <br/>

"Email" : email ,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Unsubscribe" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

<br/>

if type ( result ) is dict :<br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

else :<br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

raise YMLPException ( None , "" ) <br/>

<br/>

def delete_contact ( email , group_name ) :<br/>

groups_list = get_groups_list ( ) <br/>

<br/>

group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>

if group_id is None :<br/>

raise YMLPException ( None , "Invalid group name" ) <br/>

<br/>

params = { <br/>

"Email" : email ,<br/>

"GroupID" : group_id,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Delete" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

<br/>

if type ( result ) is dict :<br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

else :<br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

raise YMLPException ( None , "" ) <br/>

<br/>

if __name__ == "__main__" :<br/>

print datetime . now ( ) . isoformat ( " " ) <br/>

print "Exporting..." <br/>

count = 0 <br/>

<br/>

def _subscribe_profile ( profile , fields ) :<br/>

global count, YMLP_GROUP_NAME<br/>

add_contact ( profile . user . email , YMLP_GROUP_NAME, fields ) <br/>

profile . subscribed = True <br/>

profile . save ( ) <br/>

print [ "o" , "O" ] [ randint ( 0 , 1 ) ] ,<br/>

sys . stdout . flush ( ) <br/>

count += 1 <br/>

<br/>

for profile in UserProfile. objects . filter ( subscription_accepted= True , subscribed= False ) :<br/>

if profile . filled :<br/>

fields = { <br/>

"name" : profile . user . first_name ,<br/>

"lastname" : profile . user . last_name ,<br/>

"sex" : profile . gender ,<br/>

"city" : profile . city ,<br/>

"country" : profile . country ,<br/>

"reference" : profile . reference ,<br/>

"year" : 0 if profile . birth_date == None else profile . birth_date . year <br/>

} <br/>

else :<br/>

fields = { } <br/>

<br/>

done = False <br/>

first_try = True <br/>

<br/>

while not done:<br/>

try :<br/>

_subscribe_profile ( profile , fields ) <br/>

done = True <br/>

except YMLPException as inst:<br/>

print >> sys . stderr , "Error #%s while adding contact \" %s \" . The error was: %s" % ( inst [ 0 ] , profile . user . email , inst [ 1 ] ) <br/>

if not first_try:<br/>

done = True <br/>

continue <br/>

if inst [ 0 ] == "1" and inst [ 1 ] == None : # contact already exists, let's update <br/>

print "Contact \" %s \" already exists, trying to update" % profile . user . email <br/>

try :<br/>

delete_contact ( profile . user . email , YMLP_GROUP_NAME ) <br/>

except YMLPException:<br/>

pass <br/>

first_try = False <br/>

continue <br/>

elif inst [ 0 ] == "3" : # "Email address already in selected groups" <br/>

print "Setting the 'Subscribed' flag..." <br/>

profile . subscribed = True <br/>

profile . save ( ) <br/>

elif inst [ 0 ] == "4" : # "This email address has previously unsubscribed" <br/>

print "Removing the 'Subscription Accepted' flag..." <br/>

profile . subscription_accepted = False <br/>

profile . save ( ) <br/>

elif inst [ 0 ] in [ "2" , "100" , "101" , "102" ] :<br/>

sys . exit ( - 1 ) <br/>

done = True <br/>

<br/>

print " \n Done. Contacts added: %d, total users number: %d. Bye." % ( count, UserProfile. objects . all ( ) . count ( ) ) <br/>

print "" . zfill ( 80 ) . replace ( "0" , "-" ) <br/>

print <br/>







#!/usr/bin/env python2.6 <br/>

import os , sys , urllib <br/>

from random import randint<br/>

from datetime import datetime <br/>

<br/>

sys . path . append ( '/home/www-data/university' ) <br/>

os . environ [ 'DJANGO_SETTINGS_MODULE' ] = 'settings' <br/>

<br/>

API_URL = "www.ymlp.com/api/" <br/>

<br/>

try :<br/>

from django. utils import simplejson as json<br/>

except ImportError :<br/>

import simplejson as json<br/>

<br/>

from accounts. models import UserProfile<br/>

from settings import ROOT, YMLP_API_KEY, YMLP_USERNAME, YMLP_GROUP_NAME<br/>

<br/>

_groups = None <br/>

_fields = None <br/>

<br/>

class YMLPException ( Exception ) :<br/>

pass <br/>

<br/>

def _search_id ( dicts, search_key, search_value ) :<br/>

for d in dicts:<br/>

if d. get ( search_key, "" ) . lower ( ) == search_value. lower ( ) :<br/>

return d. get ( "ID" ) <br/>

<br/>

def get_fields_list ( ) :<br/>

global _fields<br/>

<br/>

if _fields is None :<br/>

params = { <br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Fields.GetList" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if type ( result ) is dict and result. has_key ( "Code" ) :<br/>

raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>

_fields = result<br/>

return _fields<br/>

<br/>

def get_groups_list ( ) :<br/>

global _groups<br/>

<br/>

if _groups is None :<br/>

params = { <br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Groups.GetList" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if type ( result ) is dict and result. has_key ( "Code" ) :<br/>

raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>

_groups = result<br/>

return _groups<br/>

<br/>

def add_contact ( email , group_name, fields, overrule_unsubscribed= False ) :<br/>

groups_list = get_groups_list ( ) <br/>

<br/>

group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>

if group_id is None :<br/>

raise YMLPException ( None , "Invalid group name" ) <br/>

<br/>

fields_list = get_fields_list ( ) <br/>

params = { <br/>

"Email" : email ,<br/>

"GroupID" : group_id,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

<br/>

for name,value in fields. items ( ) :<br/>

field_id = _search_id ( fields_list, "FieldName" , name ) or _search_id ( fields_list, "Alias" , name ) <br/>

if field_id is not None :<br/>

if type ( value ) is unicode :<br/>

value = value. encode ( "utf-8" ) <br/>

params [ "Field" +field_id ] = value<br/>

<br/>

params [ "OverruleUnsubscribedBounced" ] = "1" if overrule_unsubscribed else "0" <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Add" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

<br/>

def unsubscribe_contact ( email ) :<br/>

params = { <br/>

"Email" : email ,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Unsubscribe" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

<br/>

if type ( result ) is dict :<br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

else :<br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

raise YMLPException ( None , "" ) <br/>

<br/>

def delete_contact ( email , group_name ) :<br/>

groups_list = get_groups_list ( ) <br/>

<br/>

group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>

if group_id is None :<br/>

raise YMLPException ( None , "Invalid group name" ) <br/>

<br/>

params = { <br/>

"Email" : email ,<br/>

"GroupID" : group_id,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Delete" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

<br/>

if type ( result ) is dict :<br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

else :<br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

raise YMLPException ( None , "" ) <br/>

<br/>

if __name__ == "__main__" :<br/>

print datetime . now ( ) . isoformat ( " " ) <br/>

print "Exporting..." <br/>

count = 0 <br/>

<br/>

def _subscribe_profile ( profile , fields ) :<br/>

global count, YMLP_GROUP_NAME<br/>

add_contact ( profile . user . email , YMLP_GROUP_NAME, fields ) <br/>

profile . subscribed = True <br/>

profile . save ( ) <br/>

print [ "o" , "O" ] [ randint ( 0 , 1 ) ] ,<br/>

sys . stdout . flush ( ) <br/>

count += 1 <br/>

<br/>

for profile in UserProfile. objects . filter ( subscription_accepted= True , subscribed= False ) :<br/>

if profile . filled :<br/>

fields = { <br/>

"name" : profile . user . first_name ,<br/>

"lastname" : profile . user . last_name ,<br/>

"sex" : profile . gender ,<br/>

"city" : profile . city ,<br/>

"country" : profile . country ,<br/>

"reference" : profile . reference ,<br/>

"year" : 0 if profile . birth_date == None else profile . birth_date . year <br/>

} <br/>

else :<br/>

fields = { } <br/>

<br/>

done = False <br/>

first_try = True <br/>

<br/>

while not done:<br/>

try :<br/>

_subscribe_profile ( profile , fields ) <br/>

done = True <br/>

except YMLPException as inst:<br/>

print >> sys . stderr , "Error #%s while adding contact \" %s \" . The error was: %s" % ( inst [ 0 ] , profile . user . email , inst [ 1 ] ) <br/>

if not first_try:<br/>

done = True <br/>

continue <br/>

if inst [ 0 ] == "1" and inst [ 1 ] == None : # contact already exists, let's update <br/>

print "Contact \" %s \" already exists, trying to update" % profile . user . email <br/>

try :<br/>

delete_contact ( profile . user . email , YMLP_GROUP_NAME ) <br/>

except YMLPException:<br/>

pass <br/>

first_try = False <br/>

continue <br/>

elif inst [ 0 ] == "3" : # "Email address already in selected groups" <br/>

print "Setting the 'Subscribed' flag..." <br/>

profile . subscribed = True <br/>

profile . save ( ) <br/>

elif inst [ 0 ] == "4" : # "This email address has previously unsubscribed" <br/>

print "Removing the 'Subscription Accepted' flag..." <br/>

profile . subscription_accepted = False <br/>

profile . save ( ) <br/>

elif inst [ 0 ] in [ "2" , "100" , "101" , "102" ] :<br/>

sys . exit ( - 1 ) <br/>

done = True <br/>

<br/>

print " \n Done. Contacts added: %d, total users number: %d. Bye." % ( count, UserProfile. objects . all ( ) . count ( ) ) <br/>

print "" . zfill ( 80 ) . replace ( "0" , "-" ) <br/>

print <br/>







#!/usr/bin/env python2.6 <br/>

import os , sys , urllib <br/>

from random import randint<br/>

from datetime import datetime <br/>

<br/>

sys . path . append ( '/home/www-data/university' ) <br/>

os . environ [ 'DJANGO_SETTINGS_MODULE' ] = 'settings' <br/>

<br/>

API_URL = "www.ymlp.com/api/" <br/>

<br/>

try :<br/>

from django. utils import simplejson as json<br/>

except ImportError :<br/>

import simplejson as json<br/>

<br/>

from accounts. models import UserProfile<br/>

from settings import ROOT, YMLP_API_KEY, YMLP_USERNAME, YMLP_GROUP_NAME<br/>

<br/>

_groups = None <br/>

_fields = None <br/>

<br/>

class YMLPException ( Exception ) :<br/>

pass <br/>

<br/>

def _search_id ( dicts, search_key, search_value ) :<br/>

for d in dicts:<br/>

if d. get ( search_key, "" ) . lower ( ) == search_value. lower ( ) :<br/>

return d. get ( "ID" ) <br/>

<br/>

def get_fields_list ( ) :<br/>

global _fields<br/>

<br/>

if _fields is None :<br/>

params = { <br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Fields.GetList" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if type ( result ) is dict and result. has_key ( "Code" ) :<br/>

raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>

_fields = result<br/>

return _fields<br/>

<br/>

def get_groups_list ( ) :<br/>

global _groups<br/>

<br/>

if _groups is None :<br/>

params = { <br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Groups.GetList" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if type ( result ) is dict and result. has_key ( "Code" ) :<br/>

raise YMLPException ( result [ "Code" ] , result. get ( "Output" ) ) <br/>

_groups = result<br/>

return _groups<br/>

<br/>

def add_contact ( email , group_name, fields, overrule_unsubscribed= False ) :<br/>

groups_list = get_groups_list ( ) <br/>

<br/>

group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>

if group_id is None :<br/>

raise YMLPException ( None , "Invalid group name" ) <br/>

<br/>

fields_list = get_fields_list ( ) <br/>

params = { <br/>

"Email" : email ,<br/>

"GroupID" : group_id,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

<br/>

for name,value in fields. items ( ) :<br/>

field_id = _search_id ( fields_list, "FieldName" , name ) or _search_id ( fields_list, "Alias" , name ) <br/>

if field_id is not None :<br/>

if type ( value ) is unicode :<br/>

value = value. encode ( "utf-8" ) <br/>

params [ "Field" +field_id ] = value<br/>

<br/>

params [ "OverruleUnsubscribedBounced" ] = "1" if overrule_unsubscribed else "0" <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Add" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

<br/>

def unsubscribe_contact ( email ) :<br/>

params = { <br/>

"Email" : email ,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Unsubscribe" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

<br/>

if type ( result ) is dict :<br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

else :<br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

raise YMLPException ( None , "" ) <br/>

<br/>

def delete_contact ( email , group_name ) :<br/>

groups_list = get_groups_list ( ) <br/>

<br/>

group_id = _search_id ( groups_list, "GroupName" , group_name ) <br/>

if group_id is None :<br/>

raise YMLPException ( None , "Invalid group name" ) <br/>

<br/>

params = { <br/>

"Email" : email ,<br/>

"GroupID" : group_id,<br/>

"Key" : YMLP_API_KEY,<br/>

"Username" : YMLP_USERNAME,<br/>

"Output" : "JSON" ,<br/>

} <br/>

f = urllib . urlopen ( "%s%s?%s" % ( API_URL, "Contacts.Delete" , urllib . urlencode ( params ) ) ) <br/>

result = json. loads ( f. read ( ) ) <br/>

<br/>

if type ( result ) is dict :<br/>

if result. get ( "Code" ) == "0" :<br/>

return True <br/>

else :<br/>

raise YMLPException ( result. get ( "Code" ) , result. get ( "Output" ) ) <br/>

raise YMLPException ( None , "" ) <br/>

<br/>

if __name__ == "__main__" :<br/>

print datetime . now ( ) . isoformat ( " " ) <br/>

print "Exporting..." <br/>

count = 0 <br/>

<br/>

def _subscribe_profile ( profile , fields ) :<br/>

global count, YMLP_GROUP_NAME<br/>

add_contact ( profile . user . email , YMLP_GROUP_NAME, fields ) <br/>

profile . subscribed = True <br/>

profile . save ( ) <br/>

print [ "o" , "O" ] [ randint ( 0 , 1 ) ] ,<br/>

sys . stdout . flush ( ) <br/>

count += 1 <br/>

<br/>

for profile in UserProfile. objects . filter ( subscription_accepted= True , subscribed= False ) :<br/>

if profile . filled :<br/>

fields = { <br/>

"name" : profile . user . first_name ,<br/>

"lastname" : profile . user . last_name ,<br/>

"sex" : profile . gender ,<br/>

"city" : profile . city ,<br/>

"country" : profile . country ,<br/>

"reference" : profile . reference ,<br/>

"year" : 0 if profile . birth_date == None else profile . birth_date . year <br/>

} <br/>

else :<br/>

fields = { } <br/>

<br/>

done = False <br/>

first_try = True <br/>

<br/>

while not done:<br/>

try :<br/>

_subscribe_profile ( profile , fields ) <br/>

done = True <br/>

except YMLPException as inst:<br/>

print >> sys . stderr , "Error #%s while adding contact \" %s \" . The error was: %s" % ( inst [ 0 ] , profile . user . email , inst [ 1 ] ) <br/>

if not first_try:<br/>

done = True <br/>

continue <br/>

if inst [ 0 ] == "1" and inst [ 1 ] == None : # contact already exists, let's update <br/>

print "Contact \" %s \" already exists, trying to update" % profile . user . email <br/>

try :<br/>

delete_contact ( profile . user . email , YMLP_GROUP_NAME ) <br/>

except YMLPException:<br/>

pass <br/>

first_try = False <br/>

continue <br/>

elif inst [ 0 ] == "3" : # "Email address already in selected groups" <br/>

print "Setting the 'Subscribed' flag..." <br/>

profile . subscribed = True <br/>

profile . save ( ) <br/>

elif inst [ 0 ] == "4" : # "This email address has previously unsubscribed" <br/>

print "Removing the 'Subscription Accepted' flag..." <br/>

profile . subscription_accepted = False <br/>

profile . save ( ) <br/>

elif inst [ 0 ] in [ "2" , "100" , "101" , "102" ] :<br/>

sys . exit ( - 1 ) <br/>

done = True <br/>

<br/>

print " \n Done. Contacts added: %d, total users number: %d. Bye." % ( count, UserProfile. objects . all ( ) . count ( ) ) <br/>

print "" . zfill ( 80 ) . replace ( "0" , "-" ) <br/>

print <br/>











рдЕрдкрдиреЗ рдиреНрдпреВрдЬрд╝рд▓реЗрдЯрд░реНрд╕ рдХреЗ рд╕рд╛рде рд╢реБрднрдХрд╛рдордирд╛рдПрдБ! :-)




All Articles